#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2005, MOD = 998244353;
int f[MAXN][MAXN], mat[MAXN][MAXN], g[MAXN], h[MAXN], fac[MAXN], n;
int cnt[MAXN], bit1[MAXN], bit2[MAXN];
void add(int *bit, int x) {
  for (; x <= n; x += x & -x) ++bit[x];
}
int sum(int *bit, int x) {
  int res = 0;
  for (; x; x -= x & -x) res += bit[x];
  return res;
}
int main() {
  scanf("%d", &n);
  g[0] = fac[0] = h[0] = f[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    f[i][i] = g[i] =
        (i == 1 ? 0 : (long long)(i - 1) * (g[i - 1] + g[i - 2])) % MOD;
    f[i][0] = fac[i] = (long long)fac[i - 1] * i % MOD;
    for (int j = 1; j < i; j++)
      f[i][j] =
          ((long long)j * f[i - 1][j - 1] + (long long)(i - j) * f[i - 1][j]) %
          MOD;
  }
  for (int i = 1; i <= n; i++) h[i] = (long long)h[i - 1] * g[n] % MOD;
  int res = 0;
  for (int j = 1; j <= n; j++) {
    scanf("%d", &mat[1][j]);
    res = ((long long)fac[n - j] * (mat[1][j] - sum(bit1, mat[1][j]) - 1) %
               MOD * h[n - 1] +
           res) %
          MOD;
    add(bit1, mat[1][j]);
  }
  for (int i = 2; i <= n; i++) {
    memset(cnt, 0, sizeof(cnt)), memset(bit1, 0, sizeof(bit1)),
        memset(bit2, 0, sizeof(bit2));
    int rep = n;
    for (int j = 1; j <= n; j++) {
      scanf("%d", &mat[i][j]);
      if (++cnt[mat[i - 1][j]] == 1) add(bit2, mat[i - 1][j]), --rep;
      int a = mat[i][j] - 1 - sum(bit2, mat[i][j] - 1),
          b = mat[i][j] - 1 - sum(bit1, mat[i][j] - 1) - a;
      if (cnt[mat[i - 1][j]] == 1 && mat[i - 1][j] < mat[i][j]) --b;
      res = (((rep ? (long long)f[n - j][rep - 1] * a : 0) +
              (long long)f[n - j][rep] * b) %
                 MOD * h[n - i] +
             res) %
            MOD;
      if (++cnt[mat[i][j]] == 1) add(bit2, mat[i][j]), --rep;
      add(bit1, mat[i][j]);
    }
  }
  printf("%d\n", res);
  return 0;
}
