#include <bits/stdc++.h>
using namespace std;
const int N = 2002;
const int mod = 998244353;
int n, a[N][N], f[N], dp[N][N], las[N], now[N], ans;
struct Stree {
  int v[N], tp;
  void ins(int x) {
    for (int i = x; i <= n; i += (i & -i)) v[i]++;
  }
  void ers(int x) {
    for (int i = x; i <= n; i += (i & -i)) v[i]--;
  }
  int que(int x) {
    int rt = 0;
    for (int i = x; i; i -= (i & -i)) rt += v[i];
    return rt;
  }
} Sl, Sr;
int pow(int x, int n) {
  int ret = 1;
  while (n) {
    if (n & 1) ret = 1ll * ret * x % mod;
    n >>= 1, x = 1ll * x * x % mod;
  }
  return ret;
}
int main() {
  Sl.tp = 1, Sr.tp = 2;
  scanf("%d", &n);
  f[0] = dp[0][0] = 1;
  for (int i = 2; i <= n; i++)
    f[i] = 1ll * (i - 1) * (f[i - 1] + f[i - 2]) % mod;
  for (int i = 1; i <= n; i++) dp[i][0] = 1ll * dp[i - 1][0] * i % mod;
  for (int j = 1; j <= n; j++)
    for (int i = 1; i <= n; i++)
      dp[i][j] = (dp[i][j - 1] - dp[i - 1][j - 1] + mod) % mod;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]);
  for (int i = 1; i <= n; i++) {
    int lim = n, res = 0;
    if (i == 1) {
      for (int j = 1; j <= n; j++) Sr.ins(j);
      for (int j = 1; j <= n; j++) {
        Sr.ers(a[i][j]);
        res = (1ll * Sr.que(a[i][j]) * dp[n - j][0] + res) % mod;
      }
      ans = (1ll * res * pow(f[n], n - i) + ans) % mod;
      continue;
    }
    for (int j = 1; j <= n; j++) Sr.ins(j), las[j] = now[j] = true;
    for (int j = 1; j <= n; j++) {
      las[a[i - 1][j]] = false;
      if (now[a[i - 1][j]]) Sr.ers(a[i - 1][j]), lim--;
      if (las[a[i][j]])
        Sr.ers(a[i][j]);
      else
        Sl.ers(a[i][j]);
      res = (1ll * (Sr.que(a[i][j])) * dp[n - j][lim - 1] + res) % mod;
      res = (1ll * (Sl.que(a[i][j])) * dp[n - j][lim] + res) % mod;
      if (now[a[i - 1][j]]) Sl.ins(a[i - 1][j]);
      if (las[a[i][j]]) lim--;
      now[a[i][j]] = false;
    }
    ans = (1ll * res * pow(f[n], n - i) + ans) % mod;
  }
  printf("%d\n", ans);
}
