#include <bits/stdc++.h>
char ch;
bool fs;
void re(int& x) {
  while (ch = getchar(), ch < 33)
    ;
  if (ch == '-')
    fs = 1, x = 0;
  else
    fs = 0, x = ch - 48;
  while (ch = getchar(), ch > 33) x = x * 10 + ch - 48;
  if (fs) x = -x;
}
using namespace std;
const int mod = 998244353;
int n, ans, s[2001][2001], f[2001][2001];
struct bit {
  int s[2001];
  void add(int x) {
    for (; x < 2001; x += x & -x) ++s[x];
  }
  int ask(int x) {
    int res = 0;
    for (; x; x -= x & -x) res += s[x];
    return res;
  }
} a, b;
int main() {
  re(n);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) re(s[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 j = 1; j <= i; ++j)
      f[i][j] = (f[i][j - 1] - f[i - 1][j - 1] + mod) % mod;
  }
  for (int i = 1; i <= n; ++i) {
    int wz = n, tmp = 0;
    static bool have[2001];
    memset(have, 0, sizeof have);
    memset(a.s, 0, sizeof a.s);
    memset(b.s, 0, sizeof b.s);
    if (i == 1) {
      for (int j = 1; j <= n; ++j) {
        tmp = (tmp + 1ll * (s[i][j] - 1 - a.ask(s[i][j] - 1)) * f[n - j][0]) %
              mod;
        a.add(s[i][j]);
      }
    } else {
      for (int j = 1; j <= n; ++j) {
        if (!have[s[i - 1][j]]) --wz;
        int ok = s[i][j] - 1 - a.ask(s[i][j] - 1);
        int gg = s[i][j] - 1 - b.ask(s[i][j] - 1);
        if (s[i - 1][j] < s[i][j] && !have[s[i - 1][j]]) --ok, --gg;
        ok -= gg;
        tmp = (tmp + 1ll * ok * f[n - j][wz]) % mod;
        if (wz) tmp = (tmp + 1ll * gg * f[n - j][wz - 1]) % mod;
        a.add(s[i][j]);
        if (!have[s[i - 1][j]]) {
          b.add(s[i - 1][j]);
          have[s[i - 1][j]] = 1;
        }
        if (!have[s[i][j]]) {
          b.add(s[i][j]);
          have[s[i][j]] = 1;
          --wz;
        }
      }
    }
    for (int j = i + 1; j <= n; ++j) tmp = 1ll * tmp * f[n][n] % mod;
    ans = (ans + tmp) % mod;
  }
  printf("%d\n", ans);
}
