#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
const long long MAXN = 2007;
long long sum(long long a, long long b) {
  return a + b >= MOD ? a + b - MOD : a + b;
}
long long sub(long long a, long long b) {
  return a - b < 0 ? a - b + MOD : a - b;
}
long long mul(long long a, long long b) { return (a * b) % MOD; }
struct BIT {
  int ar[MAXN];
  BIT() { memset(ar, 0, sizeof ar); }
  void add(int idx, int v) {
    while (idx < MAXN) {
      ar[idx] += v;
      idx += idx & (-idx);
    }
  }
  int query(int idx) {
    int ans = 0;
    while (idx > 0) {
      ans += ar[idx];
      idx -= idx & (-idx);
    }
    return ans;
  }
  int range(int l, int r) {
    if (l > r) return 0;
    return query(r) - query(l - 1);
  }
  void setOff(int idx) {
    if (range(idx, idx) > 0) add(idx, -1);
  }
};
long long a[MAXN][MAXN];
long long d[MAXN][MAXN];
long long derp[MAXN];
long long n;
void pre() {
  d[0][0] = 1;
  for (long long j = 1; j < MAXN; j++) d[0][j] = mul(d[0][j - 1], j);
  for (long long i = 1; i < MAXN; i++) {
    for (long long j = 0; j < MAXN; j++) {
      d[i][j] = mul(j, d[i - 1][j]);
      if (i > 1) d[i][j] = sum(d[i][j], mul(i - 1, d[i - 2][j + 1]));
    }
  }
}
long long f(long long n, long long m) {
  if (n < 0 || m < 0) return 0;
  return d[n][m];
}
long long row(long long i) {
  BIT fenwick1, fenwick2;
  for (long long j = 1; j <= n; j++) {
    fenwick1.add(j, 1);
    fenwick2.add(j, 1);
  }
  long long ans = 0;
  if (i == 1) {
    for (long long j = 1; j <= n; j++) {
      ans = sum(ans, mul(fenwick1.query(a[i][j] - 1), f(0, n - j)));
      fenwick1.add(a[i][j], -1);
    }
    return ans;
  }
  for (long long j = 1; j <= n; j++) {
    long long choto = fenwick1.query(a[i][j] - 1);
    if (a[i - 1][j] < a[i][j] && fenwick1.range(a[i - 1][j], a[i - 1][j]))
      choto--;
    fenwick2.setOff(a[i - 1][j]);
    long long jhamela = fenwick2.query(a[i][j] - 1);
    assert(jhamela <= choto);
    int tot = fenwick2.query(n);
    ans = sum(ans, mul(jhamela, f(tot - 1, n - j - tot + 1)));
    ans = sum(ans, mul(choto - jhamela, f(tot, n - j - tot)));
    fenwick2.setOff(a[i][j]);
    fenwick1.setOff(a[i][j]);
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  pre();
  while (cin >> n) {
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= n; j++) {
        cin >> a[i][j];
      }
    }
    derp[0] = 1;
    for (long long i = 1; i < MAXN; i++) derp[i] = mul(derp[i - 1], d[n][0]);
    long long ans = 0;
    for (long long i = 1; i <= n; i++) {
      long long x = row(i);
      ans = sum(ans, mul(x, derp[n - i]));
    }
    cout << ans << "\n";
  }
  return 0;
}
