#include <bits/stdc++.h>
using namespace std;
int n, a[2005][2005];
long long F[2005] = {1};
long long f[2005] = {1}, g[2005][2005];
int T1[2005], T2[2005];
void init() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) scanf("%d", a[i] + j);
  for (int i = 1; i <= n; ++i) F[i] = F[i - 1] * i % 998244353;
  for (int i = 2; i <= n; ++i)
    f[i] = (i - 1) * (f[i - 1] + f[i - 2]) % 998244353;
  for (int i = 0; i <= n; ++i) {
    g[i][0] = F[i];
    for (int j = 1; j <= i; ++j)
      g[i][j] = (g[i][j - 1] - g[i - 1][j - 1] + 998244353) % 998244353;
  }
}
long long pw(long long a, long long k) {
  long long ans = 1;
  for (; k; k >>= 1) {
    if (k & 1) ans = ans * a % 998244353;
    a = a * a % 998244353;
  }
  return ans;
}
int lowbit(int i) { return i & (-i); }
void change(int T[], int i, int d) {
  for (; i <= n; i += lowbit(i)) T[i] += d;
}
int query(int T[], int i) {
  if (i <= 0) return 0;
  int ans = 0;
  for (; i; i -= lowbit(i)) ans += T[i];
  return ans;
}
void solve() {
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    long long t = pw(f[n], n - i), c = (i == 1 ? 0 : n);
    for (int j = 1; j <= n; ++j)
      T1[j] = lowbit(j), T2[j] = (i == 1 ? 0 : lowbit(j));
    for (int j = 1; j <= n; ++j) {
      int f = query(T1, a[i - 1][j]) - query(T1, a[i - 1][j] - 1);
      int p = query(T2, a[i][j] - 1), q = query(T1, a[i][j] - 1) - p;
      if (f) {
        if (a[i - 1][j] < a[i][j]) {
          ans +=
              ((p - 1) * g[n - j][c - 2] + q * g[n - j][c - 1]) % 998244353 * t;
        } else {
          ans += (p * g[n - j][c - 2] + q * g[n - j][c - 1]) % 998244353 * t;
        }
        --c;
        change(T2, a[i - 1][j], -1);
      } else {
        ans += (p * g[n - j][c - 1] + q * g[n - j][c]) % 998244353 * t;
      }
      if (query(T2, a[i][j]) - query(T2, a[i][j] - 1))
        --c, change(T2, a[i][j], -1);
      change(T1, a[i][j], -1);
      ans %= 998244353;
    }
  }
  printf("%lld\n", ans);
}
int main() {
  init();
  solve();
  return 0;
}
