#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > vec;
unordered_map<long long, int> mp;
unordered_map<long long, int>::iterator it;
long long p[300100];
int main() {
  p[0] = 1;
  for (long long i = 1; i <= 300000; i++) p[i] = (p[i - 1] * i) % 998244353;
  int n;
  int a, b;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &a, &b);
    vec.push_back({a, b});
  }
  sort(vec.begin(), vec.end());
  long long ans;
  a = 1;
  for (auto cur : vec) mp[cur.first]++;
  it = mp.begin();
  while (it != mp.end()) {
    a = (a * p[it->second]) % 998244353;
    it++;
  }
  ans = a;
  a = 1;
  mp.clear();
  for (auto cur : vec) mp[cur.second]++;
  it = mp.begin();
  while (it != mp.end()) {
    a = (a * p[it->second]) % 998244353;
    it++;
  }
  ans = (ans + a) % 998244353;
  a = 1;
  mp.clear();
  for (auto cur : vec) {
    long long x = ((1ll * cur.first) << 24) | cur.second;
    mp[x]++;
  }
  it = mp.begin();
  while (it != mp.end()) {
    a = (a * p[it->second]) % 998244353;
    it++;
  }
  for (int i = 1; i < n; i++)
    if (vec[i - 1].second > vec[i].second) a = 0;
  ans = (ans - a + 998244353) % 998244353;
  ans = (p[n] - ans + 998244353) % 998244353;
  cout << ans << endl;
  return 0;
}
