#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 10;
const int mod = 998244353;
int n, f[maxn][maxn], s[maxn][maxn];
int fac[maxn], pw[maxn], t[maxn], tt[maxn];
bool v[maxn], vv[maxn];
int read(int x = 0, bool f = 0, char ch = getchar()) {
  for (; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-';
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 3) + (x << 1) + (ch & 15);
  return f ? -x : x;
}
void Add(int x, int v, int *t) {
  for (; x <= n; x += (x & -x)) t[x] += v;
}
int Ask(int x, int *t, int ret = 0) {
  for (; x; x -= (x & -x)) ret += t[x];
  return ret;
}
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      s[i][j] = read();
    }
  }
  f[0][0] = 1;
  f[1][0] = 0;
  f[2][0] = 1;
  for (int i = 3; i <= n; ++i)
    f[i][0] = (long long)(i - 1) * (f[i - 1][0] + f[i - 2][0]) % mod;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= i; ++j) {
      f[i][j] = (f[i][j - 1] + f[i - 1][j - 1]) % mod;
    }
  }
  pw[0] = fac[0] = 1;
  for (int i = 1; i <= n; ++i) pw[i] = (long long)pw[i - 1] * f[n][0] % mod;
  for (int i = 1; i <= n; ++i) fac[i] = (long long)fac[i - 1] * i % mod;
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    int x = s[1][i];
    (ans += (long long)(x - 1 - Ask(x - 1, t)) * fac[n - i] % mod) %= mod;
    Add(x, 1, t);
  }
  ans = (long long)ans * pw[n - 1] % mod;
  for (int i = 2; i <= n; ++i) {
    int tot = 0;
    memset(t, 0, sizeof(t));
    memset(v, 0, sizeof(v));
    memset(tt, 0, sizeof(tt));
    memset(vv, 0, sizeof(vv));
    for (int j = 1; j <= n; ++j) {
      int x = s[i][j], lx = s[i - 1][j];
      if (!vv[lx]) vv[lx] = 1, Add(lx, 1, tt);
      int c1 = n - Ask(n, tt);
      int c2 = x - 1 - Ask(x - 1, t);
      int c3 = x - 1 - Ask(x - 1, tt);
      if (!v[lx] && lx <= x - 1) --c2;
      (tot += (long long)c3 * f[n - j][n - j - (c1 - 1)] % mod) %= mod;
      (tot += (long long)(c2 - c3) * f[n - j][n - j - c1] % mod) %= mod;
      if (!v[x]) v[x] = 1, Add(x, 1, t);
      if (!vv[x]) vv[x] = 1, Add(x, 1, tt);
    }
    (ans += (long long)tot * pw[n - i] % mod) %= mod;
  }
  printf("%d\n", ans);
  return 0;
}
