#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 3e5 + 1;
const ll mod = 998244353LL;
int f[2][N];
ll fac[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  fac[0] = 1LL;
  for (int i = 1; i <= n; i += 1) fac[i] = (fac[i - 1] * 1LL * i) % mod;
  vector<pair<int, int>> v;
  for (int i = 1; i <= n; i += 1) {
    int x, y;
    cin >> x >> y;
    f[0][x] += 1;
    f[1][y] += 1;
    v.push_back({x, y});
  }
  ll ans = fac[n], p = 1LL, q = 1LL, r = 1LL;
  for (int i = 1; i <= n; i += 1) {
    p = (p * fac[f[0][i]]) % mod;
    q = (q * fac[f[1][i]]) % mod;
  }
  ans -= p;
  ans += mod;
  ans %= mod;
  ans -= q;
  ans += mod;
  ans %= mod;
  sort(v.begin(), v.end());
  int flag = 1;
  for (int i = 0; i < n; i += 1) {
    if (i && v[i].second < v[i - 1].second) {
      flag = 0;
      break;
    }
  }
  if (flag == 1) {
    int x = 1, y = 1;
    for (int i = 0; i < n; i += 1) {
      if (i && v[i].second == v[i - 1].second)
        x += 1;
      else {
        x = min(x, y);
        r = (r * fac[x]) % mod;
        x = 1;
        y = 1;
        continue;
      }
      if (i && v[i].first == v[i - 1].first)
        y += 1;
      else {
        x = min(x, y);
        r = (r * fac[x]) % mod;
        x = 1;
        y = 1;
        continue;
      }
    }
    x = min(x, y);
    r = (r * fac[x]) % mod;
    ans += r;
    ans %= mod;
  }
  cout << ans;
  return 0;
}
