#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int f[2020][2020];
int a[2020][2020];
int sum[2][2020];
bool ap1[2020], ap2[2020];
int fp[2020];
int n;
void add(int tp, int x, int v) {
  for (int i = x; i <= n; i += (i & (-i))) sum[tp][i] += v;
}
int query(int tp, int x) {
  int ret = 0;
  for (int i = x; i; i -= (i & (-i))) ret += sum[tp][i];
  return ret;
}
int main() {
  int ans = 0, cnt0, cnt1, cnt;
  scanf("%d", &n);
  memset(f, 0, sizeof(f));
  f[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    f[i][0] = (long long)f[i - 1][0] * i % mod;
    f[i][1] = (long long)f[i - 1][0] * (i - 1) % mod;
    for (int j = 2; j <= i; j++)
      f[i][j] = ((long long)f[i - 1][j - 2] * (j - 1) +
                 (long long)f[i - 1][j - 1] * (i - j)) %
                mod;
  }
  fp[0] = 1;
  for (int i = 1; i <= n; i++) fp[i] = (long long)fp[i - 1] * f[n][n] % mod;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]);
  memset(ap1, false, sizeof(ap1));
  for (int i = 1; i <= n; i++) {
    if (i == 1) {
      for (int j = 1; j <= n; j++) {
        ap1[j] = true;
        ap2[j] = false;
        add(1, j, 1);
      }
      cnt = 0;
    } else {
      for (int j = 1; j <= n; j++) {
        ap1[j] = false;
        ap2[j] = false;
        add(0, j, 1);
        add(1, j, 1);
      }
      cnt = n;
    }
    for (int j = 1; j <= n; j++) {
      if (i > 1) {
        ap1[a[i - 1][j]] = true;
        if (!ap2[a[i - 1][j]]) {
          add(0, a[i - 1][j], -1);
          cnt--;
        }
      }
      cnt0 = query(0, a[i][j] - 1);
      cnt1 = query(1, a[i][j] - 1) - cnt0;
      if (i > 1 && !ap2[a[i - 1][j]] && a[i][j] > a[i - 1][j]) cnt1--;
      if (cnt0)
        ans =
            ((long long)cnt0 * f[n - j][cnt - 1] % mod * fp[n - i] + ans) % mod;
      ans = ((long long)cnt1 * f[n - j][cnt] % mod * fp[n - i] + ans) % mod;
      ap2[a[i][j]] = true;
      add(1, a[i][j], -1);
      if (!ap1[a[i][j]]) {
        add(0, a[i][j], -1);
        cnt--;
      }
    }
  }
  printf("%d", ans);
  return 0;
}
