#include <bits/stdc++.h>
inline int add(int a, int b) {
  a += b;
  if (a >= 998244353) a -= 998244353;
  return a;
}
inline int dec(int a, int b) {
  a -= b;
  if (a < 0) a += 998244353;
  return a;
}
inline int mult(int a, int b) {
  long long t = 1ll * a * b;
  if (t >= 998244353) t %= 998244353;
  return t;
}
inline int lowbit(int x) { return x & (-x); }
int n, m[2010][2010], fac[2010], f[2010][2010], ans = 0, pw[2010];
int vis[2010];
class tree {
 public:
  int dat[2010];
  void clear() { memset(dat, 0, sizeof(dat)); }
  void modify(int pos, int v) {
    for (int i = pos; i <= n; i += lowbit(i)) dat[i] += v;
  }
  int query(int pos) {
    int out = 0;
    for (int i = pos; i; i -= lowbit(i)) out += dat[i];
    return out;
  }
} A, B;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%d", &m[i][j]);
    }
  }
  fac[0] = fac[1] = 1;
  f[1][0] = 1;
  for (int i = 2; i <= n; i++) {
    f[i][0] = fac[i] = mult(fac[i - 1], i);
    for (int j = 1; j <= i; 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;
  for (int i = 1; i <= n; i++) {
    int t1 = A.query(m[1][i] - 1);
    sum = add(sum, mult(fac[n - i], m[1][i] - 1 - t1));
    A.modify(m[1][i], 1);
  }
  ans = add(ans, mult(sum, pw[n - 1]));
  for (int i = 2; i <= n; i++) {
    memset(vis, 0, sizeof(vis));
    A.clear();
    B.clear();
    sum = 0;
    for (int j = n; j >= 1; j--) {
      ++vis[m[i][j]];
      if (vis[m[i][j]] == 2) B.modify(m[i][j], 1);
      ++vis[m[i - 1][j]];
      if (vis[m[i - 1][j]] == 2) B.modify(m[i - 1][j], 1);
      A.modify(m[i][j], 1);
      int t1 = B.query(m[i][j] - 1), t2 = A.query(m[i][j] - 1) - t1,
          t3 = B.query(n);
      if (m[i - 1][j] < m[i][j] && vis[m[i - 1][j]] == 2) t1--;
      if (vis[m[i - 1][j]] == 2) t3--;
      sum = add(sum, add(mult(t1, f[n - j][t3 - 1]), mult(t2, f[n - j][t3])));
    }
    ans = add(ans, mult(sum, pw[n - i]));
  }
  printf("%d\n", ans);
  return 0;
}
