#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
void qread(int &x) {
  int neg = 1;
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') neg = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = 10 * x + c - '0', c = getchar();
  x *= neg;
}
const int maxn = 2005;
const int mod = 998244353;
int n, ans;
int a[maxn][maxn], s[maxn], id[maxn][maxn], sub[maxn], cnt[maxn];
int pw[maxn], f[maxn][maxn], fac[maxn];
bool occ[maxn];
class Fenwick {
 public:
  int t[maxn];
  void init() {
    for (int(i) = 1; (i) <= n; i++) t[i] = 0;
  }
  void add(int v, int val) {
    for (; v <= n; v += (v & -v)) t[v] += val;
  }
  int sum(int v) {
    int res = 0;
    for (; v; v -= (v & -v)) res += t[v];
    return res;
  }
} T1, T2;
void precalc() {
  fac[0] = 1;
  for (int(i) = 1; (i) <= n; i++) {
    fac[i] = 1LL * fac[i - 1] * i % mod;
    f[i][1] = (fac[i] - fac[i - 1] + mod) % mod;
    f[i][0] = fac[i];
  }
  f[0][0] = 1;
  for (int(i) = 1; (i) <= n; i++)
    for (int j = 2; j <= i; j++) {
      f[i][j] = (1LL * (j - 1) * f[i - 1][j - 2] % mod +
                 1LL * (i - j) * f[i - 1][j - 1] % mod) %
                mod;
    }
  pw[0] = 1;
  for (int(i) = 1; (i) <= n; i++) pw[i] = 1LL * pw[i - 1] * f[n][n] % mod;
}
int main() {
  scanf("%d", &n);
  precalc();
  for (int(i) = 1; (i) <= n; i++)
    for (int(j) = 1; (j) <= n; j++) scanf("%d", &a[i][j]), id[i][a[i][j]] = j;
  for (int(i) = 1; (i) <= n; i++) {
    for (int(j) = 0; (j) < n + 1; j++)
      occ[j] = false, s[j] = 0, cnt[j] = 1 + (i > 1);
    T1.init();
    T2.init();
    if (i > 1)
      for (int(j) = 1; (j) <= n; j++) T2.add(j, 1);
    if (i > 1) {
      for (int(j) = 1; (j) <= n; j++) s[min(id[i - 1][j], id[i][j]) - 1]++;
    }
    for (int j = n; j >= 1; j--) s[j] += s[j + 1];
    for (int(j) = 1; (j) <= n; j++) {
      int cur = a[i][j] - 1 - T1.sum(a[i][j] - 1);
      if (i > 1 && !occ[a[i - 1][j]] && a[i - 1][j] < a[i][j]) cur--;
      int tmp = s[j];
      if (i > 1 && id[i - 1][a[i][j]] > j) tmp++;
      cnt[a[i][j]]--;
      if (cnt[a[i][j]] == 1) T2.add(a[i][j], -1);
      if (i > 1) {
        cnt[a[i - 1][j]]--;
        if (cnt[a[i - 1][j]] == 1) T2.add(a[i - 1][j], -1);
      }
      int tot = T2.sum(a[i][j] - 1);
      ans = (ans + 1LL * (cur - tot) * pw[n - i] % mod * f[n - j][tmp] % mod) %
            mod;
      if (tot)
        ans =
            (ans + 1LL * tot * pw[n - i] % mod * f[n - j][tmp - 1] % mod) % mod;
      occ[a[i][j]] = true;
      T1.add(a[i][j], 1);
    }
  }
  printf("%d\n", ans);
  return 0;
}
