#include <bits/stdc++.h>
using namespace std;
long long mod = 998244353;
long long perm(int a) {
  long long ret = 1;
  for (int i = 1; i <= a; i++) {
    ret *= i;
    ret %= mod;
  }
  return ret;
}
int main() {
  int n;
  long long rez = 1, cr = 1, rc = 1, cc = 1;
  cin >> n;
  rez = perm(n);
  vector<pair<int, int> > a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
  }
  sort(a.begin(), a.end());
  for (int i = 0, j = 0; i < n; i = j) {
    for (j = i; j < n && a[i].first == a[j].first; j++)
      ;
    int sz = j - i;
    cr *= perm(sz);
    cr %= mod;
  }
  for (int i = 0; i < n; i++) {
    swap(a[i].first, a[i].second);
  }
  sort(a.begin(), a.end());
  for (int i = 0, j = 0; i < n; i = j) {
    for (j = i; j < n && a[i].first == a[j].first; j++)
      ;
    int sz = j - i;
    rc *= perm(sz);
    rc %= mod;
  }
  for (int i = 0, j = 0; i < n; i = j) {
    for (j = i; j < n && a[i] == a[j]; j++)
      ;
    int sz = j - i;
    cc *= perm(sz);
    cc %= mod;
  }
  for (int i = 1; i < n; i++) {
    if (a[i].second < a[i - 1].second) cc = 0;
  }
  rez = (rez - cr - rc + cc) % mod;
  rez += mod;
  rez %= mod;
  cout << rez;
}
