#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int MOD = 998244353;
  long long int n;
  cin >> n;
  pair<long long int, long long int> arr[n];
  for (long long int i = 0; i < n; ++i) cin >> arr[i].first >> arr[i].second;
  long long int fact[n + 1];
  fact[0] = 1;
  for (long long int i = 1; i <= n; ++i) {
    fact[i] = (fact[i - 1] * i + MOD) % MOD;
  }
  map<long long int, long long int> count1;
  map<long long int, long long int> count2;
  map<pair<long long int, long long int>, long long int> count3;
  for (long long int i = 0; i < n; ++i) {
    count1[arr[i].first]++;
    count2[arr[i].second]++;
    count3[make_pair(arr[i].first, arr[i].second)]++;
  }
  long long int n1 = 1, n2 = 1, n3 = 1;
  map<long long int, long long int>::iterator it;
  for (it = count1.begin(); it != count1.end(); ++it) {
    n1 = (n1 * (fact[it->second]) + MOD) % MOD;
  }
  for (it = count2.begin(); it != count2.end(); ++it) {
    n2 = (n2 * (fact[it->second]) + MOD) % MOD;
  }
  map<pair<long long int, long long int>, long long int>::iterator itd;
  long long int last2 = -1;
  for (itd = count3.begin(); itd != count3.end(); ++itd) {
    n3 = (n3 * (fact[itd->second]) + MOD) % MOD;
    if (itd->first.second < last2) {
      n3 = 0;
      break;
    }
    last2 = itd->first.second;
  }
  long long int ans = (fact[n] - n1 - n2 + n3 + 2 * MOD) % MOD;
  cout << ans;
  return 0;
}
