#include <bits/stdc++.h>
using namespace std;
const int N = 3 * 100009;
const int mod = 998244353;
long long m[N], m1[N], fa[N];
vector<pair<int, int>> v;
map<pair<int, int>, long long> mm;
int main() {
  int n;
  cin >> n;
  int x, y;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    m[x]++;
    m1[y]++;
    pair<int, int> z = {x, y};
    mm[z]++;
    v.push_back(z);
  }
  long long re = 1;
  long long re2 = 1;
  long long re1 = 1;
  fa[0] = 1;
  for (int i = 1; i < n + 3; i++) {
    fa[i] = fa[i - 1] * i;
    fa[i] %= mod;
  }
  for (int i = 0; i < n + 1; i++) {
    if (m[i] == 0) continue;
    re *= fa[m[i]];
    re %= mod;
  }
  for (int i = 0; i < n + 1; i++) {
    if (m1[i] == 0) continue;
    re1 *= fa[m1[i]];
    re1 %= mod;
  }
  sort(v.begin(), v.end());
  int u = 1;
  for (int i = 1; i < n; i++) {
    if (v[i - 1].second > v[i].second) u = 0;
  }
  if (u == 0)
    re2 = 0;
  else {
    for (auto it : mm) {
      re2 *= fa[mm[it.first]];
      re2 %= mod;
    }
  }
  re += re1;
  re %= mod;
  re = (re - re2 + mod + mod) % mod;
  long long ans = 1;
  for (int i = n; i > 0; i--) {
    ans *= i;
    ans %= mod;
  }
  cout << (ans - re + mod + mod) % mod;
}
