#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
class fenwick_tree {
  vector<int> f;
  int n;

 public:
  fenwick_tree(int N) : f(vector<int>(N, 0)), n(N) {}
  void add(int pos, int val) {
    for (; pos < n; pos = (pos | (pos + 1))) f[pos] += val;
  }
  int sum(int pos) {
    int rval = 0;
    for (; pos >= 0; pos = (pos & (pos + 1)) - 1) rval += f[pos];
    return rval;
  }
};
int mul(int a, int b) { return (a * 1ll * b) % mod; }
int add(int a, int b) {
  a += b;
  while (a < 0) a += mod;
  while (a >= mod) a -= mod;
  return a;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<vector<int> > f(n + 1, vector<int>(n + 1, 0));
  for (int i = 0; i <= n; i++) {
    f[i][i] = (i ? mul(i, f[i - 1][i - 1]) : 1);
    for (int j = i - 1; j >= 0; j--) {
      f[i][j] = add(f[i][j + 1], -f[i - 1][j]);
    }
  }
  vector<int> pwf(n + 1, 1);
  for (int i = 1; i <= n; i++) pwf[i] = mul(pwf[i - 1], f[n][0]);
  vector<vector<int> > a(n, vector<int>(n));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) cin >> a[i][j];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    fenwick_tree f1(n + 1), f2(n + 1);
    vector<bool> seennw(n + 1, false), seenpv(n + 1, false);
    int val = 0, placed = 0;
    for (int j = 0; j < n; j++) {
      if (i == 0) {
        f1.add(a[i][j], 1);
        int small = a[i][j] - f1.sum(a[i][j] - 1) - 1;
        val = add(val, mul(small, f[n - j - 1][n - j - 1]));
      } else {
        int small1 = a[i][j] - f1.sum(a[i][j] - 1) - 1,
            small2 = f2.sum(a[i][j] - 1);
        seennw[a[i][j]] = true, seenpv[a[i - 1][j]] = true;
        if (seennw[a[i - 1][j]] && seenpv[a[i - 1][j]]) placed++;
        if (!seennw[a[i - 1][j]] && a[i - 1][j] < a[i][j]) small1--;
        val = add(val, mul(small2, f[n - j - 1][j - placed]));
        val = add(val, mul(small1 - small2, f[n - j - 1][1 + j - placed]));
        f1.add(a[i][j], 1);
        if (seennw[a[i][j]] && seenpv[a[i][j]]) {
          f2.add(a[i][j], -1);
          placed++;
        }
        if (!seennw[a[i - 1][j]] && seenpv[a[i - 1][j]]) f2.add(a[i - 1][j], 1);
      }
    }
    ans = add(ans, mul(val, pwf[n - i - 1]));
  }
  cout << ans << "\n";
  return 0;
}
