#include <bits/stdc++.h>
using namespace std;
const int N = 2005, mod = 998244353;
int n, a[N][N], f[N][N], jie[N], ans, vis1[N], vis2[N], c[N], c2[N], vis3[N];
inline int qpow(int a, int b, int res = 1) {
  for (; b; b >>= 1, a = 1ll * a * a % mod)
    if (b & 1) res = 1ll * res * a % mod;
  return res;
}
void init() {
  jie[0] = 1;
  for (register int i = 1; i <= n; ++i) jie[i] = 1ll * jie[i - 1] * i % mod;
  f[0][0] = 1, f[1][1] = 1;
  for (register int i = 2; i <= n; i++) {
    f[i][0] = 1ll * (i - 1) * (f[i - 1][0] + f[i - 2][0]) % mod;
    for (register int j = 1; j <= i; j++)
      f[i][j] = (f[i][j - 1] + f[i - 1][j - 1]) % mod;
  }
}
inline int lowbit(int x) { return x & -x; }
inline void add(int x, int y) {
  for (; x <= n; x += lowbit(x)) c[x] += y;
}
inline int get(int x, int res = 0) {
  for (; x; x -= lowbit(x)) res += c[x];
  return res;
}
inline void add2(int x, int y) {
  for (; x <= n; x += lowbit(x)) c2[x] += y;
}
inline int get2(int x, int res = 0) {
  for (; x; x -= lowbit(x)) res += c2[x];
  return res;
}
signed main() {
  scanf("%d", &n);
  init();
  for (register int i = 1; i <= n; ++i) {
    int tmp = qpow(f[n][0], n - i), res = 0, num = 0;
    memset(vis1, 0, sizeof(vis1)), memset(vis2, 0, sizeof(vis2));
    memset(vis3, 0, sizeof(vis3));
    memset(c, 0, sizeof(c)), memset(c2, 0, sizeof(c2));
    for (register int j = 1; j <= n; ++j) add(j, 1), add2(j, 1);
    for (register int j = 1; j <= n; ++j) {
      scanf("%d", &a[i][j]);
      if (i == 1) {
        int cnt = get(a[i][j] - 1);
        res = (res + 1ll * cnt * jie[n - j] % mod) % mod;
        add(a[i][j], -1);
      } else {
        vis1[a[i - 1][j]] = 1;
        if (vis3[a[i - 1][j]] == 0)
          add2(a[i - 1][j], -1), vis3[a[i - 1][j]] = 1;
        if (vis2[a[i - 1][j]]) num--;
        int cnt = get(a[i][j] - 1);
        if (a[i - 1][j] < a[i][j] && get(a[i - 1][j]) - get(a[i - 1][j] - 1))
          cnt--;
        int cnt1 = get2(a[i][j] - 1), cnt2 = 0;
        if (a[i - 1][j] < a[i][j] && get2(a[i - 1][j]) - get2(a[i - 1][j] - 1))
          cnt1--;
        cnt2 = cnt - cnt1;
        res = (res + 1ll * cnt1 * f[n - j][num + 1] % mod) % mod;
        res = (res + 1ll * cnt2 * f[n - j][num] % mod) % mod;
        vis2[a[i][j]] = 1;
        add(a[i][j], -1);
        if (!vis3[a[i][j]]) add2(a[i][j], -1), vis3[a[i][j]] = 1;
        if (!vis1[a[i][j]]) num++;
      }
    }
    ans = (ans + 1ll * res * tmp % mod) % mod;
  }
  cout << ans << '\n';
  return 0;
}
