#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 1;
const long long LINF = 1e17;
const int MOD = 998244353;
const int MAKS = 400100;
int n;
bool boo;
long long fak[MAKS], ans[3], cnt[2][MAKS], cntt;
vector<long long> lst[MAKS];
pair<int, int> p[MAKS];
int main() {
  scanf("%d", &n);
  fak[0] = 1;
  for (int i = 1; i <= n; i++) {
    fak[i] = fak[i - 1] * i;
    fak[i] %= MOD;
  }
  for (int i = 0; i < 3; i++) ans[i] = 1;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &p[i].first, &p[i].second);
    cnt[0][p[i].first]++;
    cnt[1][p[i].second]++;
    lst[p[i].first].push_back(p[i].second);
  }
  for (int j = 0; j < 2; j++) {
    for (int i = 1; i <= n; i++) {
      ans[j] *= fak[cnt[j][i]];
      ans[j] %= MOD;
    }
  }
  long long last = 0;
  for (int i = 1; i <= n; i++) {
    if (lst[i].size() == 0) continue;
    sort(lst[i].begin(), lst[i].end());
    if (boo && lst[i].front() < last) {
      ans[2] = 0;
      break;
    }
    boo = true;
    cntt = 0;
    for (int j = 0; j < lst[i].size(); j++) {
      if (j == 0 || lst[i][j] == lst[i][j - 1])
        cntt++;
      else {
        ans[2] *= fak[cntt];
        ans[2] %= MOD;
        cntt = 1;
      }
    }
    ans[2] *= fak[cntt];
    ans[2] %= MOD;
    last = lst[i].back();
  }
  long long temp = ans[0] + ans[1];
  temp -= ans[2];
  temp = fak[n] - temp;
  while (temp < 0) temp += MOD;
  temp %= MOD;
  printf("%lld\n", temp);
}
