#include <bits/stdc++.h>
using namespace std;
const int N = 2005, mod = 998244353;
int dp[N][N], a[N][N], t[2][N];
long long bp(long long a, long long p) {
  long long r = 1;
  while (p > 0) {
    if (p & 1) r = r * a % mod;
    a = a * a % mod;
    p >>= 1;
  }
  return r;
}
void upd(int y, int x, int d) {
  for (; x < N; x |= x + 1) t[y][x] += d;
}
int que(int y, int x) {
  int s = 0;
  for (; x >= 0; x = (x & (x + 1)) - 1) s += t[y][x];
  return s;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  dp[0][0] = 1;
  for (int i = 1; i < N; i++) {
    dp[i][0] = i * (long long)dp[i - 1][0] % mod;
    for (int j = 1; j <= i; j++) {
      dp[i][j] = dp[i][j - 1] - dp[i - 1][j - 1];
      if (dp[i][j] < 0) dp[i][j] += mod;
    }
  }
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
    }
  }
  long long ans = 0;
  bool used[N], w[N];
  fill(used, used + N, 0);
  for (int j = 0; j < n; j++) {
    for (int k = 1; k < a[0][j]; k++) {
      if (!used[k]) {
        ans = (ans + dp[n - j - 1][0]) % mod;
      }
    }
    used[a[0][j]] = 1;
  }
  ans = ans * bp(dp[n][n], n - 1) % mod;
  for (int i = 1; i < n; i++) {
    fill(used, used + N, 0);
    fill(w, w + N, 0);
    memset(t, 0, sizeof(t));
    long long cur = 0;
    int x = 0;
    for (int j = 0; j < n; j++) {
      w[a[i - 1][j]] = 1;
      if (!used[a[i - 1][j]]) {
        x++;
        upd(0, a[i - 1][j], 1);
      }
      long long p = que(0, a[i][j] - 1),
                q = a[i][j] - 1 - que(1, a[i][j] - 1) - p;
      if (a[i - 1][j] < a[i][j] && !used[a[i - 1][j]]) {
        if (w[a[i - 1][j]])
          p--;
        else
          q--;
      }
      if (n - j - x >= 0) {
        cur = (cur + dp[n - j - 1][n - j - x] * p) % mod;
      }
      if (n - j - 1 - x >= 0) {
        cur = (cur + dp[n - j - 1][n - j - 1 - x] * q) % mod;
      }
      used[a[i][j]] = 1;
      upd(1, a[i][j], 1);
      if (w[a[i][j]] == 1) {
        x--;
        upd(0, a[i][j], -1);
      }
    }
    ans = (ans + cur * bp(dp[n][n], n - 1 - i)) % mod;
  }
  ans %= mod;
  cout << ans;
  return 0;
}
