#include <bits/stdc++.h>
using namespace std;
int mat[2001][2001];
long long dp[2001][2001], derr[2001];
long long fpow(long long a, long long n) {
  long long rez = 1;
  while (n) {
    if (n & 1) rez = (rez * a) % 998244353;
    a = (a * a) % 998244353;
    n >>= 1;
  }
  return rez;
}
struct FenwickTree {
  vector<long long> bit;
  int n;
  void init(int n) {
    this->n = n;
    bit.assign(n, 0);
  }
  long long sum(int r) {
    long long ret = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r];
    return ret;
  }
  void add(int idx, long long delta) {
    assert(idx != 0);
    for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta;
  }
  long long sum(int l, int r) { return sum(r) - sum(l - 1); }
  void init(vector<int> a) {
    init(a.size());
    for (size_t i = 0; i < a.size(); i++) add(i, a[i]);
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int ctr1 = 0; ctr1 < n; ++ctr1)
    for (int ctr2 = 0; ctr2 < n; ++ctr2) cin >> mat[ctr1][ctr2];
  dp[0][0] = 1;
  for (int ctr1 = 1; ctr1 <= n; ++ctr1)
    dp[ctr1][0] = dp[ctr1 - 1][0] * ctr1 % 998244353;
  for (int ctr2 = 0; ctr2 <= n; ++ctr2)
    for (int ctr1 = 1; ctr1 <= ctr2; ++ctr1)
      if (ctr2)
        dp[ctr2][ctr1] =
            ((dp[ctr2][ctr1 - 1] - dp[ctr2 - 1][ctr1 - 1]) % 998244353 +
             998244353) %
            998244353;
      else
        dp[ctr2][ctr1] = dp[ctr2][ctr1 - 1];
  derr[0] = 1;
  derr[1] = 0;
  for (int ctr1 = 2; ctr1 <= n; ++ctr1)
    derr[ctr1] = (ctr1 - 1) * (derr[ctr1 - 1] + derr[ctr1 - 2]) % 998244353;
  long long rez = 0;
  for (int ctr1 = 0; ctr1 < n; ++ctr1) {
    FenwickTree bit;
    bit.init(n + 1);
    for (int ctr2 = 1; ctr2 <= n; ++ctr2) bit.add(ctr2, 1);
    long long c_sum = 0;
    if (ctr1 == 0) {
      for (int ctr2 = 0; ctr2 < n; ++ctr2) {
        c_sum = (c_sum + bit.sum(mat[ctr1][ctr2] - 1) * dp[n - ctr2 - 1][0]) %
                998244353;
        bit.add(mat[ctr1][ctr2], -1);
      }
    } else {
      int done[2001] = {0};
      int k = n;
      FenwickTree bit2;
      bit2.init(n + 1);
      for (int ctr2 = 0; ctr2 < n; ++ctr2) {
        if (!done[mat[ctr1 - 1][ctr2]]) {
          bit.add(mat[ctr1 - 1][ctr2], -1);
          done[mat[ctr1 - 1][ctr2]] = 1;
          --k;
        }
        int cnt = bit.sum(mat[ctr1][ctr2] - 1);
        if (k > 0) c_sum = (c_sum + cnt * dp[n - ctr2 - 1][k - 1]) % 998244353;
        c_sum = (c_sum +
                 (mat[ctr1][ctr2] - cnt - 1 - bit2.sum(mat[ctr1][ctr2] - 1)) *
                     dp[n - ctr2 - 1][k] % 998244353) %
                998244353;
        if (mat[ctr1][ctr2] > mat[ctr1 - 1][ctr2] &&
            !bit2.sum(mat[ctr1 - 1][ctr2], mat[ctr1 - 1][ctr2]))
          c_sum = ((c_sum - dp[n - ctr2 - 1][k]) % 998244353 + 998244353) %
                  998244353;
        if (!done[mat[ctr1][ctr2]]) {
          bit.add(mat[ctr1][ctr2], -1);
          done[mat[ctr1][ctr2]] = 1;
          --k;
        }
        bit2.add(mat[ctr1][ctr2], 1);
      }
    }
    rez = (rez + c_sum * fpow(derr[n], n - ctr1 - 1)) % 998244353;
  }
  cout << rez << '\n';
  return 0;
}
