#include <bits/stdc++.h>
using namespace std;
int N;
signed long long mo = 998244353;
signed long long fact[303030];
void solve() {
  int i, j, k, l, r, x, y;
  string s;
  fact[0] = 1;
  for (i = 1; i <= 300000; i++) fact[i] = fact[i - 1] * i % mo;
  cin >> N;
  map<int, int> M[2];
  map<pair<int, int>, int> L;
  for (i = 0; i < (N); i++) {
    cin >> x >> y;
    M[0][x]++;
    M[1][y]++;
    L[{x, y}]++;
  }
  signed long long A = 1, B = 1, C = 1;
  for (auto& m : M[0]) A = A * fact[m.second] % mo;
  for (auto& m : M[1]) B = B * fact[m.second] % mo;
  pair<int, int> p = {-1, -1};
  for (auto& m : L) {
    if (m.first.first < p.first || m.first.second < p.second) C = 0;
    p = m.first;
    C = C * fact[m.second] % mo;
  }
  cout << (fact[N] + C - A - B + 2 * mo) % mo << endl;
}
int main(int argc, char** argv) {
  string s;
  int i;
  if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
  for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
  for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}
