#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
int n, a[2005][2005];
long long T[2005][2005];
long long qp(long long a, long long b) {
  long long x = 1;
  a %= MOD;
  while (b) {
    if (b & 1) x = x * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return x;
}
struct BS {
  int b[666666];
  void edt(int x, int y) {
    for (; x <= n; x += x & -x) b[x] += y;
  }
  int qs(int x) {
    int s = 0;
    for (; x >= 1; x -= x & -x) s += b[x];
    return s;
  }
  void clr() {
    for (int i = 1; i <= n; ++i) b[i] = 0;
  }
} A, B, C;
bool va[666666], vb[666666];
int main() {
  scanf("%d", &n);
  T[0][0] = 1;
  for (int i = 1; i <= n; ++i) {
    T[i][0] = T[i - 1][0] * i % MOD;
    for (int j = 1; j <= i; ++j)
      T[i][j] = (T[i][j - 1] - T[i - 1][j - 1]) % MOD;
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) scanf("%d", &a[i][j]);
  long long aa = 0;
  {
    for (int j = 1; j <= n; ++j) A.edt(j, 1);
    long long ans = 0;
    for (int j = 1; j <= n; ++j)
      ans += A.qs(a[1][j] - 1) * (T[n - j][0]), ans %= MOD, A.edt(a[1][j], -1);
    aa += ans * qp((T[n][n]), n - 1);
    aa %= MOD;
  }
  for (int i = 2; i <= n; ++i) {
    A.clr();
    B.clr();
    for (int j = 1; j <= n; ++j) B.edt(j, 1), va[j] = vb[j] = 0;
    int a1 = 0, a0 = 0;
    long long ans = 0;
    for (int j = 1; j <= n; ++j) {
      if (!vb[a[i - 1][j]]) {
        if (va[a[i - 1][j]])
          A.edt(a[i - 1][j], -1);
        else
          B.edt(a[i - 1][j], -1);
      };
      if (va[a[i - 1][j]]) {
        if (vb[a[i - 1][j]])
          a1 += -1;
        else
          a0 += -1;
      };
      va[a[i - 1][j]] = 1;
      if (va[a[i - 1][j]]) {
        if (vb[a[i - 1][j]])
          a1 += 1;
        else
          a0 += 1;
      };
      {
        int x1 = a1 + 1, x0 = a0 - 1;
        long long f = A.qs(a[i][j] - 1);
        if (f) ans += T[n - x0 - x1][n - x0 - x1 - x0] * f;
        ans %= MOD;
      }
      {
        int x1 = a1, x0 = a0;
        long long f = B.qs(a[i][j] - 1);
        if (f) ans += T[n - x0 - x1][n - x0 - x1 - x0] * f;
        ans %= MOD;
      }
      if (!vb[a[i - 1][j]]) {
        if (va[a[i - 1][j]])
          A.edt(a[i - 1][j], 1);
        else
          B.edt(a[i - 1][j], 1);
      };
      if (va[a[i][j]]) {
        if (vb[a[i][j]])
          a1 += -1;
        else
          a0 += -1;
      }
      if (!vb[a[i][j]]) {
        if (va[a[i][j]])
          A.edt(a[i][j], -1);
        else
          B.edt(a[i][j], -1);
      };
      vb[a[i][j]] = 1;
      if (va[a[i][j]]) {
        if (vb[a[i][j]])
          a1 += 1;
        else
          a0 += 1;
      }
      if (!vb[a[i][j]]) {
        if (va[a[i][j]])
          A.edt(a[i][j], 1);
        else
          B.edt(a[i][j], 1);
      };
    }
    aa += ans * qp((T[n][n]), n - i);
    aa %= MOD;
  }
  aa = (aa % MOD + MOD) % MOD;
  cout << aa << "\n";
}
