#include <bits/stdc++.h>
using namespace std;
const long long p = 998244353;
const long long N = 3e5;
long long fact[N + 1];
int32_t main() {
  clock_t clk = clock();
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  fact[0] = 1;
  for (long long i = 1; i <= N; i++) {
    fact[i] = (fact[i - 1] * i) % p;
  }
  long long n;
  cin >> n;
  vector<pair<long long, long long> > a(n);
  for (auto &it : a) cin >> it.first >> it.second;
  map<long long, vector<long long> > byA;
  map<long long, vector<long long> > byB;
  long long ans = 0;
  long long temp;
  for (pair<long long, long long> pa : a) {
    byA[pa.first].push_back(pa.second);
  }
  temp = 1;
  for (auto pa : byA) {
    temp = (temp * fact[pa.second.size()]) % p;
  }
  ans = (ans + temp) % p;
  for (pair<long long, long long> pa : a) {
    byB[pa.second].push_back(pa.first);
  }
  temp = 1;
  for (auto pa : byB) {
    temp = (temp * fact[pa.second.size()]) % p;
  }
  ans = (ans + temp) % p;
  bool isPossible = 1;
  for (auto it = byA.begin(); it != byA.end(); it++) {
    sort(it->second.begin(), it->second.end());
    if (it == byA.begin()) continue;
    auto it2 = it;
    --it2;
    long long v1 = it2->second.back();
    long long v2 = it->second[0];
    if (v2 < v1) {
      isPossible = 0;
      break;
    }
  }
  temp = 0;
  if (isPossible) {
    temp = 1;
    for (auto pa : byA) {
      long long m = pa.second.size();
      vector<long long> isLand;
      long long c = 1;
      long long flag = pa.second[0];
      for (long long i = 1; i < m; i++) {
        if (flag == pa.second[i])
          c++;
        else {
          isLand.push_back(c);
          c = 1;
          flag = pa.second[i];
        }
      }
      if (c) isLand.push_back(c);
      long long temp3 = 1;
      for (long long x : isLand) {
        temp3 = (temp3 * fact[x]) % p;
      }
      temp = (temp * temp3) % p;
    }
    ans = (ans + p - temp) % p;
  }
  long long total = fact[n];
  total = (total + p - ans) % p;
  cout << total;
  return 0;
}
