#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f;
const int N = 2e3 + 10;
const long long mod = 998244353;
const bool debug = true;
struct BITstree {
  int node[N];
  void init(int n) {
    for (int i = 1; i <= n; ++i) node[i] = 0;
  }
  int sum(int x) {
    int ret = 0;
    for (; x; x -= (x & (-x))) ret += node[x];
    return ret;
  }
  void up(int x, int n, int d) {
    for (; x <= n; x += (x & (-x))) node[x] += d;
  }
} bit1, bit2;
int dp[N][N], v[N][N], n, vis1[N], vis2[N], pp[N];
void init() {
  dp[0][0] = 1;
  for (int i = 1; i <= n; ++i) {
    dp[i][0] = i * 1ll * dp[i - 1][0] % mod;
    dp[i][1] = (i - 1) * 1ll * dp[i - 1][0] % mod;
    for (int j = 2; j <= i; ++j)
      dp[i][j] = ((i - j) * 1ll * dp[i - 1][j - 1] +
                  (j - 1) * 1ll * dp[i - 1][j - 2]) %
                 mod;
  }
  pp[0] = 1, pp[1] = dp[n][n];
  for (int i = 2; i <= n; ++i) pp[i] = pp[i - 1] * 1ll * pp[1] % mod;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) scanf("%d", &v[i][j]);
  init();
  int ans = 0, ret = 0;
  for (int i = 1; i <= n; ++i) {
    bit1.init(n), bit2.init(n);
    for (int j = 0; j <= n; ++j) vis1[j] = 0, vis2[j] = 0;
    ret = 0;
    for (int j = n, x, u, vvv, vv; j; --j) {
      x = v[i][j];
      if (vis1[x])
        bit2.up(x, n, 1);
      else
        bit1.up(x, n, 1);
      vis2[x] = 1;
      x = v[i - 1][j];
      if (vis2[x]) bit1.up(x, n, -1);
      x = v[i][j];
      u = bit1.sum(x - 1);
      vv = bit2.sum(n);
      vvv = bit2.sum(x - 1);
      ret =
          (ret + vvv * 1ll * dp[n - j][vv - 1] + u * 1ll * dp[n - j][vv]) % mod;
      x = v[i - 1][j];
      if (vis2[x]) bit2.up(x, n, 1);
      vis1[x] = 1;
    }
    ans = (ans + ret * 1ll * pp[n - i]) % mod;
  }
  printf("%d\n", ans);
  return 0;
}
