# 洛谷OJ: P1373 小a和uim之大逃离

1.dp[i][j][l][0] = dp[i][j][l][0] + dp[i-1][j][(l-maps[i][j]+k)%k][1] + dp[i][j-1][(l-maps[i][j]+k)%k][1]

2.dp[i][j][l][1] = dp[i][j][l][1] + dp[i-1][j][(l+maps[i][j])%k][0] + dp[i][j-1][(l+maps[i][j])%k][0]

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;

const int maxn = 800+2, MOD = 1e9+7;
int n, m, k, dp[maxn][maxn][17][2], maps[maxn][maxn];
long long ans;

int main() {
ios::sync_with_stdio(false);
/**************input***************/
cin >> n >> m >> k; k++;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> maps[i][j];
maps[i][j] %= k;
dp[i][j][maps[i][j]][0]++;
}
}
/**********************************/
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(i == 1 && j == 1) continue;
for(int l = 0; l <= k; l++) {
dp[i][j][l][0] += dp[i-1][j][(l-maps[i][j]+k)%k][1] % MOD + dp[i][j-1][(l-maps[i][j]+k)%k][1] % MOD;
dp[i][j][l][0] %= MOD;
dp[i][j][l][1] += dp[i-1][j][(l+maps[i][j])%k][0] % MOD + dp[i][j-1][(l+maps[i][j])%k][0] % MOD;
dp[i][j][l][1] %= MOD;
}
}
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
ans += dp[i][j][0][1];
ans %= MOD;
}
}
cout << ans << endl;
return 0;
}