#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
struct pt {
  int a, b;
};
bool cmp1(pt a, pt b) { return a.a < b.a; }
bool cmp2(pt a, pt b) { return a.b < b.b; }
bool cmp12(pt a, pt b) { return a.a < b.a && a.b < b.b; }
long long fact(int n) {
  long long res = 1;
  for (int i = 2; i <= n; ++i) res = res * i % mod;
  return res;
}
int main() {
  int n, cnt;
  cin >> n;
  vector<pt> s(n);
  for (int i = 0; i < n; ++i) cin >> s[i].a >> s[i].b;
  sort(s.begin(), s.end(), &cmp1);
  long long ans1 = 1;
  cnt = 1;
  for (int i = 1; i < n; ++i) {
    if (s[i].a != s[i - 1].a) {
      ans1 = ans1 * fact(cnt) % mod;
      cnt = 1;
    } else
      ++cnt;
  }
  ans1 = ans1 * fact(cnt) % mod;
  sort(s.begin(), s.end(), &cmp2);
  long long ans2 = 1;
  cnt = 1;
  for (int i = 1; i < n; ++i) {
    if (s[i].b != s[i - 1].b) {
      ans2 = ans2 * fact(cnt) % mod;
      cnt = 1;
    } else
      ++cnt;
  }
  ans2 = ans2 * fact(cnt) % mod;
  sort(s.begin(), s.end(), &cmp12);
  long long ans12 = 1;
  for (int i = 1; i < n; ++i)
    ans12 *= (s[i].a >= s[i - 1].a && s[i].b >= s[i - 1].b);
  if (ans12) {
    cnt = 1;
    for (int i = 1; i < n; ++i) {
      if (s[i].a != s[i - 1].a || s[i].b != s[i - 1].b) {
        ans12 = ans12 * fact(cnt) % mod;
        cnt = 1;
      } else
        ++cnt;
    }
    ans12 = ans12 * fact(cnt) % mod;
  }
  long long ans = fact(n);
  cout << (((ans - ans1 + mod) % mod - ans2 + mod) % mod + ans12) % mod;
  return 0;
}
