#include <bits/stdc++.h>
using namespace std;
long long factmodMAX(int ma) {
  long long rez = 1;
  for (long long i = 2; i <= ma; i++) {
    rez *= i;
    if (rez > 998244353) rez %= 998244353;
  }
  return rez;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  long long ans, badl, badr, bad2;
  ans = factmodMAX(n);
  vector<int> ri;
  vector<pair<int, int> > leri;
  int bufl, bufr;
  for (int i = 0; i < n; i++) {
    cin >> bufl >> bufr;
    ri.push_back(bufr);
    leri.push_back(pair<int, int>(bufl, bufr));
  }
  sort(leri.begin(), leri.end());
  leri.push_back(pair<int, int>(1000000, 1000000));
  int ind1 = 0;
  badl = 1;
  for (int i = 0; i <= n; i++) {
    if (leri[i].first == leri[ind1].first) continue;
    badl *= factmodMAX(i - ind1);
    badl %= 998244353;
    ind1 = i;
  }
  bool bad2e = true;
  for (int i = 1; i <= n; i++) {
    if (leri[i].second >= leri[i - 1].second) continue;
    bad2e = false;
    break;
  }
  if (bad2e) {
    bad2 = 1;
    ind1 = 0;
    for (int i = 0; i <= n; i++) {
      if (leri[i].first == leri[ind1].first &&
          leri[i].second == leri[ind1].second)
        continue;
      bad2 *= factmodMAX(i - ind1);
      bad2 %= 998244353;
      ind1 = i;
    }
  } else {
    bad2 = 0;
  }
  sort(ri.begin(), ri.end());
  ri.push_back(1000000);
  ind1 = 0;
  badr = 1;
  for (int i = 0; i <= n; i++) {
    if (ri[i] == ri[ind1]) continue;
    badr *= factmodMAX(i - ind1);
    badr %= 998244353;
    ind1 = i;
  }
  ans = ans - badl - badr + bad2 + (long long)(998244353 * 2);
  ans %= 998244353;
  cout << ans << endl;
  return 0;
}
