#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
const long long int mod = 998244353;
const long long int big = 2.19e15 + 1;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-15;
template <class T, class U>
bool mineq(T& a, U b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U>
bool maxeq(T& a, U b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
long long int gcd(long long int a, long long int b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
template <class T>
void SO(T& ve) {
  sort(ve.begin(), ve.end());
}
template <class T>
void REV(T& ve) {
  reverse(ve.begin(), ve.end());
}
template <class T>
long long int LBI(vector<T>& ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T>
long long int UBI(vector<T>& ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}
int main(void) {
  long long int n, i, j, k, ans = 0, gyo = 1;
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  static long long int dp[2005][2005] = {0};
  dp[0][0] = 1;
  static long long int kai[2005] = {0};
  kai[0] = 1;
  for (i = 1; i <= n; i++) {
    kai[i] = kai[i - 1] * i % mod;
  }
  long long int wa = 1;
  for (i = 1; i <= n; i++) {
    wa = (kai[i] - wa) % mod;
    if (wa < 0) {
      wa += mod;
    }
    dp[i][0] = wa;
    for (j = 1; j <= i; j++) {
      dp[i][j] = (dp[i][j - 1] + dp[i - 1][j - 1]) % mod;
      wa += dp[i][j];
      wa %= mod;
    }
  }
  vector<int> mae(n);
  for (i = 0; i < n; i++) {
    vector<int> ima(n);
    ans *= dp[n][0];
    ans %= mod;
    vector<int> seg(2048);
    vector<int> use(2048);
    long long int gen = 0;
    vector<int> aaa(n + 1);
    int dti = 0;
    for (j = 0; j < n; j++) {
      int x, bas;
      cin >> x;
      ima[j] = x;
      aaa[x]++;
      if (aaa[x] == 1) {
        dti++;
        bas = 2048 + x;
        while (bas > 1) {
          if (bas % 2 == 0) {
            use[bas / 2]++;
          }
          bas /= 2;
        }
      } else {
        dti--;
      }
      aaa[mae[j]]++;
      if (aaa[mae[j]] == 1) {
        bas = 2048 + mae[j];
        while (bas > 1) {
          if (bas % 2 == 0) {
            use[bas / 2]++;
          }
          bas /= 2;
        }
        dti++;
      } else {
        dti--;
      }
      bas = 2048 + x;
      int uuu = x - 1;
      while (bas > 1) {
        if (bas % 2 == 1) {
          uuu -= use[bas / 2];
        }
        bas /= 2;
      }
      int bbb = x - 1;
      bas = 2048 + x;
      while (bas > 1) {
        if (bas % 2 == 1) {
          bbb -= seg[bas / 2];
        } else {
          seg[bas / 2]++;
        }
        bas /= 2;
      }
      if (i > 0) {
        int y = dti / 2;
        if (aaa[x] == 1) {
          y--;
        }
        int frag = 0;
        if (x > mae[j] && aaa[mae[j]] == 1) {
          frag++;
        }
        gen += (bbb - uuu - frag) * dp[n - j - 1][y];
        gen += uuu * dp[n - j - 1][y + 1];
      } else {
        gen += bbb * kai[n - 1 - j];
      }
      gen %= mod;
    }
    ans += gen;
    ans %= mod;
    mae = ima;
  }
  cout << ans << endl;
  return 0;
}
