#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2,no-stack-protector,unroll-loops,fast-math")
const long long maxn = 2e3 + 10, maxm = 32e3 + 10, lg = 7, mod = 998244353,
                inf = 1e18;
long long n, a[maxn][maxn], dp[maxn][maxn], fac[maxn], tp[maxn];
bool rr[maxn], rg[maxn];
long long bit[maxn][2];
void add(long long i, long long x, bool b) {
  for (i += 5; i < maxn; i += (i & -i)) bit[i][b] += x;
}
long long get(long long i, bool b) {
  long long ret = 0;
  for (i += 5; i; i -= (i & -i)) ret += bit[i][b];
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  fac[0] = 1;
  for (int i = 1; i < maxn; i++) fac[i] = fac[i - 1] * i % mod;
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) cin >> a[i][j];
  for (int i = 0; i <= n; i++) {
    dp[i][i] = fac[i];
    for (int j = 0; j < i; j++)
      dp[i][j] =
          (j * dp[i - 1][j] % mod + (i - j - 1) * dp[i - 1][j + 1] % mod) % mod;
  }
  tp[0] = 1;
  for (int i = 1; i <= n; i++) tp[i] = tp[i - 1] * dp[n][0] % mod;
  long long ans = 0;
  for (int j = 0; j < n; j++) {
    (ans += (a[0][j] - 1 - get(a[0][j], 0)) * dp[n - j - 1][n - j - 1] % mod) %=
        mod;
    add(a[0][j], +1, 0);
  }
  (ans *= tp[n - 1]) %= mod;
  for (int i = 1; i < n; i++) {
    memset(bit, 0, sizeof(bit));
    for (int j = 1; j <= n; j++) add(j, +1, 0), rr[j] = 0, rg[j] = 1;
    long long rans = 0;
    for (int j = 0; j < n; j++) {
      long long g = a[i - 1][j];
      if (!rr[g]) add(g, -1, 0);
      rg[g] = 0;
      long long nn = get(n, 1) + (rr[g] == 0);
      (rans += get(a[i][j] - 1, 0) * dp[n - j - 1][nn - 0] % mod) %= mod;
      (rans += get(a[i][j] - 1, 1) * dp[n - j - 1][nn - 1] % mod) %= mod;
      if (!rr[g]) add(g, +1, 1);
      g = a[i][j];
      if (rg[g])
        add(g, -1, 0);
      else
        add(g, -1, 1);
      rr[g] = 1;
    }
    (rans *= tp[n - i - 1]) %= mod;
    (ans += rans) %= mod;
  }
  cout << ans;
  return 0;
}
