#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
const int MOD = 998244353;
int n;
int a[N][N];
long long fakt[N], raz[N];
long long b[N][N], res;
long long mn, f1[N], f2[N], f3[N];
int imam[N];
pair<int, int> gdje[N];
long long fastpot(int x, int e) {
  if (e == 1) return x;
  if (e == 0) return 1;
  if (e % 2 == 0) {
    long long ret = fastpot(x, e / 2);
    ret *= ret;
    ret %= MOD;
    return ret;
  }
  long long ret = fastpot(x, e / 2);
  ret *= ret;
  ret %= MOD;
  ret *= x;
  ret %= MOD;
  return ret;
}
void ubaci1(int x, int kol) {
  for (; x <= N; x += x & -x) {
    f1[x] += kol;
  }
}
void ubaci2(int x) {
  for (; x <= N; x += x & -x) {
    f2[x]++;
  }
}
void ubaci3(int x, int kol) {
  for (; x <= N; x += x & -x) {
    f3[x] += kol;
  }
}
int upit1(int x) {
  int ret = 0;
  for (; x > 0; x -= x & -x) {
    ret += f1[x];
  }
  return ret;
}
int upit2(int x) {
  int ret = 0;
  for (; x > 0; x -= x & -x) {
    ret += f2[x];
  }
  return ret;
}
int upit3(int x) {
  int ret = 0;
  for (; x > 0; x -= x & -x) {
    ret += f3[x];
  }
  return ret;
}
long long func(int x) {
  long long ret = 0;
  int br = 0;
  memset(imam, 0, sizeof imam);
  memset(f1, 0, sizeof f1);
  memset(f2, 0, sizeof f2);
  memset(f3, 0, sizeof f3);
  for (int i = 0; i < n; i++) {
    gdje[a[x][i]].first = i;
    gdje[a[x - 1][i]].second = i;
  }
  for (int i = n - 1; i > 0; i--) {
    ubaci1(a[x - 1][i], 1);
    imam[a[x - 1][i]]++;
  }
  for (int i = 0; i < n; i++) {
    br = upit3(n);
    long long im = a[x][i] - 1 - upit2(a[x][i] - 1);
    if (upit2(a[x - 1][i]) - upit2(a[x - 1][i] - 1) == 0 &&
        a[x - 1][i] < a[x][i])
      im--;
    long long des = upit1(a[x][i] - 1);
    long long dod = 0;
    dod += (im - des) * b[n - 1 - i][n - 1 - i - br];
    dod %= MOD;
    if (des != 0) dod += des * b[n - 1 - i][n - 2 - i - br];
    dod %= MOD;
    ret += dod;
    ret %= MOD;
    ubaci2(a[x][i]);
    if (i != n - 1 && imam[a[x - 1][i + 1]]) {
      ubaci1(a[x - 1][i + 1], -1);
      imam[a[x - 1][i + 1]] = 0;
    }
    if (imam[a[x][i]]) {
      ubaci1(a[x][i], -1);
      imam[a[x][i]] = 0;
    }
    if (gdje[a[x][i]].second > i) ubaci3(a[x][i], 1);
    if (i != n - 1 && gdje[a[x - 1][i + 1]].first < i + 1)
      ubaci3(a[x - 1][i + 1], -1);
  }
  ret *= fastpot(mn, n - x - 1);
  ret %= MOD;
  return ret;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) scanf("%d", &a[i][j]);
  }
  fakt[0] = 1;
  raz[0] = 1;
  for (int i = 1; i <= n; i++) {
    fakt[i] = fakt[i - 1] * i;
    fakt[i] %= MOD;
  }
  for (int i = 1; i <= n; i++) {
    raz[i] = fastpot(i, MOD - 2);
    raz[i] *= raz[i - 1];
    raz[i] %= MOD;
  }
  for (int i = 0; i <= n; i++) {
    b[i][i] = fakt[i];
    long long mno = i;
    for (int j = 1; j <= i; j++) {
      long long dod = mno;
      dod *= raz[j];
      dod %= MOD;
      dod *= fakt[i - j];
      dod %= MOD;
      if (j % 2 == 1)
        b[i][i] -= dod;
      else
        b[i][i] += dod;
      b[i][i] %= MOD;
      b[i][i] += MOD;
      b[i][i] %= MOD;
      mno *= (i - j);
      mno %= MOD;
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < i; j++) {
      if (j > 0) {
        b[i][j] = b[i - 1][j - 1];
        b[i][j] *= j;
        b[i][j] %= MOD;
      }
      long long dod = b[i - 1][j];
      dod *= (i - j);
      dod %= MOD;
      b[i][j] += dod;
      b[i][j] %= MOD;
    }
  }
  mn = b[n][n];
  for (int i = 0; i < n; i++) {
    long long dod = 0;
    dod += a[0][i] - 1 - upit2(a[0][i]);
    dod *= fakt[n - i - 1];
    dod %= MOD;
    dod *= fastpot(mn, n - 1);
    dod %= MOD;
    res += dod;
    ubaci2(a[0][i]);
  }
  for (int i = 1; i < n; i++) {
    res += func(i);
    res %= MOD;
  }
  cout << res;
  return 0;
}
