#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
long long factorial(long long k) {
  long long ans = 1;
  for (long long i = 1; i <= k; i++) ans = ans * i % mod;
  return ans;
}
int main() {
  int n;
  cin >> n;
  vector<pair<long long, long long> > a(n);
  vector<long long> b(n);
  vector<long long> c(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
    b[i] = a[i].first;
    c[i] = a[i].second;
  }
  sort(a.begin(), a.begin() + n);
  sort(b.begin(), b.begin() + n);
  sort(c.begin(), c.begin() + n);
  long long num1 = 1, num2 = 1, num3 = 1, counter = 1, flag = 1;
  for (int i = 0; i < n - 1; i++)
    if (b[i + 1] == b[i]) {
      counter++;
    } else if (counter > 1) {
      num1 = num1 * factorial(counter) % mod;
      counter = 1;
    }
  num1 = num1 * factorial(counter) % mod;
  counter = 1;
  for (int i = 0; i < n - 1; i++)
    if (c[i + 1] == c[i])
      counter++;
    else if (counter > 1) {
      num2 = num2 * factorial(counter) % mod;
      counter = 1;
    }
  num2 = num2 * factorial(counter) % mod;
  counter = 1;
  for (int i = 0; i < n - 1; i++)
    if (a[i + 1].second < a[i].second) flag = 0;
  if (flag) {
    for (int i = 0; i < n - 1; i++)
      if (a[i + 1].first == a[i].first && a[i + 1].second == a[i].second)
        counter++;
      else if (counter > 1) {
        num3 = num3 * factorial(counter) % mod;
        counter = 1;
      }
    num3 = num3 * factorial(counter) % mod;
  } else
    num3 = 0;
  cout << ((factorial(n) - num1 - num2 + num3) % mod + mod) % mod << endl;
  return 0;
}
