#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return b < a ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
const int Mod = 998244353;
const int maxn = 2010;
int A[maxn + 5][maxn + 5];
int C[maxn + 5][maxn + 5];
int fac[maxn + 5];
int n;
int a[maxn + 5][maxn + 5];
int dp[maxn + 5];
int ways[maxn + 5][maxn + 5];
inline void prepare() {
  for (int i = (0), i_end_ = (maxn + 1); i < i_end_; ++i) {
    C[i][0] = 1;
    for (int j = (1), j_end_ = (i + 1); j < j_end_; ++j)
      C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % Mod;
  }
  for (int i = (0), i_end_ = (maxn + 1); i < i_end_; ++i) {
    A[i][0] = 1;
    for (int j = (0), j_end_ = (i); j < j_end_; ++j)
      A[i][j + 1] = (long long)A[i][j] * (i - j) % Mod;
  }
  for (int i = (0), i_end_ = (maxn + 1); i < i_end_; ++i) fac[i] = A[i][i];
  dp[0] = 1;
  for (int i = (1), i_end_ = (maxn + 1); i < i_end_; ++i) {
    dp[i] = 0;
    for (int j = (2), j_end_ = (i + 1); j < j_end_; ++j)
      (dp[i] += (long long)dp[i - j] * A[i - 1][j - 1] % Mod) %= Mod;
  }
  for (int i = (0), i_end_ = (maxn + 1); i < i_end_; ++i) {
    ways[i][0] = dp[i];
    for (int j = (1), j_end_ = (i + 1); j < j_end_; ++j) {
      ways[i][j] = (ways[i][j - 1] + ways[i - 1][j - 1]) % Mod;
    }
  }
}
int main() {
  prepare();
  scanf("%d", &n);
  for (int i = (0), i_end_ = (n); i < i_end_; ++i)
    for (int j = (0), j_end_ = (n); j < j_end_; ++j)
      scanf("%d", a[i] + j), --a[i][j];
  int ans = 0;
  int tmp = 1;
  for (int i = n - 1; i >= 0; --i) {
    static int fen0[maxn + 5], fen1[maxn + 5];
    memset(fen0, 0, sizeof fen0);
    memset(fen1, 0, sizeof fen1);
    static bool vis[maxn + 5];
    memset(vis, 0, sizeof vis);
    int chain = 0;
    for (int j = (0), j_end_ = (n); j < j_end_; ++j) {
      for (int k = a[i][j] + 1; k <= n; k += k & -k) ++fen0[k];
      int cnt0 = 0;
      for (int k = a[i][j]; k > 0; k -= k & -k) cnt0 += fen0[k];
      if (!i) {
        (ans +=
         (long long)(a[i][j] - cnt0) * fac[n - j - 1] % Mod * tmp % Mod) %= Mod;
      } else {
        if (!vis[a[i - 1][j]]) ++chain;
        int cnt1 = 0;
        for (int k = a[i][j]; k > 0; k -= k & -k) cnt1 += fen1[k];
        if (!vis[a[i - 1][j]] && a[i - 1][j] < a[i][j]) ++cnt0;
        (ans += (long long)(a[i][j] - cnt0 - cnt1) * ways[n - j - 1][chain] %
                Mod * tmp % Mod) %= Mod;
        (ans +=
         (long long)cnt1 * ways[n - j - 1][chain - 1] % Mod * tmp % Mod) %= Mod;
        if (!vis[a[i - 1][j]])
          for (int k = a[i - 1][j] + 1; k <= n; k += k & -k) ++fen1[k];
        vis[a[i - 1][j]] = 1;
        if (vis[a[i][j]]) {
          for (int k = a[i][j] + 1; k <= n; k += k & -k) --fen1[k];
          --chain;
        }
        vis[a[i][j]] = 1;
      }
    }
    tmp = (long long)tmp * dp[n] % Mod;
  }
  (ans += Mod) %= Mod;
  printf("%d\n", ans);
  return 0;
}
