#include <bits/stdc++.h>
using namespace std;
template <typename T>
void chkmax(T &x, T y) {
  x = x > y ? x : y;
}
template <typename T>
void chkmin(T &x, T y) {
  x = x > y ? y : x;
}
template <typename T>
void upd(T &x, T y, T mod) {
  x = x + y > mod ? x + y - mod : x + y;
}
template <typename T>
void sub(T &x, T y, T mod) {
  x = x - y < 0 ? x - y + mod : x - y;
}
template <typename T>
void multi(T &x, T y, T mod) {
  x = 1ll * x * y % mod;
}
const int INF = (1ll << 30);
template <typename T>
void read(T &x) {
  x = 0;
  bool f = 1;
  char ch;
  do {
    ch = getchar();
    if (ch == '-') f = 0;
  } while (ch > '9' || ch < '0');
  do {
    x = x * 10 + ch - '0';
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  x = f ? x : -x;
}
template <typename T>
void write(T x) {
  if (x < 0) x = ~x + 1, putchar('-');
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
const int N = 2000 + 5;
const long long mod = 998244353;
int n, pw[N], cnt[N], fac[N], a[N][N], f[N][N];
bool vis[N];
struct BIT {
  int c[N];
  inline int lowbit(int x) { return x & -x; }
  inline void add(int i, int x) {
    for (; i <= n; i += lowbit(i)) c[i] += x;
  }
  inline int sum(int i) {
    int ret = 0;
    for (; i; i -= lowbit(i)) ret += c[i];
    return ret;
  }
} bit1, bit2;
inline int qpow(int a, int b) {
  int ret = 1;
  while (b) {
    if (b & 1) ret = 1ll * ret * a % mod;
    a = 1ll * a * a % mod;
    b >>= 1;
  }
  return ret;
}
inline void init() {
  fac[0] = pw[0] = 1;
  for (int i = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % mod;
  f[1][0] = 1;
  for (int i = 2; i <= n; i++) {
    f[i][0] = fac[i];
    for (int j = 1; j <= n; j++) {
      f[i][j] = (f[i][j - 1] % mod - f[i - 1][j - 1] % mod + mod) % mod;
    }
  }
  for (int i = 1; i <= n; i++) pw[i] = 1ll * pw[i - 1] * f[n][n] % mod;
}
int main() {
  read(n);
  init();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      read(a[i][j]);
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int t = a[1][i] - 1 - bit1.sum(a[1][i] - 1);
    upd(ans, (1ll * t * fac[n - i] % mod * pw[n - 1]) % mod, mod);
    bit1.add(a[1][i], 1);
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j <= n; j++) bit1.c[j] = bit2.c[j] = cnt[j] = 0;
    int rep = n;
    for (int j = 1; j <= n; j++) {
      if (++cnt[a[i - 1][j]] == 1) bit2.add(a[i - 1][j], 1), rep--;
      int s1 = a[i][j] - 1 - bit2.sum(a[i][j] - 1),
          s2 = a[i][j] - 1 - bit1.sum(a[i][j] - 1) - s1;
      if (cnt[a[i - 1][j]] == 1 && a[i - 1][j] < a[i][j]) s2--;
      ans = (ans + 1ll *
                       (1ll * s1 * f[n - j][rep - 1] % mod +
                        1ll * s2 * f[n - j][rep] % mod) *
                       pw[n - i] % mod) %
            mod;
      if (++cnt[a[i][j]] == 1) bit2.add(a[i][j], 1), rep--;
      bit1.add(a[i][j], 1);
    }
  }
  cout << ans << endl;
  return 0;
}
