#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T read(register T& t) {
  register T f = 1;
  register char ch = getchar();
  t = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -f;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') t = t * 10 + ch - '0', ch = getchar();
  t *= f;
  return t;
}
template <typename T, typename... Args>
inline void read(T& t, Args&... args) {
  read(t);
  read(args...);
}
const long long p = 998244353;
inline long long power(register long long x, register long long k = p - 2) {
  register long long re = 1;
  for (; k; k >>= 1, x = x * x % p)
    if (k & 1) re = re * x % p;
  return re;
}
int n;
int a[2005][2005];
long long f[2005][2005], g[2005];
long long fac[2005];
template <int T>
struct BIT {
  int c[T];
  BIT() { memset(c, 0, sizeof(c)); }
  inline void add(int x, int val) {
    for (; x < T; x += (x & -x)) c[x] += val;
  }
  inline int ask(int x) {
    int re = 0;
    for (; x; x -= (x & -x)) re += c[x];
    return re;
  }
};
int main() {
  read(n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) read(a[i][j]);
  fac[0] = 1;
  for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p;
  f[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    f[i][0] = fac[i];
    for (int j = 1; j <= i; j++)
      f[i][j] = (f[i][j - 1] - f[i - 1][j - 1] + p) % p;
  }
  for (int i = 0; i <= n; i++) g[i] = f[i][i];
  long long ans = 0;
  static bool vis[2005];
  for (int j = 1; j <= n; j++) {
    int t = 0;
    for (int i = 1; i < a[1][j]; i++)
      if (!vis[i]) t++;
    ans = (ans + fac[n - j] * t) % p;
    vis[a[1][j]] = 1;
  }
  ans = ans * power(g[n], n - 1) % p;
  for (int i = 2; i <= n; i++) {
    static BIT<2005> S, T;
    S = BIT<2005>();
    T = BIT<2005>();
    long long sum = 0;
    static bool vis[2005];
    memset(vis, 0, sizeof(vis));
    static bool used[2005];
    memset(used, 0, sizeof(used));
    for (int j = 1; j <= n; j++) {
      long long A = a[i][j] - 1 - S.ask(a[i][j] - 1);
      long long B = T.ask(a[i][j] - 1);
      if (vis[a[i - 1][j]] == 0 && a[i - 1][j] < a[i][j]) A--;
      if (vis[a[i - 1][j]] == 0) T.add(a[i - 1][j], 1);
      vis[a[i][j]] = 1;
      int x = T.ask(n);
      sum =
          (sum + B * f[n - j][n - j - x + 1] + (A - B) * f[n - j][n - j - x]) %
          p;
      S.add(a[i][j], 1);
      if (used[a[i][j]]) T.add(a[i][j], -1);
      used[a[i - 1][j]] = 1;
    }
    ans = (ans + sum * power(g[n], n - i)) % p;
  }
  printf("%lld\n", ans);
  return 0;
}
