#include <bits/stdc++.h>
using namespace std;
int a[2005][2005], f[2005][2005], n, fac[2005], ans, g[2005], c[2005], s[2005],
    pf[2005], vis1[2005], vis2[2005];
void fix(int *a, int x, int op) {
  for (; x <= n; x += x & -x) a[x] += op;
}
int find(int *a, int x) {
  int ret = 0;
  for (; x; x -= x & -x) ret += a[x];
  return ret;
}
void init() {
  memset(c, 0, sizeof(c));
  memset(s, 0, sizeof(s));
}
int main() {
  scanf("%d", &n);
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]);
  g[1] = 0, g[2] = 1;
  for (int i = 1; i <= n; i++) fac[i] = (long long)fac[i - 1] * i % 998244353;
  for (int i = 3; i <= n; i++)
    g[i] = (long long)(g[i - 1] + g[i - 2]) * (i - 1) % 998244353;
  for (int i = 1; i <= n; i++) {
    f[i][0] = fac[i];
    for (int j = 1; j < i; j++)
      f[i][j] =
          ((long long)(i - j) * f[i - 1][j] + (long long)j * f[i - 1][j - 1]) %
          998244353;
    f[i][i] = g[i];
  }
  pf[0] = 1;
  for (int i = 1; i <= n; i++) pf[i] = (long long)pf[i - 1] * g[n] % 998244353;
  init();
  for (int i = n; i; i--) {
    fix(c, a[1][i], 1);
    ans = (ans + (long long)find(c, a[1][i] - 1) * fac[n - i] % 998244353 *
                     pf[n - 1]) %
          998244353;
  }
  for (int i = 2; i <= n; i++) {
    init();
    memset(vis1, 0, sizeof(vis1));
    memset(vis2, 0, sizeof(vis2));
    for (int j = n; j; j--) {
      vis2[a[i][j]] = 1;
      if (!vis1[a[i][j]])
        fix(c, a[i][j], 1);
      else
        fix(s, a[i][j], 1);
      int cnt = find(s, n);
      ans = (ans + (long long)find(c, a[i][j] - 1) * f[n - j][cnt] % 998244353 *
                       pf[n - i]) %
            998244353;
      if (cnt)
        ans = (ans + (long long)find(s, a[i][j] - 1) * f[n - j][cnt - 1] %
                         998244353 * pf[n - i]) %
              998244353;
      if (a[i][j] > a[i - 1][j] && vis2[a[i - 1][j]])
        ans = (ans - (long long)f[n - j][cnt] * pf[n - i]) % 998244353;
      if (!vis2[a[i - 1][j]])
        vis1[a[i - 1][j]] = 1;
      else {
        vis1[a[i - 1][j]] = 1;
        fix(c, a[i - 1][j], -1);
        fix(s, a[i - 1][j], 1);
      }
    }
  }
  printf("%d\n", (ans + 998244353) % 998244353);
}
