#AGC057E. [AGC057E] RowCol/ColRow Sort

[AGC057E] RowCol/ColRow Sort

Score : 14001400 points

Problem Statement

Consider the following operations on an H×WH\times W matrix A=(Ai,j)A = (A_{i,j}) (1iH,1jW1\leq i\leq H, 1\leq j\leq W).

  • Row-sort: Sort every row in ascending order. That is, sort Ai,1,,Ai,WA_{i,1},\ldots,A_{i,W} in ascending order for every ii.
  • Column-sort: Sort every column in ascending order. That is, sort A1,j,,AH,jA_{1,j},\ldots,A_{H,j} in ascending order for every jj.

You are given an H×WH\times W matrix B=(Bi,j)B = (B_{i,j}). Find the number of H×WH\times W matrices AA that satisfy both of the following conditions, modulo 998244353998244353.

  • Performing row-sort and then column-sort on AA produces BB.
  • Performing column-sort and then row-sort on AA produces BB.

Constraints

  • 1H,W15001\leq H, W\leq 1500
  • 0Bi,j90\leq B_{i,j}\leq 9
  • Bi,jBi,j+1B_{i,j}\leq B_{i,j+1}, for any 1iH1\leq i\leq H and 1jW11\leq j\leq W - 1.
  • Bi,jBi+1,jB_{i,j}\leq B_{i+1,j}, for any 1jW1\leq j\leq W and 1iH11\leq i\leq H - 1.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

HH WW

B1,1B_{1,1} B1,2B_{1,2} \ldots B1,WB_{1,W}

\vdots

BH,1B_{H,1} BH,2B_{H,2} \ldots BH,WB_{H,W}

Output

Print the number of matrices AA that satisfy the conditions, modulo 998244353998244353.

2 2
0 0
1 2
4

The four matrices that satisfy the conditions are:

(0012)\begin{pmatrix}0&0\\1&2\end{pmatrix}, (0021)\begin{pmatrix}0&0\\2&1\end{pmatrix}, (1200)\begin{pmatrix}1&2\\0&0\end{pmatrix}, (2100)\begin{pmatrix}2&1\\0&0\end{pmatrix}.

We can verify that (2100)\begin{pmatrix}2&1\\0&0\end{pmatrix}, for example, satisfies the conditions as follows.

  • Performing row-sort and then column-sort: $\begin{pmatrix}2&1\\0&0\end{pmatrix}\to \begin{pmatrix}1&2\\0&0\end{pmatrix} \to \begin{pmatrix}0&0\\1&2\end{pmatrix}$.
  • Performing column-sort and then row-sort:$\begin{pmatrix}2&1\\0&0\end{pmatrix}\to \begin{pmatrix}0&0\\2&1\end{pmatrix} \to \begin{pmatrix}0&0\\1&2\end{pmatrix}$.
3 3
0 1 3
2 4 7
5 6 8
576

(576301482)\begin{pmatrix}5&7&6\\3&0&1\\4&8&2\end{pmatrix}, for example, satisfies the conditions, which can be verified as follows.

  • Performing row-sort and then column-sort: $\begin{pmatrix}5&7&6\\3&0&1\\4&8&2\end{pmatrix}\to \begin{pmatrix}5&6&7\\0&1&3\\2&4&8\end{pmatrix} \to \begin{pmatrix}0&1&3\\2&4&7\\5&6&8\end{pmatrix}$.
  • Performing column-sort and then row-sort: $\begin{pmatrix}5&7&6\\3&0&1\\4&8&2\end{pmatrix}\to \begin{pmatrix}3&0&1\\4&7&2\\5&8&6\end{pmatrix} \to \begin{pmatrix}0&1&3\\2&4&7\\5&6&8\end{pmatrix}$.
3 5
0 0 0 1 1
0 0 1 1 2
0 1 1 2 2
10440
1 7
2 3 3 6 8 8 9
1260