#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
const int mod = 998244353;
int add(int a, int b) {
  int c = a + b;
  if (c >= mod) return c - mod;
  return c;
}
int mul(int a, int b) { return a * 1ll * b % mod; }
int dp[N][N], table[N][N], fac[N];
int fw[2][N];
void upd(int k, int i, int v, int n) {
  while (i <= n) fw[k][i] += v, i += (i & -i);
}
int get(int k, int i, int n) {
  int ans = 0;
  while (i) ans += fw[k][i], i -= (i & -i);
  return ans;
}
int mark[N], markn[N], bucket[N], used[N];
int find_order(int *s, int *t, int n) {
  int ans = 0;
  memset((used), 0, sizeof(used));
  ;
  memset((mark), 0, sizeof(mark));
  ;
  memset((markn), 0, sizeof(markn));
  ;
  memset((fw), 0, sizeof(fw));
  ;
  memset((bucket), 0, sizeof(bucket));
  ;
  for (int i = (1); i <= (n); ++i) bucket[t[i]] = i;
  int cnt = 0;
  for (int i = (1); i <= (n); ++i) {
    int fnum = get(0, s[i] - 1, n);
    int nnum = s[i] - 1 - get(1, s[i] - 1, n) - fnum;
    if (s[i] > t[i] && !used[t[i]]) --nnum;
    ans = add(ans, mul(nnum, dp[n - i][cnt + 1 - mark[i]]));
    ans = add(ans, mul(fnum, dp[n - i][cnt - mark[i]]));
    used[s[i]] = 1;
    upd(1, s[i], 1, n);
    upd(0, s[i], -markn[s[i]], n);
    if (!mark[i]) markn[t[i]] = 1, upd(0, t[i], 1, n);
    if (!markn[s[i]]) mark[bucket[s[i]]] = 1;
    if (mark[i] && markn[s[i]]) --cnt;
    if (!mark[i] && !markn[s[i]]) ++cnt;
  }
  return ans;
}
int pwd[2003], mk[2003];
int main() {
  int n;
  fac[0] = dp[0][0] = 1;
  for (int i = (1); i <= (2000); ++i) {
    fac[i] = mul(fac[i - 1], i);
    for (int j = (0); j <= (i); ++j) {
      if (i == j)
        dp[i][i] = fac[i];
      else {
        dp[i][j] = add(mul(j, dp[i - 1][j]), mul(i - j - 1, dp[i - 1][j + 1]));
      }
    }
  }
  scanf("%d", &n);
  for (int i = (1); i <= (n); ++i) {
    for (int j = (1); j <= (n); ++j) {
      scanf("%d", &table[i][j]);
    }
  }
  int ans = 0;
  for (int i = (1); i <= (n - 1); ++i) {
    int sum = 0;
    int first = table[1][i];
    for (int j = (1); j <= (first); ++j) sum += mk[j];
    ans = add(ans, mul(first - 1 - sum, fac[n - i]));
    mk[first] = 1;
  }
  pwd[0] = 1;
  for (int i = (1); i <= (n - 1); ++i) pwd[i] = mul(pwd[i - 1], dp[n][0]);
  ans = mul(ans, pwd[n - 1]);
  for (int i = (1); i <= (n - 1); ++i) {
    ans =
        add(ans, mul(find_order(table[i + 1], table[i], n), pwd[n - (i + 1)]));
  }
  printf("%d", ans);
  return 0;
}
