#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
const long long maxn = 2000 + 10, N = 2e5 + 10, SQ = 320, base = 800287,
                mod = 998244353, INF = 1e14 + 10, lg = 22;
const long double eps = 1e-4;
long long n, ans, a[maxn][maxn], fac[maxn], D[maxn], dp[maxn][maxn],
    fen_suff[maxn], fen_pre[maxn];
bool mark_suff[maxn], mark_pre[maxn];
long long power(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a) % mod;
    }
    b /= 2;
    a = (a * a) % mod;
  }
  return res;
}
void pre() {
  fac[0] = 1, D[0] = 1;
  for (long long i = 2; i < maxn; ++i) {
    D[i] = ((i - 1) * ((D[i - 1] + D[i - 2]) % mod)) % mod;
  }
  for (long long i = 1; i < maxn; ++i) {
    fac[i] = (i * fac[i - 1]) % mod;
  }
  for (long long i = 0; i < maxn; ++i) {
    dp[i][0] = D[i];
  }
  for (long long i = 1; i < maxn; ++i) {
    for (long long j = 1; j <= i; ++j) {
      dp[i][j] =
          ((j * dp[i - 1][j - 1]) % mod + ((i - j) * dp[i - 1][j]) % mod) % mod;
    }
  }
}
void add_suff(long long p, long long x) {
  if (!p) {
    return;
  }
  for (; p <= n; p += (p & -p)) {
    fen_suff[p] += x;
  }
}
long long get_suff(long long p) {
  long long res = 0;
  for (; p; p -= (p & -p)) {
    res += fen_suff[p];
  }
  return res;
}
void add_pre(long long p, long long x) {
  if (!p) {
    return;
  }
  for (; p <= n; p += (p & -p)) {
    fen_pre[p] += x;
  }
}
long long get_pre(long long p) {
  long long res = 0;
  for (; p; p -= (p & -p)) {
    res += fen_pre[p];
  }
  return res;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (long long i = 0; i < n; ++i) {
    for (long long j = 0; j < n; ++j) {
      cin >> a[i][j];
    }
  }
  pre();
  for (long long i = 0; i < n; ++i) {
    long long remain = n - 1 - i, can = a[0][i] - 1 - get_pre(a[0][i] - 1);
    ans = (ans + (fac[remain] * can) % mod) % mod;
    add_pre(a[0][i], 1);
  }
  ans = (ans * power(D[n], n - 1)) % mod;
  for (long long i = 1; i < n; ++i) {
    long long cnt = 0, res = 0;
    fill(mark_suff, mark_suff + n + 10, 0);
    fill(mark_pre, mark_pre + n + 10, 0);
    fill(fen_suff, fen_suff + n + 10, 0);
    fill(fen_pre, fen_pre + n + 10, 0);
    for (long long j = 1; j < n; ++j) {
      add_suff(a[i - 1][j], 1);
      mark_suff[a[i - 1][j]] = 1;
    }
    for (long long j = 0; j < n; ++j) {
      long long tot = a[i][j] - 1 -
                      (a[i - 1][j] < a[i][j] && !mark_pre[a[i - 1][j]]) -
                      get_pre(a[i][j] - 1),
                val = get_suff(a[i][j] - 1);
      long long upd = (mark_pre[a[i - 1][j]] ? 0 : 1);
      res = (res + (val * dp[n - j - 1][cnt + upd]) % mod) % mod;
      res = (res + ((tot - val) * dp[n - j - 1][cnt - 1 + upd]) % mod) % mod;
      add_pre(a[i][j], 1), mark_pre[a[i][j]] = 1;
      cnt -= mark_pre[a[i - 1][j]];
      cnt += mark_suff[a[i][j]];
      if (mark_suff[a[i][j]]) {
        add_suff(a[i][j], -1), mark_suff[a[i][j]] = 0;
      }
      if (mark_suff[a[i - 1][j + 1]]) {
        add_suff(a[i - 1][j + 1], -1), mark_suff[a[i - 1][j + 1]] = 0;
      }
    }
    ans = (ans + (res * power(D[n], n - 1 - i) % mod)) % mod;
  }
  cout << ans << '\n';
}
