#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void cmin(T &a, T b) {
  ((a > b) && (a = b));
}
template <class T>
inline void cmax(T &a, T b) {
  ((a < b) && (a = b));
}
char IO;
template <class T = int>
T rd() {
  T s = 0;
  int f = 0;
  while (!isdigit(IO = getchar())) f |= IO == '-';
  do s = (s << 1) + (s << 3) + (IO ^ '0');
  while (isdigit(IO = getchar()));
  return f ? -s : s;
}
const int N = 2010, P = 998244353;
int n;
int A[N], B[N];
int F[N], G[N][N];
long long qpow(long long x, long long k = P - 2) {
  long long res = 1;
  for (; k; k >>= 1, x = x * x % P)
    if (k & 1) res = res * x % P;
  return res;
}
struct BIT {
  int s[N];
  void clear() { memset(s, 0, sizeof s); }
  void Add(int p, int x) {
    while (p <= n) s[p] += x, p += p & -p;
  }
  int Que(int p) {
    int res = 0;
    while (p) res += s[p], p -= p & -p;
    return res;
  }
  int Que(int l, int r) { return Que(r) - Que(l - 1); }
} T, T2;
int X[N], Y[N];
void Add(int &x, long long y) { x = (x + y) % P; }
int main() {
  n = rd(), F[0] = F[2] = 1;
  for (int i = 3, iend = n; i <= iend; ++i)
    F[i] = 1ll * (i - 1) * (F[i - 1] + F[i - 2]) % P;
  for (int t = n - 1, tend = 0; t >= tend; --t) {
    if (t < n - 1)
      for (int i = 1, iend = n; i <= iend; ++i) X[i] = 1;
    for (int i = 1, iend = n; i <= iend; ++i) Y[i] = 1;
    T.clear(), T2.clear();
    for (int i = 1, iend = n; i <= iend; ++i) T2.Add(i, 1);
    for (int i = 1, iend = n; i <= iend; ++i)
      if (X[i] && Y[i]) T.Add(i, 1);
    int w = qpow(F[n], t);
    for (int i = 1, iend = n; i <= iend; ++i) {
      if (X[A[i]] && Y[A[i]]) T.Add(A[i], -1);
      X[A[i]] = 0;
      B[i] = rd();
      int c = T2.Que(B[i] - 1) - (A[i] < B[i] && Y[A[i]]);
      int all = T.Que(n), d = T.Que(B[i] - 1);
      if (c - d) {
        Add(G[n - i - all][all], 1ll * w * (c - d));
      }
      if (d) {
        Add(G[n - i - all + 1][all - 1], 1ll * w * d);
      }
      if (X[B[i]] && Y[B[i]]) T.Add(B[i], -1);
      T2.Add(B[i], -1), Y[B[i]] = 0;
    }
    swap(A, B);
  }
  for (int i = n, iend = 1; i >= iend; --i)
    for (int j = 0, jend = n - i; j <= jend; ++j) {
      G[i - 1][j] += G[i][j], ((G[i - 1][j] >= P) && (G[i - 1][j] -= P));
      G[i - 1][j + 1] += G[i][j],
          ((G[i - 1][j + 1] >= P) && (G[i - 1][j + 1] -= P));
    }
  int ans = 0;
  for (int i = 0, iend = n; i <= iend; ++i)
    ans = (ans + 1ll * F[i] * G[0][i]) % P;
  printf("%d\n", ans);
}
