#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<vector<long long int>> v(n, vector<long long int>(2, 0));
  map<long long int, long long int> mp1, mp2;
  map<pair<long long int, long long int>, long long int> mp3;
  for (int i = 0; i < n; i++) {
    cin >> v[i][0] >> v[i][1];
    mp1[v[i][0]]++;
    mp2[v[i][1]]++;
    mp3[make_pair(v[i][0], v[i][1])]++;
  }
  vector<long long int> fac(3 * 100001, 0);
  fac[0] = 1;
  for (int i = 1; i <= 3 * 100000 + 1; i++) {
    fac[i] = (fac[i - 1] * i) % 998244353;
  }
  int ans = 1;
  for (auto i = mp1.begin(); i != mp1.end(); i++) {
    ans = (ans * fac[i->second]) % 998244353;
  }
  int ans2 = 1;
  for (auto i = mp2.begin(); i != mp2.end(); i++) {
    ans2 = (ans2 * fac[i->second]) % 998244353;
  }
  int ans3 = 1;
  pair<long long int, long long int> temp;
  int x = 0;
  for (auto i = mp3.begin(); i != mp3.end(); i++) {
    temp = i->first;
    if (i->first.second >= x)
      ans3 = (ans3 * fac[i->second]) % 998244353;
    else {
      ans3 = 0;
      break;
    }
    x = temp.second;
  }
  cout << ((fac[n] - (ans + ans2 - ans3) + 998244353) % 998244353 + 998244353) %
              998244353
       << endl;
  return 0;
}
