AAAA 1 P4 - Minimex

View as PDF

Submit solution


Points: 17 (partial)
Time limit: 2.0s
Memory limit: 1G

Author:
Problem types

"too many constructive problems... can we have a math problem instead??"
– nobody

You are given an N \times M grid containing each of the integers from 0 to NM-1 exactly once. Denote the integer in the i^{th} row and j^{th} column as a_{i,j}.

For any grid of integers, define its set of achievable MEX values to be all integers k for which there exists a subgrid of the grid whose MEX is equal to k.

Determine the number of N \times M grids containing each of the integers from 0 to NM-1 exactly once which have the same set of achievable MEX values as the given grid, modulo 10^9+7.

Note: MEX means minimum excluded value. The MEX of a set of numbers S refers to the smallest non-negative integer which is not contained in S.

Constraints

1 \le N,M \le 300
0 \le a_{i,j} \le NM-1

The given N \times M grid contains all integers from 0 to NM-1 exactly once.

Subtask 1 [20%]

N = 1

Subtask 2 [40%]

1 \le N,M \le 50

Subtask 3 [40%]

No additional constraints.

Input Specification

The first line contains two integers, N and M, the number of rows and the number of columns in the grid, respectively.

The next N lines each contain M integers, where the j^{th} integer on the i^{th} line is a_{i,j}, describing the given grid.

Output Specification

Output one line containing one integer, the number of N \times M grids containing each of the integers from 0 to NM-1 exactly once which have the same set of achievable MEX values as the given grid, modulo 10^9+7.

Sample Input 1

2 2
0 2
3 1

Sample Output 1

8

Explanation for Sample 1

The set of achievable MEX values of the given grid is \{0,1,4\}. A MEX of 0 is achievable by taking any subgrid which does not contain 0 (e.g. the subgrid consisting only of the value 1), a MEX of 1 is achievable by taking the subgrid consisting only of the value 0, and a MEX of 4 is achievable by taking the subgrid consisting of the entire grid.

There are 8 such 2\times 2 grids with the same set of achievable MEX values \{0,1,4\}, including the given grid. They are listed as follows:


\begin{bmatrix}
0 & 2 \\ 3 & 1
\end{bmatrix}
\begin{bmatrix}
0 & 3 \\ 2 & 1
\end{bmatrix}
\begin{bmatrix}
1 & 2 \\ 3 & 0
\end{bmatrix}
\begin{bmatrix}
1 & 3 \\ 2 & 0
\end{bmatrix}


\begin{bmatrix}
2 & 0 \\ 1 & 3
\end{bmatrix}
\begin{bmatrix}
2 & 1 \\ 0 & 3
\end{bmatrix}
\begin{bmatrix}
3 & 0 \\ 1 & 2
\end{bmatrix}
\begin{bmatrix}
3 & 1 \\ 0 & 2
\end{bmatrix}

Sample Input 2

3 4
8 10 11 9
7 6 3 4
5 0 2 1

Sample Output 2

261888

Explanation for Sample 2

The set of achievable MEX values of the given grid is \{0, 1, 3, 5, 8, 12\}. Let (r_1,c_1,r_2,c_2) denote the subgrid spanning rows r_1 to r_2 and columns c_1 to c_2, numbered from 1 to N and 1 to M, respectively. The following is a list of the achievable MEX values followed by one of its possible corresponding subgrids.

  • 0 : (1,1,1,1)
  • 1 : (3,4,3,4)
  • 3 : (3,1,3,4)
  • 5 : (2,2,3,4)
  • 8 : (2,1,3,4)
  • 12 : (1,1,3,4)

It can be shown that no other MEX values are achievable. It can also be shown that there are exactly 261888 grids with 3 rows and 4 columns containing all of the numbers from 0 to 11 exactly once which have the set of achievable MEX values \{0, 1, 3, 5, 8, 12\}, including the given grid.

Sample Input 3

1 20
0 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1

Sample Output 3

321822771

Explanation for Sample 3

Remember to output the answer modulo 10^9+7.


Comments

There are no comments at the moment.