#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
pair<int, int> a[300010];
long long f[300010];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n;
  cin >> n;
  f[0] = 1;
  for (int i = 1; i <= n; i++) {
    f[i] = i * f[i - 1];
    f[i] %= mod;
  }
  long long ans = f[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
  }
  sort(a, a + n);
  int x = 0, cnt = 0;
  long long temp = 1;
  for (int i = 0; i < n; i++) {
    if (a[i].first != x) {
      temp = temp * f[cnt];
      temp %= mod;
      x = a[i].first;
      cnt = 1;
    } else
      cnt++;
  }
  temp = (temp * f[cnt]) % mod;
  ans = (ans - temp + mod) % mod;
  for (int i = 0; i < n; i++) {
    swap(a[i].first, a[i].second);
  }
  sort(a, a + n);
  temp = 1;
  x = 0;
  cnt = 0;
  for (int i = 0; i < n; i++) {
    if (a[i].first != x) {
      temp = temp * f[cnt];
      temp %= mod;
      x = a[i].first;
      cnt = 1;
    } else
      cnt++;
  }
  temp = (temp * f[cnt]) % mod;
  ans = (ans - temp) % mod;
  for (int i = 0; i < n; i++) {
    swap(a[i].first, a[i].second);
  }
  sort(a, a + n);
  bool srt = 1;
  for (int i = 1; i < n; i++) {
    if (a[i].second < a[i - 1].second) srt = 0;
  }
  if (srt == 0) return cout << (ans + mod) % mod, 0;
  temp = 1;
  int l = 0;
  while (l < n) {
    int r = l + 1;
    while (a[l] == a[r]) r++;
    temp = temp * f[r - l];
    temp %= mod;
    l = r;
  }
  ans = ans + temp;
  cout << (ans + mod) % mod;
  return 0;
}
