#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int N = 3e5 + 10;
inline int add(int a, int b) {
  a += b;
  return a >= mod ? a - mod : a;
}
inline int sub(int a, int b) {
  a -= b;
  return a < 0 ? a + mod : a;
}
inline int mul(long long a, int b) { return a * b % mod; }
int cnta[N], cntb[N], cnt;
pair<int, int> a[N];
bool cmp(pair<int, int> a, pair<int, int> b) {
  if (a.first == b.first)
    return a.second < b.second;
  else
    return a.first < b.first;
}
int main(void) {
  int n;
  scanf("%d", &n);
  int f1 = 1, f2 = 1, f3 = 1, ans = 1;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i].first, &a[i].second);
    f1 = mul(f1, ++cnta[a[i].first]);
    f2 = mul(f2, ++cntb[a[i].second]);
    ans = mul(ans, i);
  }
  sort(a + 1, a + n + 1);
  cnt = 0;
  bool ok = 1;
  for (int i = 1; i <= n; i++) {
    if (i > 1 && a[i].second == a[i - 1].second &&
        a[i].first == a[i - 1].first) {
      f3 = mul(f3, ++cnt);
    } else {
      cnt = 1;
    }
  }
  for (int i = 2; i <= n; i++) {
    if (a[i].second < a[i - 1].second) ok = 0;
  }
  ans = sub(ans, f1);
  ans = sub(ans, f2);
  if (ok) {
    ans = add(ans, f3);
  }
  printf("%d\n", ans);
}
