#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007LL;
long long large = 2000000000000000000LL;
int n;
int a[2010][2010];
long long per[2010], de[2010], ip[2010], d[2010][2010] = {};
long long pd[2010];
class BIT {
 private:
  vector<long long> ft;

 public:
  BIT(long long n) { ft.assign(n + 10, 0); }
  long long LSOne(long long x) { return x & (-x); }
  long long rsq(long long b) {
    long long sum = 0;
    for (; b > 0; b -= LSOne(b)) sum += ft[b];
    return sum;
  }
  long long rsq(long long a, long long b) {
    return rsq(b) - (a == 1 ? 0 : rsq(a - 1));
  }
  void adjust(long long k, long long v) {
    for (; k < (long long)ft.size(); k += LSOne(k)) ft[k] += v;
  }
};
long long power(long long a, long long x) {
  long long ans = 1;
  while (x) {
    if (x & 1) ans = ans * a % mod;
    a = a * a % mod;
    x >>= 1;
  }
  return ans;
}
long long c(long long n, long long m) {
  if (m < 0 || m > n) return 0;
  return per[n] * ip[m] % mod * ip[n - m] % mod;
}
int main() {
  mod = 998244353LL;
  cin >> n;
  per[0] = 1;
  for (int i = 1; i < 2010; i++) {
    per[i] = per[i - 1] * i % mod;
    ip[i] = power(per[i], mod - 2);
  }
  de[0] = 1;
  de[1] = 0;
  for (int i = 2; i < 2010; i++)
    de[i] = (de[i - 1] + de[i - 2]) * (i - 1) % mod;
  pd[0] = 1;
  for (int i = 1; i < 2010; i++) pd[i] = de[n] * pd[i - 1] % mod;
  for (int i = 0; i < 2010; i++) {
    for (int j = 0; j <= i; j++) {
      if (j == 0) {
        d[i][j] = per[i];
        continue;
      }
      if (j == i) {
        d[i][j] = de[i];
        continue;
      }
      d[i][j] = d[i - 1][j] + 1LL * j * (d[i - 2][j - 1] + d[i - 1][j]) % mod;
      if (i > j + 1)
        d[i][j] += 1LL * (i - 1 - j) * (d[i - 2][j] + d[i - 1][j + 1]) % mod;
      d[i][j] %= mod;
    }
    for (int j = i + 1; j < 2010; j++) d[i][j] = 0;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%d", &a[i][j]);
    }
  }
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    BIT bt(n + 1), bt2(n + 1);
    for (int j = 1; j <= n; j++) {
      bt.adjust(j, 1);
      if (i) bt2.adjust(j, 1);
    }
    for (int j = 0; j < n; j++) {
      long long temp = 0;
      if (i) {
        if (a[i - 1][j] > a[i][j]) {
          long long l1 = bt.rsq(a[i][j] - 1);
          long long l2 = bt2.rsq(a[i][j] - 1);
          l1 -= l2;
          long long same = bt2.rsq(n);
          if (bt2.rsq(a[i - 1][j], a[i - 1][j])) same--;
          temp = l1 * d[n - 1 - j][same];
          if (same) temp += l2 * d[n - 1 - j][same - 1];
        } else {
          if (bt2.rsq(a[i - 1][j], a[i - 1][j]) == 0) {
            long long l1 = bt.rsq(a[i][j] - 1);
            long long l2 = bt2.rsq(a[i][j] - 1);
            l1 -= l2;
            long long same = bt2.rsq(n);
            temp = l1 * d[n - 1 - j][same];
            if (same) temp += l2 * d[n - 1 - j][same - 1];
          } else {
            long long l1 = bt.rsq(a[i][j] - 1) - 1;
            long long l2 = bt2.rsq(a[i][j] - 1) - 1;
            l1 -= l2;
            long long same = bt2.rsq(n);
            same--;
            temp = l1 * d[n - 1 - j][same];
            if (same) temp += l2 * d[n - 1 - j][same - 1];
          }
        }
      } else {
        temp = bt.rsq(a[i][j] - 1) * per[n - j - 1];
      }
      temp %= mod;
      temp *= pd[n - 1 - i];
      temp %= mod;
      ans += temp;
      bt.adjust(a[i][j], -1);
      if (bt2.rsq(a[i][j], a[i][j])) bt2.adjust(a[i][j], -1);
      if (i && bt2.rsq(a[i - 1][j], a[i - 1][j])) bt2.adjust(a[i - 1][j], -1);
    }
  }
  cout << ans % mod << endl;
  return 0;
}
