#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, a, b;
  cin >> n;
  pair<int, int> pairsFirst[n];
  pair<int, int> pairsSecond[n];
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    pairsFirst[i] = {a, b};
    pairsSecond[i] = {b, a};
  }
  sort(pairsFirst, pairsFirst + n);
  sort(pairsSecond, pairsSecond + n);
  long long fact[300005];
  fact[0] = 1;
  for (int i = 1; i < 300005; i++) fact[i] = (fact[i - 1] * i) % 998244353;
  long long a1, a2, aboth;
  int same;
  a1 = same = 1;
  for (int i = 1; i < n; i++) {
    if (pairsFirst[i].first == pairsFirst[i - 1].first)
      same++;
    else {
      a1 = (a1 * fact[same]) % 998244353;
      same = 1;
    }
  }
  a1 = (a1 * fact[same]) % 998244353;
  a2 = same = 1;
  for (int i = 1; i < n; i++) {
    if (pairsSecond[i].first == pairsSecond[i - 1].first)
      same++;
    else {
      a2 = (a2 * fact[same]) % 998244353;
      same = 1;
    }
  }
  a2 = (a2 * fact[same]) % 998244353;
  aboth = same = 1;
  for (int i = 1; i < n; i++) {
    if (pairsFirst[i] == pairsFirst[i - 1])
      same++;
    else {
      aboth = (aboth * fact[same]) % 998244353;
      same = 1;
    }
  }
  aboth = (aboth * fact[same]) % 998244353;
  bool sorted = true;
  for (int i = 1; i < n; i++)
    if (pairsFirst[i].first < pairsFirst[i - 1].first ||
        pairsFirst[i].second < pairsFirst[i - 1].second)
      sorted = false;
  aboth *= sorted;
  cout << (fact[n] - a1 - a2 + aboth + 4ll * 998244353) % 998244353;
  return 0;
}
