#include <bits/stdc++.h>
using namespace std;
const long long mod = (119 << 23) + 1, root = 3;
long long modpow(long long a, long long e) {
  if (e == 0) return 1;
  long long x = modpow(a * a % mod, e >> 1);
  return e & 1 ? x * a % mod : x;
}
const long long MOD = mod;
long long dp[2100][2100];
long long ncr[2100][2100];
long long fact[2100];
void init() {
  for (int n = 0; n <= 2000; n++) {
    for (int k = 0; k <= 2000; k++) {
      if (k == 0 || k == n) {
        ncr[n][k] = 1;
      } else if (k > n) {
        ncr[n][k] = 0;
      } else {
        ncr[n][k] = (ncr[n - 1][k] + ncr[n - 1][k - 1]) % MOD;
      }
    }
  }
  fact[0] = 1;
  for (int i = 1; i <= 2000; i++) {
    fact[i] = (i * fact[i - 1]) % MOD;
  }
  for (int n = 0; n <= 2000; n++) {
    for (int k = 0; k <= n; k++) {
      if (k == 0) {
        dp[n][k] = fact[n];
      } else {
        if (k >= 2) {
          dp[n][k] += (k - 1) * dp[n - 1][k - 2];
          dp[n][k] %= MOD;
        }
        dp[n][k] += (n - k) * dp[n - 1][k - 1];
        dp[n][k] %= MOD;
      }
    }
  }
}
int perm[2100][2100];
int BIT0[2100];
void upd0(int a, int v) {
  while (a < 2100) {
    BIT0[a] += v;
    a += a & -a;
  }
}
int q0(int a) {
  int v = 0;
  while (a > 0) {
    v += BIT0[a];
    a -= a & -a;
  }
  return v;
}
int BIT1[2100];
void upd1(int a, int v) {
  while (a <= 2000) {
    BIT1[a] += v;
    a += a & -a;
  }
}
int q1(int a) {
  int v = 0;
  while (a > 0) {
    v += BIT1[a];
    a -= a & -a;
  }
  return v;
}
bool occ1[2100];
bool occ2[2100];
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  init();
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> perm[i][j];
      perm[i][j]--;
    }
  }
  long long ans = 0;
  {
    long long mult0 = modpow(dp[n][n], n - 1);
    for (int j = 1; j <= n; j++) {
      upd0(j, 1);
    }
    long long r0 = 0;
    for (int j = 0; j < n; j++) {
      upd0(perm[0][j] + 1, -1);
      long long nless = q0(perm[0][j] + 1);
      r0 = (r0 + nless * fact[n - 1 - j]) % MOD;
    }
    ans = (ans + r0 * mult0) % MOD;
  }
  for (int a = 1; a < n; a++) {
    long long mult = modpow(dp[n][n], n - 1 - a);
    for (int i = 0; i <= n; i++) {
    }
    int shared = 0;
    long long cans = 0;
    for (int b = n - 1; b >= 0; b--) {
      occ2[perm[a][b]] = 1;
      if (occ1[perm[a][b]]) {
        shared++;
        upd1(perm[a][b] + 1, 1);
      }
      upd0(perm[a][b] + 1, 1);
      int tot = q0(perm[a][b]);
      int sh = q1(perm[a][b]);
      tot -= sh;
      if (perm[a - 1][b] < perm[a][b] && occ2[perm[a - 1][b]]) {
        tot--;
      }
      if (shared) {
        cans += sh * dp[n - 1 - b][shared - 1];
      }
      cans += tot * dp[n - 1 - b][shared];
      cans %= MOD;
      occ1[perm[a - 1][b]] = 1;
      if (occ2[perm[a - 1][b]]) {
        shared++;
        upd1(perm[a - 1][b] + 1, 1);
      }
    }
    ans = (ans + cans * mult) % MOD;
    for (int b = 0; b <= 2050; b++) {
      occ1[b] = occ2[b] = BIT0[b] = BIT1[b] = 0;
    }
  }
  ans %= MOD;
  if (ans < 0) ans += MOD;
  cout << ans << endl;
}
