#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
const long long mod = 998244353ll;
int arr[maxn];
long long fact[maxn];
pair<int, int> a[maxn];
int a0[maxn];
int b0[maxn];
map<pair<int, int>, int> mpfs;
map<int, int> mpf, mps;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  fact[0] = 1ll;
  for (long long i = 1ll; i < maxn; i++) {
    fact[i] = (fact[i - 1] * i) % mod;
  }
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
    a0[i] = a[i].first;
    b0[i] = a[i].second;
    arr[a0[i]]++;
    arr[b0[i]]++;
    mpf[a[i].first]++;
    mps[a[i].second]++;
    mpfs[{a[i].first, a[i].second}]++;
  }
  long long cnt1 = 1ll;
  long long cnt2 = 1ll;
  long long cnt12 = 1ll;
  for (auto i : mpf) cnt1 = (cnt1 * fact[i.second]) % mod;
  for (auto i : mps) cnt2 = (cnt2 * fact[i.second]) % mod;
  sort(a, a + n);
  long long answ;
  for (auto i : mpfs) cnt12 = (cnt12 * fact[i.second]) % mod;
  for (int i = 1; i < n; ++i)
    if (a[i - 1].second > a[i].second) cnt12 = 0;
  answ = fact[n] - cnt1 - cnt2 + cnt12;
  cout << (mod + (answ % mod)) % mod << endl;
  return 0;
}
