#include <bits/stdc++.h>
using namespace std;
const int ts = 2005;
const int mod = 998244353;
int a[ts][ts];
int A[ts][ts], fact[ts];
int both[ts], layer[ts];
int dpow[ts];
int In[ts];
int n, ans = 0;
int trace(int *f, int i) {
  int res = 0;
  while (i > 0) {
    res += f[i];
    i -= (i & (-i));
  }
  return res;
}
void update(int *f, int i, int val) {
  while (i <= n) {
    f[i] += val;
    i += i & (-i);
  }
}
int get(int *f, int i) { return trace(f, i) - trace(f, i - 1); }
void Erase(int *f, int i) { update(f, i, -get(f, i)); }
void init() {
  cin >> n;
  A[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    A[i][0] = (1ll * A[i - 1][0] * i) % mod;
    for (int j = 1; j <= n; j++) {
      A[i][j] = (A[i][j - 1] - A[i - 1][j - 1] + mod) % mod;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%d", &a[i][j]);
    }
  }
  fact[0] = 1;
  for (int i = 1; i <= n; i++) fact[i] = (1ll * fact[i - 1] * A[n][n]) % mod;
}
int calc() {
  bool cnt[ts];
  memset(cnt, 0, sizeof(cnt));
  for (int i = 1; i <= n; i++) {
    int res = 0;
    for (int x = 1; x < a[1][i]; x++) res += (!cnt[x]);
    In[1] = (In[1] + 1ll * res * A[n - i][0]) % mod;
    cnt[a[1][i]] = 1;
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      update(both, j, 1);
      update(layer, j, 1);
    }
    for (int j = 1; j <= n; j++) {
      Erase(both, a[i - 1][j]);
      int changeit = trace(both, a[i][j] - 1);
      int total = trace(layer, a[i][j] - 1) -
                  (a[i - 1][j] < a[i][j] && get(layer, a[i - 1][j]));
      int notchange = total - changeit;
      int total_right_common = trace(both, n);
      if (total_right_common >= 1) {
        In[i] =
            (In[i] + 1ll * changeit * A[n - j][total_right_common - 1]) % mod;
      }
      In[i] = (In[i] + 1ll * notchange * A[n - j][total_right_common]) % mod;
      Erase(layer, a[i][j]);
      Erase(both, a[i][j]);
    }
  }
  for (int i = 1; i <= n; i++) {
    ans = (ans + 1ll * In[i] * fact[n - i]) % mod;
  }
  return ans;
}
int main() {
  init();
  printf("%d", calc());
}
