#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int Minf = 0x7f7f7f7f;
const long long INF = 0x3f3f3f3f3f3f3f3fll;
const long long MINF = 0x7f7f7f7f7f7f7f7fll;
const int mod = 998244353;
const int N = 2020;
int a[N][N];
int f[N][N];
int vis1[N];
int vis2[N];
int flag[N];
struct bit {
  int a[N];
  void add(int x, int y) {
    for (int i = x; i < N; i += (i & -i)) a[i] += y;
  }
  int query(int x) {
    int ans = 0;
    for (int i = x; i; i -= (i & -i)) ans += a[i];
    return ans;
  }
} t[2];
int ksm(int n, int p) {
  int a = 1;
  while (p) {
    if (p & 1) a = 1ll * a * n % mod;
    n = 1ll * n * n % mod;
    p >>= 1;
  }
  return a;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = (1); i <= n; ++i)
    for (int j = (1); j <= n; ++j) scanf("%d", &a[i][j]);
  f[0][0] = 1;
  for (int i = (1); i <= n; ++i) f[i][0] = 1ll * f[i - 1][0] * i % mod;
  for (int i = (1); i <= n; ++i)
    for (int j = (1); j <= i; ++j)
      f[i][j] = (f[i][j - 1] - f[i - 1][j - 1] + mod) % mod;
  long long ans = 0;
  for (int i = (1); i <= n; ++i) {
    int cnt = 0;
    for (int j = (1); j <= n; ++j) {
      long long s1 = 0, s2 = 0;
      long long cc = a[i][j] - 1 - t[0].query(a[i][j] - 1);
      if (i - 1)
        if (a[i - 1][j] < a[i][j]) cc -= (vis2[a[i - 1][j]] == 0);
      s1 = t[1].query(a[i][j] - 1);
      s2 = cc - s1;
      if (i - 1) vis1[a[i - 1][j]] = 1;
      if (i - 1)
        if (vis2[a[i - 1][j]]) ++cnt;
      if (i - 1)
        ans += ksm(f[n][n], n - i) *
               (s1 * f[n - j][n - j - j + cnt + 1] % mod +
                s2 * f[n - j][n - j - j + cnt] % mod) %
               mod;
      else
        ans += ksm(f[n][n], n - i) * s2 % mod * f[n - j][0] % mod;
      vis2[a[i][j]] = 1;
      if (vis1[a[i][j]]) ++cnt;
      t[0].add(a[i][j], 1);
      if (i - 1)
        if (!vis2[a[i - 1][j]]) t[1].add(a[i - 1][j], 1), flag[a[i - 1][j]] = 1;
      if (flag[a[i][j]]) t[1].add(a[i][j], -1), flag[a[i][j]] = 0;
    }
    memset(vis1, 0, sizeof vis1);
    memset(vis2, 0, sizeof vis2);
    memset(t, 0, sizeof t);
  }
  cout << ans % mod << endl;
  return 0;
}
