#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
int n;
vector<int> nxt[300005];
pair<int, int> p[300005];
long long int fact[300005];
void gen() {
  fact[1] = 1;
  for (long long int i = 2; i <= n; i++) {
    fact[i] = (fact[i - 1] * i) % 998244353;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  gen();
  map<int, int> freq1, freq2;
  for (int i = 0; i < n; i++) {
    cin >> p[i].first >> p[i].second;
    freq1[p[i].first]++;
    freq2[p[i].second]++;
    nxt[p[i].first].push_back(p[i].second);
  }
  long long int ans = fact[n];
  long long int cur = 1ll;
  for (auto x : freq1) cur = (cur * fact[x.second]) % 998244353;
  ans = (998244353 + ans - cur) % 998244353;
  cur = 1ll;
  for (auto x : freq2) cur = (cur * fact[x.second]) % 998244353;
  ans = (998244353 + ans - cur) % 998244353;
  long long int prev = 0;
  long long int add = 1;
  bool prob = false;
  for (int i = 1; i <= n && !prob; i++) {
    map<int, int> mp;
    sort(nxt[i].begin(), nxt[i].end());
    for (auto x : nxt[i]) {
      if (prev > x) prob = true;
      prev = x;
      mp[x]++;
    }
    for (auto x : mp) {
      add = (add * fact[x.second]) % 998244353;
    }
  }
  if (prob) add = 0;
  ans = (ans + add) % 998244353;
  cout << ans;
  return 0;
}
