#agc057e. [agc057_e]RowCol/ColRow Sort
[agc057_e]RowCol/ColRow Sort
Problem Statement
Consider the following operations on an matrix ().
- Row-sort: Sort every row in ascending order. That is, sort in ascending order for every .
- Column-sort: Sort every column in ascending order. That is, sort in ascending order for every .
You are given an matrix . Find the number of matrices that satisfy both of the following conditions, modulo .
- Performing row-sort and then column-sort on produces .
- Performing column-sort and then row-sort on produces .
Constraints
- , for any and .
- , for any and .
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the number of matrices that satisfy the conditions, modulo .
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