#include <bits/stdc++.h>
const int MAXN = 2011;
const int MOD = 998244353;
inline int sum(const int &a, const int &b) {
  return (a + b >= MOD) ? (a + b - MOD) : (a + b);
}
inline int mul(const int &a, const int &b) {
  return (int)((1LL * a * b) % (long long)(MOD));
}
int N;
int A[MAXN][MAXN];
int Ans;
int Fac[MAXN];
int D[MAXN], Pow[MAXN];
int Val[MAXN][MAXN];
bool Vis[2][MAXN];
int Tmp[MAXN];
int C[4][MAXN];
int lowbit(int a) { return a & (-a); }
void Add(int x[], int p, int d) {
  for (int i = p; i <= N; i += lowbit(i)) x[i] += d;
}
int Ask(int x[], int p) {
  int ret = 0;
  for (int i = p; i >= 1; i -= lowbit(i)) ret += x[i];
  return ret;
}
void Update(int p) {
  Add(C[Tmp[p]], p, -1);
  Tmp[p] = (Vis[1][p] << 1) | Vis[0][p];
  Add(C[Tmp[p]], p, 1);
}
int Calc(int t, int k) {
  assert(k >= 0 && k <= t);
  return Val[t][t - k];
}
int Query(int t, int k, int v) {
  int _ = Ask(C[3], N);
  int __ = Ask(C[1], v - 1), ___ = Ask(C[0], v - 1);
  if (k && k < v) {
    if (Tmp[k] == 1) --__;
    if (Tmp[k] == 0) --___;
  }
  int ret = 0;
  if (__ > 0) ret = sum(ret, mul(Calc(t, _ + 1), __));
  if (___ > 0) ret = sum(ret, mul(Calc(t, _), ___));
  return ret;
}
int Query_(int t, int v) {
  int _ = v - 1 - Ask(C[2], v - 1);
  return mul(_, Fac[t]);
}
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i)
    for (int j = 1; j <= N; ++j) scanf("%d", &A[i][j]);
  D[0] = 1;
  D[1] = 0;
  for (int i = 2; i <= N; ++i) D[i] = mul(i - 1, sum(D[i - 1], D[i - 2]));
  Pow[0] = 1;
  for (int i = 1; i <= N; ++i) Pow[i] = mul(Pow[i - 1], D[N]);
  Fac[0] = 1;
  for (int i = 1; i <= N; ++i) Fac[i] = mul(Fac[i - 1], i);
  for (int i = 0; i <= N; ++i) {
    Val[i][0] = Fac[i];
    for (int j = 1; j <= i; ++j)
      Val[i][j] = sum(Val[i][j - 1], MOD - Val[i - 1][j - 1]);
  }
  for (int i = 1; i <= N; ++i) Add(C[0], i, 1);
  for (int j = 1; j <= N; ++j) {
    Ans = sum(Ans, mul(Query_(N - j, A[1][j]), Pow[N - 1]));
    Vis[1][A[1][j]] = true;
    Update(A[1][j]);
  }
  for (int j = 1; j <= N; ++j) {
    Vis[1][A[1][j]] = false;
    Update(A[1][j]);
  }
  for (int i = 2; i <= N; ++i) {
    for (int j = 1; j <= N; ++j) {
      Vis[0][A[i - 1][j]] = true;
      Update(A[i - 1][j]);
    }
    for (int j = 1; j <= N; ++j) {
      Vis[0][A[i - 1][j]] = false;
      Update(A[i - 1][j]);
      Ans = sum(Ans, mul(Query(N - j, A[i - 1][j], A[i][j]), Pow[N - i]));
      Vis[1][A[i][j]] = true;
      Update(A[i][j]);
    }
    for (int j = 1; j <= N; ++j) {
      Vis[1][A[i][j]] = false;
      Update(A[i][j]);
    }
  }
  printf("%d\n", Ans);
  return 0;
}
