#include <bits/stdc++.h>
using namespace std;
int read() {
  int res = 0;
  char c = getchar();
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) res = res * 10 + c - '0', c = getchar();
  return res;
}
const int N = 2e3 + 10;
const int Mod = 998244353;
int n, ans = 0;
int matrix[N][N], p[N][N], t[N];
bitset<N> f, g, pre[N], a, b, c;
int add(int a, int b) { return (a += b) >= Mod ? a - Mod : a; }
int sub(int a, int b) { return (a -= b) < 0 ? a + Mod : a; }
int mul(int a, int b) { return 1ll * a * b % Mod; }
int fast_pow(int a, int b) {
  int res = 1;
  for (; b; b >>= 1, a = mul(a, a))
    if (b & 1) res = mul(res, a);
  return res;
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    pre[i] = (pre[i - 1] << 1);
    pre[i].set(1);
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) matrix[i][j] = read();
  t[1] = 0, t[2] = 1;
  for (int i = 3; i <= n; i++) t[i] = mul(i - 1, add(t[i - 1], t[i - 2]));
  p[0][0] = 1;
  for (int i = 1; i <= n; i++) p[i][0] = mul(p[i - 1][0], i);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= i; j++) p[i][j] = sub(p[i][j - 1], p[i - 1][j - 1]);
  for (int i = 1; i <= n; i++) {
    a = f & pre[matrix[1][i] - 1];
    ans = add(ans, mul(matrix[1][i] - 1 - a.count(), p[n - i][0]));
    f.set(matrix[1][i]);
  }
  ans = mul(ans, fast_pow(t[n], n - 1));
  for (int i = 2; i <= n; i++) {
    f.reset(), g = pre[n];
    int tmp = 0;
    for (int j = 1; j <= n; j++) {
      g.reset(matrix[i - 1][j]);
      a = (pre[n] ^ f) & pre[matrix[i][j] - 1];
      a.reset(matrix[i - 1][j]);
      b = a & g;
      c = f & g;
      tmp = add(tmp, mul(b.count(), p[n - j][n - j - (1 + c.count())]));
      tmp = add(tmp, mul(a.count() - b.count(), p[n - j][n - j - c.count()]));
      f.set(matrix[i][j]);
    }
    ans = add(ans, mul(tmp, fast_pow(t[n], n - i)));
  }
  printf("%d", ans);
  return 0;
}
