#include <bits/stdc++.h>
using namespace std;
int a[2000][2000];
int D[2001], C[2001][2001], fact[2001];
int dp[2001][2001];
int bit[2001], bit2[2001], bit3[2001];
set<int> S;
int seen[2001];
int main() {
  int i, j;
  int n;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) scanf("%d", &a[i][j]);
  }
  int k;
  D[0] = 1, D[1] = 0, D[2] = 1;
  for (i = 3; i <= n; i++)
    D[i] = ((long long int)(i - 1) * (D[i - 1] + D[i - 2])) % 998244353;
  for (i = 0; i <= n; i++) {
    dp[i][0] = 1;
    for (j = 1; j < i; j++)
      dp[i][j] =
          (dp[i - 1][j] + (long long int)j * dp[i - 1][j - 1]) % 998244353;
    dp[i][i] = D[i];
  }
  for (i = 0; i <= n; i++) {
    C[i][0] = 1;
    for (j = 1; j <= i; j++)
      C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % 998244353;
  }
  fact[0] = 1;
  for (i = 1; i <= n; i++)
    fact[i] = ((long long int)i * fact[i - 1]) % 998244353;
  long long int ans = 0;
  for (i = 0; i < n; i++) {
    long long int m = 1, sum = 0;
    for (j = i + 1; j < n; j++) m *= D[n], m %= 998244353;
    fill(bit, bit + n + 1, 0), fill(bit2, bit2 + n + 1, 0), S.clear(),
        fill(bit3, bit3 + n + 1, 0), fill(seen, seen + n + 1, 0);
    if (i == 0) {
      for (j = 0; j < n; j++) {
        int x = 0;
        for (k = a[i][j]; k > 0; k -= k & (-k)) x += bit[k];
        x = a[i][j] - x - 1;
        sum += ((long long int)fact[n - j - 1] * x) % 998244353,
            sum %= 998244353;
        for (k = a[i][j]; k <= n; k += k & (-k)) bit[k]++;
      }
    } else {
      for (j = 0; j < n; j++) {
        S.insert(j + 1);
        for (k = j + 1; k <= n; k += k & (-k)) bit[k]++;
      }
      for (j = 0; j < n; j++) {
        if (S.count(a[i - 1][j])) {
          S.erase(a[i - 1][j]);
          if (!seen[a[i - 1][j]]) {
            for (k = a[i - 1][j]; k <= n; k += k & (-k)) bit[k]--, bit2[k]++;
          }
        }
        if (S.count(a[i][j])) {
          for (k = a[i][j]; k <= n; k += k & (-k)) bit[k]--;
        } else {
          for (k = a[i][j]; k <= n; k += k & (-k)) bit2[k]--;
        }
        for (k = a[i][j]; k <= n; k += k & (-k)) bit3[k]--;
        seen[a[i][j]] = 1;
        int p = 0;
        for (k = a[i][j] - 1; k > 0; k -= k & (-k)) p += bit[k];
        if (S.count(a[i - 1][j]) && !seen[a[i - 1][j]] &&
            (a[i - 1][j] < a[i][j]))
          p--;
        int s = S.size() - 1;
        ;
        sum += (p * (((long long int)dp[n - j - 1][s] * fact[n - j - 1 - s]) %
                     998244353)) %
               998244353;
        p = 0;
        for (k = a[i][j] - 1; k > 0; k -= k & (-k)) p += bit2[k];
        if (!S.count(a[i - 1][j]) && !seen[a[i - 1][j]] &&
            (a[i - 1][j] < a[i][j]))
          p--;
        s = S.size();
        ;
        sum += (p * (((long long int)dp[n - j - 1][s] * fact[n - j - 1 - s]) %
                     998244353)) %
               998244353;
        sum;
        if (S.count(a[i][j])) S.erase(a[i][j]);
        sum %= 998244353;
      }
    }
    ans += (sum * m) % 998244353, ans %= 998244353;
  }
  printf("%I64d\n", ans);
  return 0;
}
