#include <bits/stdc++.h>
using namespace std;
struct S {
  int a, b;
  S() {}
  S(int _a, int _b) {
    a = _a;
    b = _b;
  }
  const bool operator<(const S &o) const { return a < o.a; }
};
string exm;
inline void exf(void) {
  cout << exm << "\n";
  exit(0);
}
template <typename T>
inline void showAll(vector<T> &v, string sep = "") {
  for (T &here : v) cout << here << sep;
}
template <typename T>
inline void showAll(T arr[], int st, int end, string sep = "") {
  for (int i = st; i <= end; i++) cout << arr[i] << sep;
}
template <typename T>
inline vector<int> int_seperation(T N, int d = 10) {
  vector<int> v;
  while (N) {
    v.push_back(N % d);
    N /= d;
  }
  reverse(v.begin(), v.end());
  return v;
}
const int SIZE = 300009;
const int MOD = 998244353;
pair<int, int> arr[SIZE];
map<int, int> a, b;
map<pair<int, int>, int> c;
long long fact[SIZE];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &arr[i].first, &arr[i].second);
    a[arr[i].first]++;
    b[arr[i].second]++;
    c[{arr[i].first, arr[i].second}]++;
  }
  fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
  }
  sort(arr + 1, arr + 1 + n);
  long long ans = fact[n];
  long long sub_a, sub_b, sub_c;
  sub_a = sub_b = sub_c = 1;
  for (auto here : a) {
    int sec = here.second;
    sub_a = sub_a * fact[sec] % MOD;
  }
  for (auto here : b) {
    int sec = here.second;
    sub_b = sub_b * fact[sec] % MOD;
  }
  for (auto here : c) {
    int sec = here.second;
    sub_c = sub_c * fact[sec] % MOD;
  }
  for (int i = 1; i <= n - 1; i++) {
    if (arr[i].second > arr[i + 1].second) sub_c = 0;
  }
  ans = (ans - sub_a - sub_b + sub_c);
  while (ans < 0) {
    ans += MOD;
  }
  ans %= MOD;
  printf("%lld\n", ans);
  return 0;
}
