#include <algorithm>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
const int N = 5;
const ll MOD = 1e9 + 7;
int main() {
vector<int> init(N * N);
for (int i = 0; i < N * N; i++) cin >> init[i], --init[i];
vector<int> num(N * N, -1), id(N * N, -1), ps;
for (int i = 0; i < N * N; i++) {
int n = init[i];
if (n < 0) {
id[i] = ps.size();
ps.push_back(i);
} else {
num[n] = i;
}
}
auto at = [&](int mask, int n, int i) {
if (id[i] >= 0) return ((mask >> id[i]) & 1) == 1;
return init[i] < n;
};
auto check = [&](int mask, int n, int i) {
int y = i / N, x = i % N;
if (0 < y && y < N - 1) {
if (at(mask, n, (y - 1) * N + x) ^ at(mask, n, (y + 1) * N + x)) {
return false;
}
}
if (0 < x && x < N - 1) {
if (at(mask, n, y * N + (x - 1)) ^ at(mask, n, y * N + (x + 1))) {
return false;
}
}
return true;
};
int P = ps.size();
vector<ll> dp(1 << P, 0);
dp[0] = 1;
for (int n = 0; n < N * N; n++) {
int frees = 0;
for (int m = 0; m < n; m++)
if (num[m] < 0) frees++;
vector<ll> ndp(1 << P, 0);
for (int mask = 0; mask < (1 << P); mask++) {
if (__builtin_popcount(mask) != frees) continue;
if (num[n] >= 0) {
if (check(mask, n, num[n])) {
(ndp[mask] += dp[mask]) %= MOD;
}
} else {
for (int j = 0; j < P; j++) {
if ((mask >> j) & 1) continue;
if (check(mask, n, ps[j])) {
(ndp[mask | (1 << j)] += dp[mask]) %= MOD;
}
}
}
}
dp = ndp;
}
cout << dp.back() << endl;
return 0;
}