#include <bits/stdc++.h>
using namespace std;
long long mod = 998244353L;
int n;
vector<pair<long long, long long> > arr;
void swap_pairs() {
  for (int i = 0; i < n; i++) swap(arr[i].first, arr[i].second);
}
long long fact[1000000];
void calcule_factorial() {
  fact[0] = 1;
  for (long long i = 1; i < 1000000; i++) fact[i] = (i * fact[i - 1]) % mod;
}
long long count_bad_by_first_element() {
  sort(arr.begin(), arr.end());
  map<long long, long long> m;
  for (int i = 0; i < n; i++) {
    m[arr[i].first]++;
  }
  long long ret = 1;
  map<long long, long long>::iterator it = m.begin();
  while (it != m.end()) {
    pair<long long, long long> kv = *(it);
    ret = (ret * fact[kv.second]) % mod;
    it++;
  }
  return ret;
}
bool is_sorted_by_first_and_second_element() {
  for (int i = 1; i < n; i++) {
    if (arr[i - 1].second > arr[i].second) return false;
  }
  return true;
}
long long count_bad_by_first_and_second_element() {
  sort(arr.begin(), arr.end());
  if (!is_sorted_by_first_and_second_element()) {
    return 0;
  }
  map<pair<long long, long long>, long long> m;
  for (int i = 0; i < n; i++) {
    m[arr[i]]++;
  }
  long long ret = 1;
  map<pair<long long, long long>, long long>::iterator it = m.begin();
  while (it != m.end()) {
    pair<pair<long long, long long>, long long> kv = *(it);
    ret = (ret * fact[kv.second]) % mod;
    it++;
  }
  return ret;
}
void solve_test_case() {
  calcule_factorial();
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    arr.push_back(make_pair(a, b));
  }
  long long bad_by_first_element = count_bad_by_first_element();
  swap_pairs();
  long long bad_by_second_element = count_bad_by_first_element();
  swap_pairs();
  long long bad_by_first_and_second_element =
      count_bad_by_first_and_second_element();
  long long ans = (fact[n] - bad_by_first_element + mod) % mod;
  ans = (ans - bad_by_second_element + mod) % mod;
  ans = (ans + bad_by_first_and_second_element) % mod;
  cout << ans << '\n';
}
void solve_test_cases() { solve_test_case(); }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve_test_cases();
  return 0;
}
