#include <bits/stdc++.h>
using namespace std;
long long n, gt[300005], mod = 998244353, res;
pair<long long, long long> p[300005];
long long v1[300005], v2[300005];
void Solve() {
  cin >> n;
  for (long long i = 1, _b = n; i <= _b; i++) {
    long long a, b;
    cin >> a >> b;
    p[i] = {a, b};
    v1[a]++;
    v2[b]++;
  }
  gt[0] = 1;
  for (long long i = 1, _b = n; i <= _b; i++) gt[i] = gt[i - 1] * i % mod;
  long long tmp1 = 1, tmp2 = 1;
  for (long long i = 1, _b = n; i <= _b; i++) {
    (tmp1 *= gt[v1[i]]) %= mod;
  }
  for (long long i = 1, _b = n; i <= _b; i++) {
    (tmp2 *= gt[v2[i]]) %= mod;
  }
  long long ans = (tmp1 + tmp2) % mod;
  sort(p + 1, p + n + 1);
  bool ok = true;
  for (long long i = 2, _b = n; i <= _b; i++) {
    if (p[i].second < p[i - 1].second) {
      ok = false;
      break;
    }
  }
  if (ok) {
    long long tru = 1, ad = 1;
    for (long long i = 2, _b = n; i <= _b; i++) {
      if (p[i] == p[i - 1])
        ad++;
      else {
        tru *= gt[ad];
        tru %= mod;
        ad = 1;
      }
      if (i == n) {
        tru *= gt[ad];
        tru %= mod;
      }
    }
    ans -= tru;
  }
  cout << (gt[n] + mod - ans) % mod;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int Test_numbers = 1;
  while (Test_numbers--) Solve();
  return 0;
}
