#agc057e. [agc057_e]RowCol/ColRow Sort

[agc057_e]RowCol/ColRow Sort

Problem Statement

Consider the following operations on an HtimesWH\\times W matrix A=(Ai,j)A = (A_{i,j}) (1leqileqH,1leqjleqW1\\leq i\\leq H, 1\\leq j\\leq W).

  • Row-sort: Sort every row in ascending order. That is, sort Ai,1,ldots,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,ldots,AH,jA_{1,j},\\ldots,A_{H,j} in ascending order for every jj.

You are given an HtimesWH\\times W matrix B=(Bi,j)B = (B_{i,j}). Find the number of HtimesWH\\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

  • 1leqH,Wleq15001\\leq H, W\\leq 1500
  • 0leqBi,jleq90\\leq B_{i,j}\\leq 9
  • Bi,jleqBi,j+1B_{i,j}\\leq B_{i,j+1}, for any 1leqileqH1\\leq i\\leq H and 1leqjleqW11\\leq j\\leq W - 1.
  • Bi,jleqBi+1,jB_{i,j}\\leq B_{i+1,j}, for any 1leqjleqW1\\leq j\\leq W and 1leqileqH11\\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\\ldots B1,WB_{1,W} vdots\\vdots BH,1B_{H,1} BH,2B_{H,2} ldots\\ldots BH,WB_{H,W}

Output

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


Sample Input 1

2 2
0 0
1 2

Sample Output 1

4

The four matrices that satisfy the conditions are:

\\begin{pmatrix}0&0\\\\1&2\\end{pmatrix}, \\begin{pmatrix}0&0\\\\2&1\\end{pmatrix}, \\begin{pmatrix}1&2\\\\0&0\\end{pmatrix}, \\begin{pmatrix}2&1\\\\0&0\\end{pmatrix}.

We can verify that \\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}$.


Sample Input 2

3 3
0 1 3
2 4 7
5 6 8

Sample Output 2

576

$\\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}$.


Sample Input 3

3 5
0 0 0 1 1
0 0 1 1 2
0 1 1 2 2

Sample Output 3

10440

Sample Input 4

1 7
2 3 3 6 8 8 9

Sample Output 4

1260