#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  vector<long long> fact(n + 1, 1);
  for (long long i = 1; i <= n; ++i) {
    fact[i] = fact[i - 1] * i % mod;
  }
  vector<pair<long long, long long>> arr(n);
  vector<long long> cnts1(n + 1, 0), cnts2(n + 1, 0);
  for (long long i = 0; i < n; ++i) {
    cin >> arr[i].first >> arr[i].second;
    ++cnts1[arr[i].first];
    ++cnts2[arr[i].second];
  }
  long long ways1 = 1;
  long long ways2 = 1;
  for (long long i = 1; i <= n; ++i) {
    ways1 *= fact[cnts1[i]];
    ways1 %= mod;
    ways2 *= fact[cnts2[i]];
    ways2 %= mod;
  }
  sort(arr.begin(), arr.end());
  long long waysBoth = 1;
  bool possible = true;
  for (long long i = 1; i < n; ++i) {
    if (arr[i].second < arr[i - 1].second) possible = false;
  }
  if (possible) {
    map<pair<long long, long long>, long long> cntsBoth;
    for (auto &p : arr) ++cntsBoth[p];
    for (auto &p : cntsBoth) {
      waysBoth *= fact[p.second];
      waysBoth %= mod;
    }
  } else {
    waysBoth = 0;
  }
  long long res = fact[n] - ways1 - ways2 + waysBoth;
  res %= mod;
  res += mod;
  res %= mod;
  cout << res;
}
