#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
long long N, M;
const long long MAXN = 5000;
const long long INF = 2147483600;
const long long Mod = 998244353LL;
long long a[2010][2010];
long long g[2010][2010];
namespace BIT {
long long c[3][MAXN + 1];
inline void Add(long long n, long long k, long long x) {
  for (; k <= N; k += (k & (-k))) c[n][k] += x;
}
inline long long Query(long long n, long long k) {
  long long ans = 0;
  for (; k; k -= (k & (-k))) ans += c[n][k];
  return ans;
}
}  // namespace BIT
using namespace BIT;
long long f[2010], fac[2010];
bool vis[2010];
long long vall[2010];
int main() {
  N = read();
  for (long long i = 1; i <= N; i++) {
    for (long long j = 1; j <= N; j++) a[i][j] = read();
  }
  g[0][0] = 1;
  g[1][0] = 0;
  for (long long i = 2; i <= N; i++) {
    g[i][0] = 1LL * (i - 1) * (g[i - 1][0] + g[i - 2][0]) % Mod;
  }
  for (long long i = 1; i <= N; i++) {
    for (long long j = 1; j <= i; j++)
      g[i][j] = (g[i - 1][j - 1] + g[i][j - 1]) % Mod;
  }
  long long Fn = g[N][0];
  f[0] = 1;
  long long rak = 0;
  for (long long i = 1; i <= N; i++) f[i] = f[i - 1] * Fn % Mod;
  fac[0] = 1;
  long long ans = 0;
  for (long long i = 1; i <= N; i++) fac[i] = fac[i - 1] * i % Mod;
  for (long long i = 1; i <= N; i++)
    rak = (rak + (a[1][i] - 1 - Query(0, a[1][i])) * fac[N - i] % Mod) % Mod,
    Add(0, a[1][i], 1);
  (ans += rak * f[N - 1] % Mod) %= Mod;
  for (long long i = 2; i <= N; i++) {
    for (long long j = 1; j <= N; j++)
      c[0][j] = c[1][j] = c[2][j] = 0, vall[j] = vis[j] = 0;
    long long cnt = 0;
    for (long long j = 1; j <= N; j++) {
      if (vall[a[i - 1][j]]) ++cnt;
      long long d1 = a[i][j] - 1 - Query(0, a[i][j] - 1) +
                     Query(1, a[i][j] - 1) -
                     (a[i - 1][j] < a[i][j] && !vall[a[i - 1][j]]);
      long long a1 = a[i][j] - 1 - Query(2, a[i][j] - 1) -
                     (a[i - 1][j] < a[i][j] && !vis[a[i - 1][j]]) - d1;
      (ans += g[N - j][j - cnt] * d1 % Mod * f[N - i] % Mod) %= Mod;
      if (2 * j != cnt)
        (ans += g[N - j][j - cnt - 1] * a1 % Mod * f[N - i] % Mod) %= Mod;
      if (vall[a[i][j]]) Add(1, a[i][j], 1);
      if (vall[a[i - 1][j]]) Add(1, a[i - 1][j], 1);
      vall[a[i][j]]++;
      vall[a[i - 1][j]]++;
      Add(0, a[i][j], 1);
      Add(0, a[i - 1][j], 1);
      Add(2, a[i][j], 1);
      vis[a[i][j]] = true;
      if (vall[a[i][j]] == 2) ++cnt;
    }
  }
  cout << ans << endl;
  return 0;
}
