#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
const long long MOD = 998244353;
pair<int, int> seq[MAXN];
long long fac[MAXN];
int n;
int read() {
  int s = 1, x = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') s = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = 10 * x + ch - '0';
    ch = getchar();
  }
  return x * s;
}
void init() {
  fac[0] = 1;
  for (int i = 1; i <= n; ++i) fac[i] = fac[i - 1] * i % MOD;
}
bool cmp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) seq[i].first = read(), seq[i].second = read();
  init();
  long long cnt2 = 1, tmp = 1;
  sort(seq + 1, seq + n + 1, cmp);
  for (int i = 2; i <= n; ++i)
    if (seq[i].second == seq[i - 1].second)
      tmp++;
    else
      cnt2 = cnt2 * fac[tmp] % MOD, tmp = 1;
  cnt2 = cnt2 * fac[tmp] % MOD;
  long long cnt1 = 1;
  tmp = 1;
  sort(seq + 1, seq + 1 + n);
  for (int i = 2; i <= n; ++i)
    if (seq[i].first == seq[i - 1].first)
      tmp++;
    else
      cnt1 = cnt1 * fac[tmp] % MOD, tmp = 1;
  cnt1 = cnt1 * fac[tmp] % MOD;
  long long cnt12 = 1;
  tmp = 1;
  bool flag = true;
  for (int i = 2; i <= n && flag; ++i) {
    if (seq[i].second < seq[i - 1].second)
      flag = false;
    else if (seq[i].first == seq[i - 1].first &&
             seq[i].second == seq[i - 1].second)
      tmp++;
    else
      cnt12 = cnt12 * fac[tmp] % MOD, tmp = 1;
  }
  cnt12 = cnt12 * fac[tmp] % MOD;
  if (!flag) cnt12 = 0;
  cout << (fac[n] - cnt1 - cnt2 + cnt12 + MOD * 2) % MOD << endl;
  return 0;
}
