#include <bits/stdc++.h>
using namespace std;
const int N = 2005, p = 998244353;
int read() {
  int f = 1, g = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') f = -1;
  for (; isdigit(ch); ch = getchar()) g = g * 10 + ch - '0';
  return f * g;
}
int qpow(int x, int k) {
  int t = 1;
  for (; k; k >>= 1) {
    if (k & 1) t = (long long)t * x % p;
    x = (long long)x * x % p;
  }
  return t;
}
int ans, n, cnt, b[N], a[N][N], f[N], g[N][N];
void check(int &x, int y) { x = (x + y < p) ? (x + y) : (x + y - p); }
int sub(int x, int y) { return (x - y < 0) ? (x - y + p) : (x - y); }
struct BIT {
  int f[N];
  void reset() {
    for (int i = 1; i <= n; i++) f[i] = (i & (-i));
  }
  void add(int x) {
    for (; x <= n; x += (x & (-x))) f[x]++;
  }
  void del(int x) {
    for (; x <= n; x += (x & (-x))) f[x]--;
  }
  int que(int x) {
    int t = 0;
    for (; x; x -= (x & (-x))) t += f[x];
    return t;
  }
  int query(int l, int r) { return que(r) - que(l - 1); }
} h[2];
int main() {
  n = read();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) a[i][j] = read();
  f[0] = 1;
  f[1] = 0;
  for (int i = 2; i <= n; i++)
    f[i] = (long long)(i - 1) * (f[i - 1] + f[i - 2]) % p;
  g[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    g[i][0] = (long long)g[i - 1][0] * i % p;
    for (int j = 1; j <= i; j++) g[i][j] = sub(g[i][j - 1], g[i - 1][j - 1]);
  }
  for (int i = 1; i <= n; i++) {
    check(ans, (long long)g[n - i][0] * (a[1][i] - 1 - h[0].que(a[1][i])) % p);
    h[0].add(a[1][i]);
  }
  for (int i = 2; i <= n; i++) {
    ans = (long long)ans * f[n] % p;
    h[0].reset();
    h[1].reset();
    memset(b, 0, sizeof(b));
    cnt = n;
    for (int j = 1; j <= n; j++) {
      if (!b[a[i - 1][j]]++) {
        cnt--;
        h[1].del(a[i - 1][j]);
      }
      int tc = h[1].que(a[i][j] - 1), t = h[0].que(a[i][j] - 1) - tc;
      if ((a[i - 1][j] < a[i][j]) && (b[a[i - 1][j]] == 1)) t--;
      check(ans, (long long)t * g[n - j][cnt] % p);
      check(ans, (long long)tc * g[n - j][cnt - 1] % p);
      if (!b[a[i][j]]++) {
        cnt--;
        h[1].del(a[i][j]);
      }
      h[0].del(a[i][j]);
    }
  }
  printf("%d\n", ans);
  return 0;
}
