#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000 + 5;
const int mod = 998244353;
const int root = 3;
int a[maxn][maxn], fw[2][maxn], v[maxn];
int cnk[maxn][maxn], f[maxn][maxn], fc[maxn], pw[maxn];
template <int mod, int root>
struct NTT {
  static const int maxn = 4096;
  vector<int> omega;
  NTT() {
    omega.resize(maxn + 1);
    int x = fpow(root, (mod - 1) / maxn);
    omega[0] = 1;
    for (int i = 1; i <= maxn; ++i) omega[i] = omega[i - 1] * 1ll * x % mod;
  }
  int fpow(int a, int n) {
    (n += mod - 1) %= mod - 1;
    int r = 1;
    for (; n; n >>= 1) {
      if (n & 1) r = r * 1ll * a % mod;
      a = a * 1ll * a % mod;
    }
    return r;
  }
  void bitrev(vector<int> &v, int n) {
    int z = __builtin_ctz(n) - 1;
    for (int i = 0; i < n; ++i) {
      int x = 0;
      for (int j = 0; j <= z; ++j) x ^= (i >> j & 1) << (z - j);
      if (x > i) swap(v[x], v[i]);
    }
  }
  void ntt(vector<int> &v, int n) {
    bitrev(v, n);
    for (int s = 2; s <= n; s <<= 1) {
      int z = s >> 1;
      for (int i = 0; i < n; i += s) {
        for (int k = 0; k < z; ++k) {
          int x = v[i + k + z] * 1ll * omega[maxn / s * k] % mod;
          v[i + k + z] = (v[i + k] + mod - x) % mod;
          (v[i + k] += x) %= mod;
        }
      }
    }
  }
  void intt(vector<int> &v, int n) {
    ntt(v, n);
    for (int i = 1; i < n / 2; ++i) swap(v[i], v[n - i]);
    int inv = fpow(n, -1);
    for (int i = 0; i < n; ++i) v[i] = v[i] * 1ll * inv % mod;
  }
  vector<int> operator()(vector<int> a, vector<int> b) {
    int sz = 1;
    while (sz < a.size() + b.size() - 1) sz <<= 1;
    while (a.size() < sz) a.push_back(0);
    while (b.size() < sz) b.push_back(0);
    ntt(a, sz), ntt(b, sz);
    vector<int> c(sz);
    for (int i = 0; i < sz; ++i) c[i] = a[i] * 1ll * b[i] % mod;
    intt(c, sz);
    return c;
  }
};
void add(int *fw, int p, int v) {
  for (int i = p + 1; i < maxn; i += i & -i) fw[i] += v;
}
int query(int *fw, int p) {
  int res = 0;
  for (int i = p + 1; i; i -= i & -i) res += fw[i];
  return res;
}
void precalc(int n) {
  cnk[0][0] = 1;
  for (int i = 1; i <= maxn; ++i) {
    cnk[i][0] = cnk[i - 1][0];
    for (int j = 1; j <= maxn; ++j) {
      cnk[i][j] = cnk[i - 1][j] + cnk[i - 1][j - 1];
      if (cnk[i][j] >= mod) cnk[i][j] -= mod;
    }
  }
  fc[0] = 1;
  for (int i = 1; i <= n; ++i) fc[i] = fc[i - 1] * 1ll * i % mod;
  NTT<mod, root> conv;
  for (int m = 0; m <= n; ++m) {
    vector<int> a(n + 1), b(n + 1);
    for (int i = 0; i <= n; ++i) a[i] = fc[i];
    for (int i = 0, c = 1; i <= n; ++i, c = mod - c)
      b[i] = c * 1ll * cnk[m][i] % mod;
    vector<int> c = conv(a, b);
    for (int i = 0; i <= n; ++i) f[i][m] = c[i];
  }
  pw[0] = 1;
  for (int i = 1; i <= n; ++i) pw[i] = pw[i - 1] * 1ll * f[n][n] % mod;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      scanf("%d", &a[i][j]);
      --a[i][j];
    }
  }
  precalc(n);
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    memset(v, 0, sizeof(v));
    memset(fw, 0, sizeof(fw));
    if (i)
      for (int j = 0; j < n; ++j) add(fw[0], j, 1);
    else
      for (int j = 0; j < n; ++j) v[j] = 1;
    for (int j = 0; j < n; ++j) add(fw[1], j, 1);
    int way = 0;
    for (int j = 0; j < n; ++j) {
      if (i && !v[a[i - 1][j]]) {
        add(fw[0], a[i - 1][j], -1);
        v[a[i - 1][j]] = 1;
      }
      int x = query(fw[1], a[i][j] - 1);
      int y = query(fw[0], a[i][j] - 1);
      if (i && a[i - 1][j] < a[i][j] &&
          query(fw[1], a[i - 1][j]) > query(fw[1], a[i - 1][j] - 1))
        --x;
      if (i && a[i - 1][j] < a[i][j] &&
          query(fw[0], a[i - 1][j]) > query(fw[0], a[i - 1][j] - 1))
        --y;
      way += y * 1ll * f[n - 1 - j][query(fw[0], n) - 1] % mod;
      way %= mod;
      way += (x - y) * 1ll * f[n - 1 - j][query(fw[0], n)] % mod;
      way %= mod;
      if (!v[a[i][j]]) {
        add(fw[0], a[i][j], -1);
        v[a[i][j]] = 1;
      }
      add(fw[1], a[i][j], -1);
    }
    ans += way * 1ll * pw[n - i - 1] % mod;
    ans %= mod;
  }
  printf("%d\n", ans);
  return 0;
}
