#include <bits/stdc++.h>
const int mod = 998244353;
using ll = long long;
int n, p[2005][2005], pw[2005], fac[2005], ifac[2005], f[2005][2005],
    vis2[2005], vis[2005], perm, ans;
struct SupremeCalamitas {
  int sum[2005] = {0};
  void add(int x, int y) {
    assert(x > 0);
    while (x <= n) {
      sum[x] += y;
      x += x & -x;
    }
  }
  int query(int x) {
    int ans = 0;
    while (x) {
      ans += sum[x];
      x -= x & -x;
    }
    return ans;
  }
} A, B;
int det(int x) { return x & 1 ? mod - 1 : 1; }
int binom(int n, int m) {
  if (n < 0 || m < 0 || n < m) return 0;
  return (ll)fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}
int main() {
  scanf("%d", &n);
  fac[0] = ifac[0] = ifac[1] = 1;
  for (int i = 1; i <= n; ++i) fac[i] = (ll)fac[i - 1] * i % mod;
  for (int i = 2; i <= n; ++i)
    ifac[i] = (ll)(mod - mod / i) * ifac[mod % i] % mod;
  for (int i = 1; i <= n; ++i) ifac[i] = (ll)ifac[i - 1] * ifac[i] % mod;
  f[0][0] = 1;
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j <= i; ++j)
      f[i][0] =
          (f[i][0] + (ll)binom(i, j) * det(j) % mod * fac[i - j] % mod) % mod;
  for (int i = 0; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      f[i][j] = ((ll)f[i][j - 1] * j % mod +
                 (i > 0 ? (ll)f[i - 1][j] * i % mod : 0)) %
                mod;
  perm = f[n][0];
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) scanf("%d", &p[i][j]);
  pw[0] = 1;
  for (int i = 1; i <= n; ++i) pw[i] = (ll)pw[i - 1] * perm % mod;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      B.add(j, 1);
      vis[j] = vis2[j] = 1;
      if (i > 1) A.add(j, 1);
    }
    for (int j = 1; j <= n; ++j) {
      int x =
          A.query(p[i][j] - 1) - (p[i - 1][j] < p[i][j] && vis[p[i - 1][j]]);
      int y = B.query(p[i][j] - 1) -
              (i > 1 && p[i - 1][j] < p[i][j] && (vis2[p[i - 1][j]])) - x;
      int z = A.query(n);
      int w = n - j + 1 - z;
      assert(x >= 0 & y >= 0 && z >= 0 && w >= 0);
      int d = vis[p[i - 1][j]];
      if (x)
        ans = (ans + (ll)pw[n - i] * x % mod * f[z - 1 - d][w + d] % mod) % mod;
      if (y)
        ans = (ans + (ll)pw[n - i] * y % mod * f[z - d][w - 1 + d] % mod) % mod;
      B.add(p[i][j], -1);
      vis2[p[i][j]] = 0;
      if (i > 1 && vis[p[i - 1][j]]) {
        vis[p[i - 1][j]] = 0;
        A.add(p[i - 1][j], -1);
      }
      if (i > 1 && vis[p[i][j]]) {
        vis[p[i][j]] = 0;
        A.add(p[i][j], -1);
      }
    }
  }
  printf("%d", ans);
  return 0;
}
