#include <bits/stdc++.h>
using namespace std;
const int p = 998244353;
inline void rd(int &x) {
  x = 0;
  register char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - 48;
    c = getchar();
  }
}
inline void upd(int &x, int y) { x += x + y >= p ? y - p : y; }
int n, a[2005][2005], f[2005], pw[2005], c[2005][3], ans, fac[2005], finv[2005],
    inv[2005], dp[2005][2005];
bool vis[2005];
inline void ada(int x, int op) {
  for (int i = x; i <= n; i += (i & (-i))) c[i][op]++;
}
inline int ask(int x, int op) {
  int ret = 0;
  for (int i = x; i; i -= (i & (-i))) ret += c[i][op];
  return ret;
}
inline int C(int nn, int mm) {
  return nn < 0 || mm < 0 || nn < mm
             ? 0
             : 1ll * fac[nn] * finv[mm] % p * finv[nn - mm] % p;
}
int main() {
  rd(n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) rd(a[i][j]);
  f[2] = 1;
  for (int i = 3; i <= n; i++) f[i] = 1ll * (i - 1) * (f[i - 1] + f[i - 2]) % p;
  pw[0] = 1;
  for (int i = 1; i <= n; i++) pw[i] = 1ll * pw[i - 1] * f[n] % p;
  fac[0] = finv[0] = fac[1] = finv[1] = inv[1] = 1;
  for (int i = 2; i <= n; i++) {
    inv[i] = 1ll * (p - p / i) * inv[p % i] % p;
    fac[i] = 1ll * fac[i - 1] * i % p;
    finv[i] = 1ll * finv[i - 1] * inv[i] % p;
  }
  for (int i = 1; i <= n; i++) {
    int ct = a[1][i] - 1 - ask(a[1][i], 0);
    upd(ans, 1ll * ct * fac[n - i] % p * pw[n - 1] % p);
    ada(a[1][i], 0);
  }
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    dp[i][0] = fac[i];
    for (int j = 1; j <= i; j++)
      dp[i][j] = (dp[i][j - 1] - dp[i - 1][j - 1] + p) % p;
  }
  for (int i = 2; i <= n; i++) {
    memset(c, 0, sizeof(c));
    memset(vis, 0, sizeof(vis));
    int ap = 0;
    for (int j = 1; j <= n; j++) {
      int ct = a[i][j] - 1 - ask(a[i][j] - 1, 2), bj = ap;
      int op = 0;
      if (a[i][j] > a[i - 1][j] && !vis[a[i - 1][j]]) op = 1;
      upd(ans, 1ll * (ct - op) * dp[n - j][n - bj - 1 - (!vis[a[i - 1][j]])] %
                   p * pw[n - i] % p);
      ct = a[i][j] - 1 - ask(a[i][j] - 1, 0) - ct;
      upd(ans, 1ll * ct * dp[n - j][n - bj - (!vis[a[i - 1][j]])] % p *
                   pw[n - i] % p);
      ada(a[i][j], 0);
      if (!vis[a[i][j]]) ada(a[i][j], 2), vis[a[i][j]] = 1, ap++;
      ada(a[i - 1][j], 1);
      if (!vis[a[i - 1][j]]) ada(a[i - 1][j], 2), vis[a[i - 1][j]] = 1, ap++;
    }
  }
  printf("%d\n", ans);
}
