#include <bits/stdc++.h>
using namespace std;
bool compare(const pair<int, int>& i, const pair<int, int>& j) {
  if (i.first != j.first) {
    return i.first < j.first;
  } else {
    return i.second < j.second;
  }
}
int main() {
  int n;
  const long long p = 998244353;
  cin >> n;
  pair<int, int> pairs[n];
  pair<int, int> tmp;
  for (int i = 0; i < n; i++) {
    cin >> tmp.first >> tmp.second;
    pairs[i] = tmp;
  }
  sort(pairs, pairs + n, compare);
  long long ans = 0;
  vector<int> sizes;
  vector<int> sizes2;
  vector<int> intersectSizes;
  long long factorials[n + 1];
  factorials[0] = 1;
  for (int i = 1; i <= n; i++) {
    factorials[i] = (factorials[i - 1] * i) % p;
  }
  int curr = 1;
  int currIntersect = 1;
  bool isThereIntersect = true;
  for (int i = 1; i < n; i++) {
    if (pairs[i].first == pairs[i - 1].first) {
      curr++;
    } else if (curr > 1) {
      sizes.push_back(curr);
      curr = 1;
    }
    if (pairs[i].second < pairs[i - 1].second) {
      isThereIntersect = false;
    }
    if (pairs[i].first == pairs[i - 1].first &&
        pairs[i].second == pairs[i - 1].second) {
      currIntersect++;
    } else if (currIntersect > 1) {
      intersectSizes.push_back(currIntersect);
      currIntersect = 1;
    }
  }
  sizes.push_back(curr);
  intersectSizes.push_back(currIntersect);
  int temp;
  for (int i = 0; i < n; i++) {
    temp = pairs[i].first;
    pairs[i].first = pairs[i].second;
    pairs[i].second = temp;
  }
  sort(pairs, pairs + n, compare);
  curr = 1;
  for (int i = 1; i < n; i++) {
    if (pairs[i].first == pairs[i - 1].first) {
      curr++;
    } else if (curr > 1) {
      sizes2.push_back(curr);
      curr = 1;
    }
  }
  sizes2.push_back(curr);
  long long first = 1;
  long long second = 1;
  long long intersect = 1;
  for (int i = 0; i < sizes.size(); i++) {
    first = (first * factorials[sizes[i]]) % p;
  }
  for (int i = 0; i < sizes2.size(); i++) {
    second = (second * factorials[sizes2[i]]) % p;
  }
  for (int i = 0; i < intersectSizes.size(); i++) {
    intersect = (intersect * factorials[intersectSizes[i]]) % p;
  }
  if (isThereIntersect == false) {
    intersect = 0;
  }
  ans = ((factorials[n] - ((first + second) % p - intersect) % p) % p + p) % p;
  cout << ans;
}
