#include <bits/stdc++.h>
using namespace std;
const int N = 2005, mod = 998244353;
int n, a[N][N];
long long ans, fac[N], f[N][N];
int read() {
  int ret = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = 0;
    c = getchar();
  }
  while (isdigit(c)) ret = ret * 10 + (c ^ 48), c = getchar();
  return f ? ret : -ret;
}
struct BIT {
  long long c[N];
  void clear() { memset(c, 0, sizeof(c)); }
  void update(int x, long long v) {
    for (; x < N; x += (x & (-x))) c[x] += v;
  }
  void upzero(int x) {
    long long t = Query(x, x);
    update(x, -t);
  }
  long long query(int x) {
    long long res = 0;
    for (; x; x -= (x & (-x))) res += c[x];
    return res;
  }
  long long Query(int l, int r) { return query(r) - query(l - 1); }
} bit1, bit2;
long long upm(long long x) { return x < 0 ? x + mod : x; }
void up(long long &x, long long y) {
  x += y;
  if (x >= mod) x -= mod;
}
int main() {
  n = read();
  fac[0] = 1;
  for (int i = 1; i <= n; ++i) fac[i] = fac[i - 1] * i % mod;
  for (int i = 0; i <= n; ++i) {
    f[i][0] = fac[i];
    for (int j = 1; j <= i; ++j) f[i][j] = upm(f[i][j - 1] - f[i - 1][j - 1]);
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) a[i][j] = read();
  for (int i = 1; i <= n; ++i) bit1.update(i, 1);
  for (int i = 1; i <= n; ++i) {
    up(ans, bit1.query(a[1][i] - 1) * fac[n - i] % mod);
    bit1.update(a[1][i], -1);
  }
  for (int i = 2; i <= n; ++i) {
    ans = ans * f[n][n] % mod;
    bit1.clear();
    bit2.clear();
    for (int j = 1; j <= n; ++j) bit1.update(j, 1), bit2.update(j, 1);
    for (int j = 1; j <= n; ++j) {
      bit2.upzero(a[i - 1][j]);
      int s1 = bit1.query(a[i][j] - 1), s2 = bit2.query(a[i][j] - 1);
      int s3 = bit2.query(n);
      if (a[i][j] > a[i - 1][j] && bit1.Query(a[i - 1][j], a[i - 1][j])) --s1;
      up(ans, (s1 - s2 + mod) * f[n - j][s3] % mod);
      if (s3 > 0) up(ans, s2 * f[n - j][s3 - 1] % mod);
      bit1.update(a[i][j], -1);
      bit2.upzero(a[i][j]);
    }
  }
  printf("%lld\n", upm(ans % mod));
  return 0;
}
