#include <bits/stdc++.h>
using namespace std;
const int N = 2010, mod = 998244353;
int sm[2][N], n, f[N][N], a[N][N], ans;
void add(int x, int y, int z) {
  for (; x <= n; x += x & -x) sm[z][x] += y;
}
int query(int x, int z) {
  int ret = 0;
  for (; x; x -= x & -x) ret += sm[z][x];
  return ret;
}
void Init() {
  f[0][0] = 1;
  for (int i = 1; i <= n; ++i) {
    f[i][0] = (long long)f[i - 1][0] * i % mod;
    for (int j = 1; j <= i; ++j)
      f[i][j] = (f[i][j - 1] - f[i - 1][j - 1] + mod) % mod;
  }
}
int p[N], q[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  Init();
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) cin >> a[i][j];
  for (int i = 1; i <= n; ++i) {
    int les = n;
    ans = (long long)ans * f[n][n] % mod;
    memset(sm, 0, sizeof sm);
    memset(p, 0, sizeof p);
    memset(q, 0, sizeof q);
    for (int j = 1; j <= n; ++j) {
      int u = query(a[i][j] - 1, 0);
      p[a[i - 1][j]] = 1;
      if (q[a[i - 1][j]])
        ++les;
      else if (a[i - 1][j])
        add(a[i - 1][j], 1, 0), add(a[i - 1][j], 1, 1);
      int v = a[i][j] - 1 - query(a[i][j] - 1, 1);
      les -= 2;
      if (i == 1)
        ans = (ans + (long long)v * f[n - j][0]) % mod;
      else
        ans = (ans + (long long)u * f[n - j][les + 1] +
               (long long)v * f[n - j][les]) %
              mod;
      q[a[i][j]] = 1;
      if (p[a[i][j]])
        add(a[i][j], -1, 0), ++les;
      else
        add(a[i][j], 1, 1);
    }
  }
  cout << ans << '\n';
  return 0;
}
