#include <bits/stdc++.h>
using namespace std;
long long int arr1[300005] = {0}, arr2[300005] = {0}, factorial[300005];
void solve() {
  long long int i;
  factorial[0] = 1;
  for (i = 1; i < 300005; i++)
    factorial[i] = (factorial[i - 1] * i) % 998244353;
}
int main() {
  long long int n, i, a, b;
  cin >> n;
  if (n == 1)
    cout << 0;
  else {
    solve();
    vector<pair<long long int, long long int> > v;
    for (i = 0; i < n; i++) {
      cin >> a >> b;
      arr1[a]++;
      arr2[b]++;
      v.push_back(make_pair(a, b));
    }
    long long int ans1 = 1, ans2 = 1, count = 1, flag = 0;
    for (i = 0; i < 300005; i++) ans1 = (ans1 * factorial[arr1[i]]) % 998244353;
    for (i = 0; i < 300005; i++) ans2 = (ans2 * factorial[arr2[i]]) % 998244353;
    sort(v.begin(), v.end());
    ans1 = (ans1 + ans2) % 998244353;
    ans2 = 1;
    for (i = 1; i < n; i++) {
      if (v[i].second < v[i - 1].second) {
        flag = 1;
        break;
      }
      if (v[i].first == v[i - 1].first && v[i].second == v[i - 1].second)
        count++;
      else {
        ans2 = (ans2 * (factorial[count])) % 998244353;
        count = 1;
      }
    }
    ans2 = (ans2 * (factorial[count])) % 998244353;
    if (flag == 0) ans1 = (ans1 - ans2 + 998244353) % 998244353;
    ans1 = (factorial[n] - ans1 + 998244353) % 998244353;
    cout << ans1;
  }
}
