#include <bits/stdc++.h>
using namespace std;
int n, m, k;
long long int adj[(int)3e5 + 5], vis[(int)3e5 + 5], par[(int)3e5 + 5],
    fact[(int)3e5 + 5];
map<int, int> first, second;
pair<int, int> a[(int)3e5 + 5];
map<pair<int, int>, int> both;
void go() {
  cin >> n;
  fact[0] = 1;
  for (int i = 1; i < n + 1; i++) {
    fact[i] = fact[i - 1] * i % 998244353;
  }
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
    first[a[i].first]++, second[a[i].second]++, both[a[i]]++;
  }
  long long int cn1 = 1, cn2 = 1, cn3 = 1;
  for (auto i : first) {
    cn1 = cn1 * fact[i.second] % 998244353;
  }
  for (auto i : second) {
    cn2 = cn2 * fact[i.second] % 998244353;
  }
  sort(a, a + n);
  long long int phi = 1;
  for (int i = 1; i < n; i++) {
    if (a[i].second < a[i - 1].second) {
      phi = 0;
      break;
    }
  }
  if (phi) {
    for (auto i : both) {
      cn3 = cn3 * fact[i.second] % 998244353;
    }
  } else
    cn3 = 0;
  ans = fact[n] - cn1 - cn2 + cn3 + 998244353 + 998244353 % 998244353;
  ans += 998244353;
  ans = ans % 998244353;
  cout << ans << '\n';
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL), cout.tie(NULL);
  int t = 1;
  while (t--) {
    go();
  }
}
