Counting rectangles by distinct numbers

Count rectangles of every size by how many distinct numbers they contain, then output a product of those counts modulo 1e9+7.

Hard8ImplementationBit manipulationPrefix sumBrute forceNo attempts yetTime limit3sMemory limit256 MB

Problem

There is an N×MN \times M grid. Each cell holds one number between 1 and KK. The cell in row ii and column jj (1iN1 \le i \le N, 1jM1 \le j \le M) is written as (i,j)(i, j).

A rectangle is fixed by four integers x1x_1, y1y_1, x2x_2, y2y_2 with 1x1x2N1 \le x_1 \le x_2 \le N and 1y1y2M1 \le y_1 \le y_2 \le M, and it consists of every cell (x,y)(x, y) with x1xx2x_1 \le x \le x_2 and y1yy2y_1 \le y \le y_2. Its size is (x2x1+1)×(y2y1+1)(x_2 - x_1 + 1) \times (y_2 - y_1 + 1). Two rectangles are different when the values [x1,y1,x2,y2][x_1, y_1, x_2, y_2] differ.

Let Ck,n,mC_{k,n,m} be the number of rectangles of size n×mn \times m in the grid that hold exactly kk kinds of numbers. Write a program that computes every Ck,n,mC_{k,n,m}.

For example, let N=M=K=3N = M = K = 3 and let the numbers be written as in the picture below.

picture of a 3 x 3 grid

With x1=2x_1 = 2, y1=1y_1 = 1, x2=3x_2 = 3, y2=2y_2 = 2, the four cells (2,1)(2, 1), (2,2)(2, 2), (3,1)(3, 1), (3,2)(3, 2) belong to one rectangle. Taken on its own, that rectangle looks like this.

picture of the extracted rectangle

Only 1 and 2 appear in it, so it is a rectangle with two kinds of numbers.

Input

The input consists of a single test case.

The first line contains three integers NN, MM, KK (1N,M5121 \le N, M \le 512, 1K91 \le K \le 9) separated by spaces.

Each of the next NN lines contains the MM numbers written in one row of the grid, with no spaces between them. Each number is between 1 and KK.

Output

Printing every Ck,n,mC_{k,n,m} takes too long, so print on the first line the following value modulo 1000000007.

k=1Kn=1Nm=1M(Ck,n,m+k×n×m)\prod_{k=1}^{K}\prod_{n=1}^{N}\prod_{m=1}^{M}\left(C_{k,n,m} + k \times n \times m\right)