#include <bits/stdc++.h>
using namespace std;
int mp[2010][2010], f[2010][2010], bit[2][2010], n, mi[2010], used[2010],
    h[2010];
int lowbit(int x) { return x - (x & x - 1); }
void modify(int c, int x, int y) {
  while (x <= n) {
    bit[c][x] += y;
    x += lowbit(x);
  }
}
int query(int c, int x) {
  int res = 0;
  while (x) {
    res += bit[c][x];
    x -= lowbit(x);
  }
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) scanf("%d", &mp[i][j]);
  f[0][0] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= i; j++)
      if (j == 0)
        f[i][j] = (long long)i * f[i - 1][j] % 998244353;
      else {
        f[i][j] = ((long long)i - j) * f[i - 1][j - 1] % 998244353;
        if (j >= 2)
          f[i][j] =
              (f[i][j] + ((long long)j - 1) * f[i - 1][j - 2] % 998244353) %
              998244353;
      }
  int res = 0;
  mi[0] = 1;
  for (int i = 1; i <= n; i++)
    mi[i] = (long long)mi[i - 1] * f[n][n] % 998244353;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      used[j] = h[j] = 0;
      modify(0, mp[i][j], 1);
      if (i != 1) modify(1, mp[i - 1][j], 1);
    }
    int rem = n;
    for (int j = 1; j <= n; j++) {
      modify(0, mp[i][j], -1);
      h[mp[i][j]] = 1;
      if (i != 1) {
        if (used[mp[i - 1][j]] == 0) {
          used[mp[i - 1][j]] = 1;
          rem--;
          modify(1, mp[i - 1][j], -1);
        }
      }
      int t0 = query(0, mp[i][j] - 1), t1 = query(1, mp[i][j] - 1);
      if (i == 1)
        res = (res + (long long)mi[n - 1] * f[n - j][0] % 998244353 * t0 %
                         998244353) %
              998244353;
      else {
        if (mp[i - 1][j] < mp[i][j] && h[mp[i - 1][j]] == 0) t0--;
        int tmp = ((long long)t0 - t1) * f[n - j][rem] % 998244353;
        if (t1)
          tmp =
              (tmp + (long long)t1 * f[n - j][rem - 1] % 998244353) % 998244353;
        res = (res + (long long)tmp * mi[n - i] % 998244353) % 998244353;
      }
      if (i != 1)
        if (used[mp[i][j]] == 0) {
          used[mp[i][j]] = 1;
          rem--;
          modify(1, mp[i][j], -1);
        }
    }
  }
  printf("%d\n", res);
  return 0;
}
