#include <bits/stdc++.h>
using namespace std;
long long f[2536][2536];
void preprocess() {
  f[0][0] = 1;
  for (long long i = 1; i < 2536; i++) f[i][0] = f[i - 1][0] * i % 998244353;
  for (long long i = 1; i < 2536; i++)
    for (long long j = 1; j <= i; j++) {
      if (j >= 2)
        f[i][j] = f[i - 1][j - 2] * (j - 1) + f[i - 1][j - 1] * (i - j);
      else
        f[i][j] = f[i - 1][j - 1] * (i - j);
      f[i][j] %= 998244353;
    }
}
long long n;
long long mat[2536][2536];
long long qpow(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1) res = res * x % 998244353;
    x = x * x % 998244353;
    y = y / 2;
  }
  return res;
}
long long ea[2536], eb[2536];
struct ihatefenwicktree {
  long long bit[2536];
  void clear() { memset(bit, 0, sizeof(bit)); }
  void upd(long long x, long long y) {
    while (x < 2536) {
      bit[x] += y;
      x += (x & -x);
    }
  }
  long long ask(long long x) {
    long long y = 0;
    while (x) {
      y += bit[x];
      x -= (x & -x);
    }
    return y;
  }
} b1, b2;
int main() {
  preprocess();
  scanf("%lld", &n);
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    long long kth = 0;
    long long lim = n;
    memset(ea, 0, sizeof(ea));
    memset(eb, 0, sizeof(eb));
    for (long long j = 1; j <= n; j++) scanf("%lld", &mat[i][j]);
    long long tag = 1;
    b1.clear();
    b2.clear();
    for (long long j = 1; j <= n; j++) {
      long long lst = mat[i - 1][j], now = mat[i][j];
      long long dec = 0;
      if (i == 1) {
        b1.upd(now, 1);
        kth += (now - 1 - b1.ask(now - 1)) * f[n - j][0];
        kth %= 998244353;
        continue;
      }
      long long sss = b2.ask(now - 1);
      long long tot = now - 1 - b1.ask(now - 1) - (lst < now and eb[lst] == 0);
      tot -= sss;
      kth += sss * f[n - j][lim - (eb[lst] == 0)];
      kth += tot * f[n - j][lim - 1 - (eb[lst] == 0)];
      kth %= 998244353;
      if (eb[lst] == 0) b2.upd(lst, 1), lim--;
      if (ea[now] == 0)
        lim--;
      else
        b2.upd(now, -1);
      ea[lst] = eb[now] = 1;
      b1.upd(now, 1);
    }
    ans += 1ll * (kth)*qpow(f[n][n], n - i);
    ans %= 998244353;
  }
  printf("%lld\n", ans);
  return 0;
}
