#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
bool comp1(const pair<int, int> p1, const pair<int, int> p2) {
  return p1.first < p2.first || (p1.first == p2.first && p1.second < p2.second);
}
bool comp2(const pair<int, int> p1, const pair<int, int> p2) {
  return p1.second < p2.second ||
         (p1.second == p2.second && p1.first < p2.first);
}
int main() {
  int n;
  scanf("%d", &n);
  pair<int, int> t[n];
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    t[i] = make_pair(x, y);
  }
  long long fact[n + 1];
  fact[0] = 1;
  for (int i = 0; i < n; i++) fact[i + 1] = (fact[i] * (i + 1)) % MOD;
  sort(t, t + n, comp1);
  bool sorted = true;
  for (int i = 0; i < n - 1; i++) {
    if (t[i].second > t[i + 1].second) sorted = false;
  }
  long long res = fact[n];
  long long inter = 0;
  if (sorted) inter = 1;
  long long sup = 1;
  int id = 0;
  while (id < n) {
    int id_deb = id;
    while (id < n - 1 && t[id + 1].first == t[id].first) {
      int idBis = id;
      while (id < n - 1 && t[id + 1].first == t[id].first &&
             t[id + 1].second == t[id].second)
        id++;
      inter *= fact[id + 1 - idBis];
      inter %= MOD;
      if (id < n - 1 && t[id + 1].first == t[id].first) id++;
    }
    sup *= fact[id + 1 - id_deb];
    sup %= MOD;
    id++;
  }
  res -= sup;
  res += inter;
  res %= MOD;
  sort(t, t + n, comp2);
  sup = 1;
  id = 0;
  while (id < n) {
    int id_deb = id;
    while (id < n - 1 && t[id + 1].second == t[id].second) {
      id++;
    }
    sup *= fact[id + 1 - id_deb];
    sup %= MOD;
    id++;
  }
  res -= sup;
  res %= MOD;
  if (res < 0) res += MOD;
  cout << res << endl;
  return 0;
}
