atcoder#AGC057E. [AGC057E] RowCol/ColRow Sort
[AGC057E] RowCol/ColRow Sort
Score : points
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 .
2 2
0 0
1 2
4
The four matrices that satisfy the conditions are:
, , , .
We can verify that , 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
, 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