Distinct Paths

You have a rectangular n × m-cell board. Some cells are already painted some of k colors. You need to paint each uncolored cell one of the k colors so that any path from the upper left square to the lower right one doesn’t contain any two cells of the same color. The path can go only along side-adjacent cells and can only go down or right.

Print the number of possible paintings modulo 1000000007 (109 + 7).

Input

The first line contains three integers n, m, k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 10). The next n lines contain m integers each — the board. The first of them contains m uppermost cells of the board from the left to the right and the second one contains m cells from the second uppermost row and so on. If a number in a line equals 0, then the corresponding cell isn’t painted. Otherwise, this number represents the initial color of the board cell — an integer from 1 to k.

Consider all colors numbered from 1 to k in some manner.

Output

Print the number of possible paintings modulo 1000000007 (109 + 7).

Examples

input

2 2 4
0 0
0 0

output

48

input

2 2 4
1 2
2 1

output

0

input

5 6 10
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

output

3628800

input

2 6 10
1 2 3 4 5 6
0 0 0 0 0 0

output

4096

Solution:

#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

const int md = 1000000007;

int n, m, k;
int ans = 0;
int a[42][42];
int bad[42][42][42];
int res[42][42];
int mp[42];

void go(int row, int col, int mc) {
if (row == n) {
for (int c=1;c<=mc;c++)
      for (int qc=c+1;qc<=mc;qc++)
        if (mp != 0 && mp[qc] != 0 && mp == mp[qc]) return;
    int free = 0, cols = k;
    for (int c=1;c<=mc;c++)
      if (mp == 0) free++;
      else cols--;
    int cur = 1;
    for (int c=1;c<=free;c++) {
      cur *= cols;
      cols--;
    }
    ans += cur;
    if (ans >= md) ans -= md;
return;
}
for (int c=1;c<=k && c<=mc+1;c++) {
    if (bad[row][col]) continue;
    for (int x=row;x<n;x++)
      for (int y=col;y<m;y++) bad[x][y]++;
    res[row][col] = c;
    int ok = 1;
    if (a[row][col] != 0)
      if (mp != 0 && mp != a[row][col]) ok = 0;
    if (ok) {
      bool flag = false;
      if (mp == 0) {
        mp = a[row][col];
        flag = true;
      }
      int nmc = (c > mc ? c : mc);
if (col < m-1) go(row, col+1, nmc);
      else go(row+1, 0, nmc);
      if (flag) mp = 0;
    }
    for (int x=row;x<n;x++)
      for (int y=col;y<m;y++) bad[x][y]--;
  }
}

int main() {
  scanf("%d %d %d", &n, &m, &k);
  if (n+m-1 > k) {
printf("%d\n", 0);
return 0;
}
for (int i=0;i<n;i++)
    for (int j=0;j<m;j++) scanf("%d", &a[i][j]);
  for (int i=0;i<n;i++)
    for (int j=0;j<m;j++)
      for (int q=1;q<=k;q++) bad[i][j][q] = 0;
  for (int q=1;q<=k;q++) mp[q] = 0;
  go(0, 0, 0);
  printf("%d\n", ans);
  return 0;
}