#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
inline int add(int a, int b) {
  if ((a += b) >= mod) a -= mod;
  return a;
}
inline int dec(int a, int b) {
  if ((a -= b) < 0) a += mod;
  return a;
}
inline int mult(int a, int b) {
  long long t = 1ll * a * b;
  if (t >= mod) t %= mod;
  return t;
}
inline int power(int a, int b) {
  int out = 1;
  while (b) {
    if (b & 1) out = mult(out, a);
    a = mult(a, a);
    b >>= 1;
  }
  return out;
}
int n, a[2010][2010], fac[2010], F[2010][2010], pw[2010], b[2010];
inline int lowbit(int x) { return x & (-x); }
class BIT {
 public:
  int dat[2010], cnt;
  inline void init() {
    cnt = 0;
    memset(dat, 0, sizeof(dat));
  }
  inline void modify(int p) {
    cnt++;
    for (; p <= n; p += lowbit(p)) dat[p]++;
  }
  inline int query(int p) {
    int ret = 0;
    for (; p; p -= lowbit(p)) ret += dat[p];
    return ret;
  }
} T, t;
inline void add(int x) {
  if (b[x] == 1) T.modify(x);
  b[x]++;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]);
  fac[0] = 1;
  for (int i = 1; i <= n; i++) fac[i] = mult(fac[i - 1], i);
  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] = dec(F[i][j - 1], F[i - 1][j - 1]);
  }
  pw[0] = 1;
  for (int i = 1; i <= n; i++) pw[i] = mult(pw[i - 1], F[n][n]);
  int sum = 0, ans = 0;
  for (int i = 1; i <= n; i++)
    sum =
        add(sum, mult(fac[n - i], dec(dec(a[1][i], 1), t.query(a[1][i] - 1)))),
    t.modify(a[1][i]);
  ans = mult(sum, pw[n - 1]);
  for (int i = 2; i <= n; i++) {
    t.init();
    T.init();
    sum = 0;
    memset(b, 0, sizeof b);
    for (int j = n; j >= 1; j--) {
      add(a[i][j]);
      add(a[i - 1][j]);
      t.modify(a[i][j]);
      int x = T.query(a[i][j] - 1), y = t.query(a[i][j] - 1) - x, z = T.cnt;
      if (b[a[i - 1][j]] == 2 && a[i - 1][j] < a[i][j]) x--;
      if (b[a[i - 1][j]] == 2) z--;
      if (z) sum = add(sum, mult(x, F[n - j][z - 1]));
      sum = add(sum, mult(y, F[n - j][z]));
    }
    ans = add(ans, mult(sum, pw[n - i]));
  }
  printf("%d\n", ans);
  return 0;
}
