#include <bits/stdc++.h>
using namespace std;
#define FOR(i, x, n) for(ll i = x; i < n; i++)
#define pb push_back
#define ll long long
#define hii cout << "hii" << endl
#define pii pair<int, int>
#define pll pair<ll, ll>
#define int ll
#define mpp make_pair
#define endl '\n'
#define ff first
#define ss second
#define all(s) s.begin(), s.end()
#define fuck_you_scanf ios_base:: sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define si size()

const int N = 1e6 + 5;
pii arr[N];
pii brr[N];
int fact[N];
const int mod = 998244353;

void precompute()
{
    fact[0] = 1;
    for(int i = 1; i < N; i++)
    {
        fact[i] = fact[i - 1] * i;
        fact[i] %= mod;
    }
}

int32_t main()
{
    precompute();
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        int a, b;
        cin >> a >> b;
        arr[i].ff = a;
        arr[i].ss = b;
        brr[i].ff = a;
        brr[i].ss = b;
    }
    sort(arr, arr + n, [](pii a, pii b) -> bool{return a.ff < b.ff;});
    sort(brr, brr + n, [](pii a, pii b) -> bool{return a.ss < b.ss;});
    int ret = fact[n];
    int cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and arr[i].ff == arr[j].ff)
        {
            i++;
        }
        cnt *= fact[i - j];
        cnt %= mod;
    }
    // cerr << cnt << endl;
    ret -= cnt;
    ret += mod;
    ret %= mod;
    cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and brr[i].ss == brr[j].ss)
        {
            i++;
        }
        cnt *= fact[i - j];
        cnt %= mod;
    }

    // cerr << cnt << endl;

    ret -= cnt;
    ret += mod;
    ret %= mod;
    for(int i = 1; i < n; i++)
    {
        if(arr[i - 1].ff > arr[i].ff or arr[i - 1].ss > arr[i].ss)
        {
            cout << ret << endl;
            return 0;
        }
    }
    cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and arr[j].ff == arr[i].ff and arr[j].ss == arr[i].ss)
        {
            i++;
        }
        cnt *= fact[i - j];
        cnt %= mod;
    }
    ret += cnt;
    ret %= mod;
    cout << ret << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i, x, n) for(ll i = x; i < n; i++)
#define pb push_back
#define ll long long
#define hii cout << "hii" << endl
#define pii pair<int, int>
#define pll pair<ll, ll>
#define int ll
#define mpp make_pair
#define endl '\n'
#define ff first
#define ss second
#define all(s) s.begin(), s.end()
#define fuck_you_scanf ios_base:: sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define si size()

const int N = 1e6 + 5;
pii arr[N];
pii brr[N];
int fact[N];
const int mod = 998244353;

void precompute()
{
    fact[0] = 1;
    for(int i = 1; i < N; i++)
    {
        fact[i] = fact[i - 1] * i;
        fact[i] %= mod;
    }
}

int32_t main()
{
    precompute();
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        int a, b;
        cin >> a >> b;
        arr[i].ff = a;
        arr[i].ss = b;
        brr[i].ff = a;
        brr[i].ss = b;
    }
    sort(arr, arr + n, [&](pii & a, pii & b) -> bool{return a.ff < b.ff;});
    sort(brr, brr + n, [&](pii & a, pii & b) -> bool{return a.ss < b.ss;});
    int ret = fact[n];
    int cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and arr[i].ff == arr[j].ff)
        {
            i++;
        }
        cnt = (cnt * fact[i - j]) % mod;
    }
    ret = (ret - cnt + mod) % mod;
    cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and brr[i].ss == brr[j].ss)
        {
            i++;
        }
        cnt = (cnt * fact[i - j]) % mod;
    }

    ret = (ret - cnt + mod) % mod;
    for(int i = 1; i < n; i++)
    {
        if(brr[i - 1].ff > brr[i].ff)
        {
            cout << ret << endl;
            return 0;
        }
    }
    cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and brr[j].ff == brr[i].ff and brr[j].ss == brr[i].ss)
        {
            i++;
        }
        cnt *= fact[i - j];
        cnt %= mod;
    }
    ret += cnt;
    ret %= mod;
    cout << ret << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i, x, n) for(ll i = x; i < n; i++)
#define pb push_back
#define ll long long
#define hii cout << "hii" << endl
#define pii pair<int, int>
#define pll pair<ll, ll>
#define int ll
#define mpp make_pair
#define endl '\n'
#define ff first
#define ss second
#define all(s) s.begin(), s.end()
#define fuck_you_scanf ios_base:: sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define si size()

const int N = 1e6 + 5;
pii arr[N];
pii brr[N];
int fact[N];
const int mod = 998244353;

void precompute()
{
    fact[0] = 1;
    for(int i = 1; i < N; i++)
    {
        fact[i] = fact[i - 1] * i;
        fact[i] %= mod;
    }
}

int32_t main()
{
    precompute();
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        int a, b;
        cin >> a >> b;
        arr[i].ff = a;
        arr[i].ss = b;
        brr[i].ff = a;
        brr[i].ss = b;
    }
    sort(arr, arr + n, [](pii a, pii b) -> bool{return a.ff < b.ff;});
    sort(brr, brr + n, [](pii a, pii b) -> bool{return a.ss < b.ss;});
    int ret = fact[n];
    int cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and arr[i].ff == arr[j].ff)
        {
            i++;
        }
        cnt *= fact[i - j];
        cnt %= mod;
    }
    // cerr << cnt << endl;
    ret -= cnt;
    ret += mod;
    ret %= mod;
    cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and brr[i].ss == brr[j].ss)
        {
            i++;
        }
        cnt *= fact[i - j];
        cnt %= mod;
    }

    // cerr << cnt << endl;

    ret -= cnt;
    ret += mod;
    ret %= mod;
    for(int i = 1; i < n; i++)
    {
        if(brr[i - 1].ff > brr[i].ff or brr[i - 1].ss > brr[i].ss)
        {
            if(n == 300000) hii;
            cout << ret << endl;
            return 0;
        }
    }
    cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and brr[j].ff == brr[i].ff and brr[j].ss == brr[i].ss)
        {
            i++;
        }
        cnt *= fact[i - j];
        cnt %= mod;
    }
    // cerr << cnt << endl;
    ret += cnt;
    ret %= mod;
    cout << ret << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i, x, n) for(ll i = x; i < n; i++)
#define pb push_back
#define ll long long
#define hii cout << "hii" << endl
#define pii pair<int, int>
#define pll pair<ll, ll>
#define int ll
#define mpp make_pair
#define endl '\n'
#define ff first
#define ss second
#define all(s) s.begin(), s.end()
#define fuck_you_scanf ios_base:: sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define si size()

const int N = 1e6 + 5;
pii arr[N];
pii brr[N];
int fact[N];
const int mod = 998244353;

void precompute()
{
    fact[0] = 1;
    for(int i = 1; i < N; i++)
    {
        fact[i] = fact[i - 1] * i;
        fact[i] %= mod;
    }
}

int32_t main()
{
    precompute();
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        int a, b;
        cin >> a >> b;
        arr[i].ff = a;
        arr[i].ss = b;
        brr[i].ff = a;
        brr[i].ss = b;
    }
    sort(arr, arr + n, [&](pii & a, pii & b) -> bool{return a.ff < b.ff;});
    sort(brr, brr + n, [&](pii & a, pii & b) -> bool{return a.ss < b.ss;});
    int ret = fact[n];
    int a = 0, b = 0, c = 0;
    int cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and arr[i].ff == arr[j].ff)
        {
            i++;
        }
        cnt = (cnt * fact[i - j]) % mod;
    }
    a = cnt;
    ret = (ret - cnt + mod) % mod;
    cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and brr[i].ss == brr[j].ss)
        {
            i++;
        }
        cnt = (cnt * fact[i - j]) % mod;
    }
    b = cnt;
    if(n == 300000) cout << a << " " << b << endl;
    ret = (ret - cnt + mod) % mod;
    for(int i = 1; i < n; i++)
    {
        if(brr[i - 1].ff > brr[i].ff)
        {
            if(n == 300000) break;
            cout << ret << endl;
            return 0;
        }
    }
    cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and brr[j].ff == brr[i].ff and brr[j].ss == brr[i].ss)
        {
            i++;
        }
        cnt *= fact[i - j];
        cnt %= mod;
    }
    if(n == 300000) cout << cnt << endl;
    ret += cnt;
    ret %= mod;
    cout << ret << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i, x, n) for(ll i = x; i < n; i++)
#define pb push_back
#define ll long long
#define hii cout << "hii" << endl
#define pii pair<int, int>
#define pll pair<ll, ll>
#define int ll
#define mpp make_pair
#define endl '\n'
#define ff first
#define ss second
#define all(s) s.begin(), s.end()
#define fuck_you_scanf ios_base:: sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define si size()

const int N = 1e6 + 5;
pii arr[N];
pii brr[N];
int fact[N];
const int mod = 998244353;

void precompute()
{
    fact[0] = 1;
    for(int i = 1; i < N; i++)
    {
        fact[i] = fact[i - 1] * i;
        fact[i] %= mod;
    }
}

int32_t main()
{
    precompute();
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        int a, b;
        cin >> a >> b;
        arr[i].ff = a;
        arr[i].ss = b;
        brr[i].ff = a;
        brr[i].ss = b;
    }
    sort(arr, arr + n, [](pii a, pii b) -> bool{return a.ff < b.ff;});
    sort(brr, brr + n, [](pii a, pii b) -> bool{return a.ss < b.ss;});
    int ret = fact[n];
    int cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and arr[i].ff == arr[j].ff)
        {
            i++;
        }
        cnt *= fact[i - j];
        cnt %= mod;
    }
    // cerr << cnt << endl;
    ret -= cnt;
    ret += mod;
    ret %= mod;
    cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and brr[i].ss == brr[j].ss)
        {
            i++;
        }
        cnt *= fact[i - j];
        cnt %= mod;
    }

    // cerr << cnt << endl;

    ret -= cnt;
    ret += mod;
    ret %= mod;
    for(int i = 1; i < n; i++)
    {
        if(brr[i - 1].ff > brr[i].ff or brr[i - 1].ss > brr[i].ss)
        {
            if(n == 300000) break;
            cout << ret << endl;
            return 0;
        }
    }
    cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and brr[j].ff == brr[i].ff and brr[j].ss == brr[i].ss)
        {
            i++;
        }
        cnt *= fact[i - j];
        cnt %= mod;
    }
    // cerr << cnt << endl;
    ret += cnt;
    ret %= mod;
    cout << ret << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i, x, n) for(ll i = x; i < n; i++)
#define pb push_back
#define ll long long
#define hii cout << "hii" << endl
#define pii pair<int, int>
#define pll pair<ll, ll>
#define int ll
#define mpp make_pair
#define endl '\n'
#define ff first
#define ss second
#define all(s) s.begin(), s.end()
#define fuck_you_scanf ios_base:: sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define si size()

const int N = 1e6 + 5;
pii arr[N];
pii brr[N];
int fact[N];
const int mod = 998244353;

void precompute()
{
    fact[0] = 1;
    for(int i = 1; i < N; i++)
    {
        fact[i] = fact[i - 1] * i;
        fact[i] %= mod;
    }
}

int32_t main()
{
    precompute();
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        int a, b;
        cin >> a >> b;
        arr[i].ff = a;
        arr[i].ss = b;
        brr[i].ff = a;
        brr[i].ss = b;
    }
    sort(arr, arr + n, [&](pii & a, pii & b) -> bool{return a.ff < b.ff;});
    sort(brr, brr + n, [&](pii & a, pii & b) -> bool{return a.ss < b.ss;});
    int ret = fact[n];
    int cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and arr[i].ff == arr[j].ff)
        {
            i++;
        }
        cnt *= fact[i - j];
        cnt %= mod;
    }
    // cerr << cnt << endl;
    ret = (ret - cnt + mod) % mod;
    cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and brr[i].ss == brr[j].ss)
        {
            i++;
        }
        cnt *= fact[i - j];
        cnt %= mod;
    }

    ret = (ret - cnt + mod) % mod;
    for(int i = 1; i < n; i++)
    {
        if(brr[i - 1].ff > brr[i].ff)
        {
            cout << ret << endl;
            return 0;
        }
    }
    cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and brr[j].ff == brr[i].ff and brr[j].ss == brr[i].ss)
        {
            i++;
        }
        cnt *= fact[i - j];
        cnt %= mod;
    }
    ret += cnt;
    ret %= mod;
    cout << ret << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i, x, n) for(ll i = x; i < n; i++)
#define pb push_back
#define ll long long
#define hii cout << "hii" << endl
#define pii pair<int, int>
#define pll pair<ll, ll>
#define int ll
#define mpp make_pair
#define endl '\n'
#define ff first
#define ss second
#define all(s) s.begin(), s.end()
#define fuck_you_scanf ios_base:: sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define si size()

const int N = 1e6 + 5;
pii arr[N];
pii brr[N];
int fact[N];
const int mod = 998244353;

void precompute()
{
    fact[0] = 1;
    for(int i = 1; i < N; i++)
    {
        fact[i] = fact[i - 1] * i;
        fact[i] %= mod;
    }
}

int32_t main()
{
    precompute();
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        int a, b;
        cin >> a >> b;
        arr[i].ff = a;
        arr[i].ss = b;
        brr[i].ff = a;
        brr[i].ss = b;
    }
    sort(arr, arr + n);
    sort(brr, brr + n, [](pii a, pii b) -> bool{return a.ss < b.ss;});
    int ret = fact[n];
    int cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and arr[i].ff == arr[j].ff)
        {
            i++;
        }
        cnt *= fact[i - j];
        cnt %= mod;
    }
    cerr << cnt << endl;
    ret -= cnt;
    ret += mod;
    ret %= mod;
    cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and brr[i].ss == brr[j].ss)
        {
            i++;
        }
        cnt *= fact[i - j];
        cnt %= mod;
    }

    cerr << cnt << endl;

    ret -= cnt;
    ret += mod;
    ret %= mod;
    for(int i = 1; i < n; i++)
    {
        if(arr[i - 1].ff > arr[i].ff or arr[i - 1].ss > arr[i].ss)
        {
            cout << ret << endl;
            return 0;
        }
    }
    cnt = 1;
    for(int i = 0; i < n;)
    {
        int j = i;
        while(i < n and arr[j].ff == arr[i].ff and arr[j].ss == arr[i].ss)
        {
            i++;
        }
        cnt *= fact[i - j];
        cnt %= mod;
    }
    cerr << cnt << endl;
    ret += cnt;
    ret %= mod;
    cout << ret << endl;
}


#include <bits/stdc++.h>

using namespace std;
const int MOD = 998244353;
bool func(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.first<p2.first)||(p1.first==p2.first&&p1.second<=p2.second))
        return true;
    return false;
}
bool func2(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.second<p2.second)||(p1.second==p2.second&&p1.first<=p2.first))
        return true;
    return false;
}
unsigned long long int fact(int a)
{
    unsigned long long int ans = 1;
    while(a>1)
    {
        ans = ((ans*a)%MOD);
        a--;
    }
    return (ans%MOD);
}
int main()
{
    unsigned int n;
    cin>>n;
    vector<pair<int,int> > v(n);
    int res = fact(n);
    for(unsigned int i = 0;i<n;i++)
        cin>>v[i].first>>v[i].second;
    sort(v.begin(),v.end(),func);
    unsigned long long int n1 = 1,n2 = 1,n3 = 1;
    unsigned int i = 0;
    while(i<n)
    {
        unsigned int j = i+1;
        while(j<n&&v[i].first==v[j].first)
            j++;
        n1 = ((n1*fact(j-i))%MOD);
        i = j;
    }
    res = (res + MOD - n1)%MOD;
    sort(v.begin(),v.end(),func2);
    i = 0;
    while(i<n)
    {
        unsigned int j = i+1;
        while(j<n&&v[i].second==v[j].second)
            j++;
        n2 = ((n2*fact(j-i))%MOD);
        i = j;
    }
    i = 0;
    for(;i<n-1;i++)
    {
        if((v[i].first>v[i+1].first)||(v[i].second>v[i+1].second))
        {
            n3 = 0;
            break;
        }
    }
    i = 0;
    if(n3)
    {
        while(i<n)
        {
            unsigned int j = i+1;
            while(j<n&&v[i].second==v[j].second&&v[i].first==v[j].first)
                j++;
            n3 = ((n3*fact(j-i))%MOD);
            i = j;
        }
    }
    res = (res + n3)%MOD;
    res = (res + MOD -n2)%MOD;
    cout<<res<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
bool func(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.first<p2.first)||(p1.first==p2.first&&p1.second<=p2.second))
        return true;
    return false;
}
bool func2(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.second<p2.second)||(p1.second==p2.second&&p1.first<=p2.first))
        return true;
    return false;
}
unsigned long long int fact(int a)
{
    unsigned long long int ans = 1;
    while(a>1)
    {
        ans = ((ans*a)%998244353);
        a--;
    }
    return (ans%998244353);
}
int main()
{
    int n;
    cin>>n;
    vector<pair<int,int> > v(n);
    for(int i = 0;i<n;i++)
    {
        cin>>v[i].first>>v[i].second;
    }
    sort(v.begin(),v.end(),func);
    unsigned long long int n1 = 1,n2 = 1,n3 = 0;
    int i = 0;
    while(i<n)
    {
        int j = i+1;
        int count = 0;
        while(j<n&&v[i].first==v[j].first)
            j++;
        n1 = ((n1*fact(j-i))%998244353);
        i = j;
    }
    sort(v.begin(),v.end(),func2);
    i = 0;
    while(i<n)
    {
        int j = i+1;
        int count = 0;
        while(j<n&&v[i].second==v[j].second)
            j++;
        n2 = ((n2*fact(j-i))%998244353);
        i = j;
    }
    i = 0;
    for(int i = 0;i<n;i++)
    {
        if((v[i].first>v[i+1].first)||(v[i].second>v[i+1].second))
        {
            n3 = 1;
            break;
        }
    }
    if(n3)
    {
        while(i<n)
        {
            int j = i+1;
            int count = 0;
            while(j<n&&v[i].second==v[j].second&&v[i].first==v[j].first)
                j++;
            n3 = ((n3*fact(j-i))%998244353);
            i = j;
        }
    }
    cout<<((fact(n)-n1-n2+n3)%998244353)<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
bool func(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.first<p2.first)||(p1.first==p2.first&&p1.second<=p2.second))
        return true;
    return false;
}
bool func2(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.second<p2.second)||(p1.second==p2.second&&p1.first<=p2.first))
        return true;
    return false;
}
unsigned long long int fact(int a)
{
    unsigned long long int ans = 1;
    while(a>1)
    {
        ans = ((ans*a));
        a--;
    }
    return (ans);
}
int main()
{
    int n;
    cin>>n;
    vector<pair<int,int> > v(n);
    for(int i = 0;i<n;i++)
    {
        cin>>v[i].first>>v[i].second;
    }
    sort(v.begin(),v.end(),func);
    unsigned long long int n1 = 1,n2 = 1,n3 = 1;
    int i = 0;
    while(i<n)
    {
        int j = i+1;
        int count = 0;
        while(j<n&&v[i].first==v[j].first)
            j++;
        n1 = ((n1*fact(j-i)));
        i = j;
    }
    sort(v.begin(),v.end(),func2);
    i = 0;
    while(i<n)
    {
        int j = i+1;
        int count = 0;
        while(j<n&&v[i].second==v[j].second)
            j++;
        n2 = ((n2*fact(j-i)));
        i = j;
    }
    i = 0;
    for(int i = 0;i<n-1;i++)
    {
        if((v[i].first>v[i+1].first)||(v[i].second>v[i+1].second))
        {
            n3 = 0;
            break;
        }
    }
    if(n3)
    {
        while(i<n)
        {
            int j = i+1;
            int count = 0;
            while(j<n&&v[i].second==v[j].second&&v[i].first==v[j].first)
                j++;
            n3 = (n3*fact(j-i));
            i = j;
        }
    }
    cout<<((fact(n)-n1-n2+n3)%998244353)<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int MOD = 998244353;
bool func(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.first<p2.first)||(p1.first==p2.first&&p1.second<=p2.second))
        return true;
    return false;
}
bool func2(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.second<p2.second)||(p1.second==p2.second&&p1.first<=p2.first))
        return true;
    return false;
}
unsigned long long int fact(int a)
{
    unsigned long long int ans = 1;
    while(a>1)
    {
        ans = ((ans*a)%MOD);
        a--;
    }
    return (ans%MOD);
}
int main()
{
    int n;
    cin>>n;
    //cout<<sizeof(unsigned long long int)<<" ";
    vector<pair<int,int> > v(n);
    unsigned long long int res = fact(n);
        //cout<<res<<endl;

    for(int i = 0;i<n;i++)
    {
        cin>>v[i].first>>v[i].second;
    }
    sort(v.begin(),v.end(),func);
    unsigned long long int n1 = 1,n2 = 1,n3 = 1;
    int i = 0;
    while(i<n)
    {
        int j = i+1;
        while(j<n&&v[i].first==v[j].first)
            j++;
        n1 = ((n1*fact(j-i))%MOD);
        i = j;
    }
    res = (res + MOD - n1)%MOD;
    //cout<<res<<endl;
    sort(v.begin(),v.end(),func2);
    i = 0;
    while(i<n)
    {
        int j = i+1;
        while(j<n&&v[i].second==v[j].second)
            j++;
        n2 = ((n2*fact(j-i))%MOD);
        i = j;
    }
    i = 0;
    //cout<<res<<endl;
    for(int i = 0;i<n-1;i++)
    {
        if((v[i].first>v[i+1].first)||(v[i].second>v[i+1].second))
        {
            n3 = 0;
            break;
        }
    }
    if(n3)
    {
        while(i<n)
        {
            int j = i+1;
            while(j<n&&v[i].second==v[j].second&&v[i].first==v[j].first)
                j++;
            n3 = ((n3*fact(j-i))%MOD);
            i = j;
        }
    }
    res = (res + n3)%MOD;
    res = (res + MOD -n2)%MOD;
    cout<<res<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
bool func(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.first<p2.first)||(p1.first==p2.first&&p1.second<=p2.second))
        return true;
    return false;
}
bool func2(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.second<p2.second)||(p1.second==p2.second&&p1.first<=p2.first))
        return true;
    return false;
}
unsigned long long int fact(int a)
{
    unsigned long long int ans = 1;
    while(a>1)
    {
        ans = ((ans*a)%998244353);
        a--;
    }
    return (ans%998244353);
}
int main()
{
    int n;
    cin>>n;
    vector<pair<int,int> > v(n);
    for(int i = 0;i<n;i++)
    {
        cin>>v[i].first>>v[i].second;
    }
    sort(v.begin(),v.end(),func);
    unsigned long long int n1 = 1,n2 = 1,n3 = 1;
    int i = 0;
    while(i<n)
    {
        int j = i+1;
        int count = 0;
        while(j<n&&v[i].first==v[j].first)
            j++;
        n1 = ((n1*fact(j-i))%998244353);
        i = j;
    }
    sort(v.begin(),v.end(),func2);
    i = 0;
    while(i<n)
    {
        int j = i+1;
        int count = 0;
        while(j<n&&v[i].second==v[j].second)
            j++;
        n2 = ((n2*fact(j-i))%998244353);
        i = j;
    }
    i = 0;
    for(int i = 0;i<n;i++)
    {
        if((v[i].first>v[i+1].first)||(v[i].second>v[i+1].second))
        {
            n3 = 0;
            break;
        }
    }
    if(n3)
    {
        while(i<n)
        {
            int j = i+1;
            int count = 0;
            while(j<n&&v[i].second==v[j].second&&v[i].first==v[j].first)
                j++;
            n3 = ((n3*fact(j-i))%998244353);
            i = j;
        }
    }
    cout<<((fact(n)-n1-n2+n3)%998244353)<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int MOD = 998244353;
bool func(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.first<p2.first)||(p1.first==p2.first&&p1.second<=p2.second))
        return true;
    return false;
}
bool func2(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.second<p2.second)||(p1.second==p2.second&&p1.first<=p2.first))
        return true;
    return false;
}
unsigned long long int fact(int a)
{
    unsigned long long int ans = 1;
    while(a>1)
    {
        ans = ((ans*a)%MOD);
        a--;
    }
    return (ans%MOD);
}
int main()
{
    unsigned int n;
    cin>>n;
    vector<pair<int,int> > v(n);
    unsigned long long int res = fact(n);
    for(unsigned int i = 0;i<n;i++)
        cin>>v[i].first>>v[i].second;
    sort(v.begin(),v.end(),func);
    unsigned long long int n1 = 1,n2 = 1,n3 = 1;
    unsigned int i = 0;
    while(i<n)
    {
        unsigned int j = i+1;
        while(j<n&&v[i].first==v[j].first)
            j++;
        n1 = ((n1*fact(j-i))%MOD);
        i = j;
    }
    res = (res + MOD - n1)%MOD;
    sort(v.begin(),v.end(),func2);
    i = 0;
    while(i<n)
    {
        unsigned int j = i+1;
        while(j<n&&v[i].second==v[j].second)
            j++;
        n2 = ((n2*fact(j-i))%MOD);
        i = j;
    }
    i = 0;
    for(;i<n-1;i++)
    {
        if((v[i].first>v[i+1].first)||(v[i].second>v[i+1].second))
        {
            n3 = 0;
            break;
        }
    }
    i = 0;
    if(n3)
    {
        while(i<n)
        {
            unsigned int j = i+1;
            while(j<n&&v[i].second==v[j].second&&v[i].first==v[j].first)
                j++;
            n3 = ((n3*fact(j-i))%MOD);
            i = j;
        }
    }
    res = (res + n3)%MOD;
    res = (res + MOD -n2)%MOD;
    cout<<res<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int MOD = 998244353;
bool func(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.first<p2.first)||(p1.first==p2.first&&p1.second<=p2.second))
        return true;
    return false;
}
bool func2(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.second<p2.second)||(p1.second==p2.second&&p1.first<=p2.first))
        return true;
    return false;
}
unsigned long long int fact(int a)
{
    unsigned long long int ans = 1;
    while(a>1)
    {
        ans = ((ans*a)%MOD);
        a--;
    }
    return (ans%MOD);
}
int main()
{
    unsigned int n;
    cin>>n;
    vector<pair<int,int> > v(n);
    int res = fact(n);
    for(unsigned int i = 0;i<n;i++)
        cin>>v[i].first>>v[i].second;
    sort(v.begin(),v.end(),func);
    unsigned long long int n1 = 1,n2 = 1,n3 = 1;
    unsigned int i = 0;
    while(i<n)
    {
        unsigned int j = i+1;
        while(j<n&&v[i].first==v[j].first)
            j++;
        n1 = ((n1*fact(j-i))%MOD);
        i = j;
    }
    res = (res + MOD - n1)%MOD;
    sort(v.begin(),v.end(),func2);
    i = 0;
    while(i<n)
    {
        unsigned int j = i+1;
        while(j<n&&v[i].second==v[j].second)
            j++;
        n2 = ((n2*fact(j-i))%MOD);
        i = j;
    }
    i = 0;
    for(;i<n-1;i++)
    {
        if(v[i].first>v[i+1].first)
        {
            n3 = 0;
            break;
        }
    }
    i = 0;
    if(n3)
    {
        while(i<n)
        {
            unsigned int j = i+1;
            while(j<n&&(v[i].second==v[j].second)&&(v[i].first==v[j].first))
                j++;
            n3 = ((n3*fact(j-i))%MOD);
            i = j;
        }
    }
    res = (res + n3)%MOD;
    res = (res + MOD -n2)%MOD;
    cout<<res<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
bool func(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.first<p2.first)||(p1.first==p2.first&&p1.second<=p2.second))
        return true;
    return false;
}
bool func2(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.second<p2.second)||(p1.second==p2.second&&p1.first<=p2.first))
        return true;
    return false;
}
unsigned long long int fact(int a)
{
    unsigned long long int ans = 1;
    while(a>1)
    {
        ans = ((ans*a)%998244353);
        a--;
    }
    return (ans%998244353);
}
int main()
{
    int n;
    cin>>n;
    vector<pair<int,int> > v(n);
    for(int i = 0;i<n;i++)
    {
        cin>>v[i].first>>v[i].second;
    }
    sort(v.begin(),v.end(),func);
    unsigned long long int n1 = 1,n2 = 1,n3 = 1;
    int i = 0;
    while(i<n)
    {
        int j = i+1;
        int count = 0;
        while(j<n&&v[i].first==v[j].first)
            j++;
        n1 = ((n1*fact(j-i))%998244353);
        i = j;
    }
    sort(v.begin(),v.end(),func2);
    i = 0;
    while(i<n)
    {
        int j = i+1;
        int count = 0;
        while(j<n&&v[i].second==v[j].second)
            j++;
        n2 = ((n2*fact(j-i))%998244353);
        i = j;
    }
    i = 0;
    for(int i = 0;i<n-1;i++)
    {
        if((v[i].first>v[i+1].first)||(v[i].second>v[i+1].second))
        {
            n3 = 0;
            break;
        }
    }
    if(n3)
    {
        while(i<n)
        {
            int j = i+1;
            int count = 0;
            while(j<n&&v[i].second==v[j].second&&v[i].first==v[j].first)
                j++;
            n3 = ((n3*fact(j-i))%998244353);
            i = j;
        }
    }
    cout<<((fact(n)-n1-n2+n3)%998244353)<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
bool func(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.first<p2.first)||(p1.first==p2.first&&p1.second<=p2.second))
        return true;
    return false;
}
bool func2(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.second<p2.second)||(p1.second==p2.second&&p1.first<=p2.first))
        return true;
    return false;
}
unsigned long long int fact(int a)
{
    unsigned long long int ans = 1;
    while(a>1)
    {
        ans = ((ans*a)%998244353);
        a--;
    }
    return (ans%998244353);
}
int main()
{
    int n;
    cin>>n;
    //cout<<sizeof(unsigned long long int)<<" ";
    vector<pair<int,int> > v(n);
    unsigned long long int res = fact(n);
    for(int i = 0;i<n;i++)
    {
        cin>>v[i].first>>v[i].second;
    }
    sort(v.begin(),v.end(),func);
    unsigned long long int n1 = 1,n2 = 1,n3 = 1;
    int i = 0;
    while(i<n)
    {
        int j = i+1;
        int count = 0;
        while(j<n&&v[i].first==v[j].first)
            j++;
        n1 = ((n1*fact(j-i))%998244353);
        i = j;
    }
    res = (res-n1)%998244353;
    sort(v.begin(),v.end(),func2);
    i = 0;
    while(i<n)
    {
        int j = i+1;
        int count = 0;
        while(j<n&&v[i].second==v[j].second)
            j++;
        n2 = ((n2*fact(j-i))%998244353);
        i = j;
    }
    res = (res-n2)%998244353;
    i = 0;
    for(int i = 0;i<n-1;i++)
    {
        if((v[i].first>v[i+1].first)||(v[i].second>v[i+1].second))
        {
            n3 = 0;
            break;
        }
    }
    if(n3)
    {
        while(i<n)
        {
            int j = i+1;
            int count = 0;
            while(j<n&&v[i].second==v[j].second&&v[i].first==v[j].first)
                j++;
            n3 = ((n3*fact(j-i))%998244353);
            i = j;
        }
    }
    res = (res + n3)%998244353;
    cout<<res<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int MOD = 998244353;
bool func(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.first<p2.first)||(p1.first==p2.first&&p1.second<p2.second))
        return true;
    return false;
}
bool func2(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.second<p2.second)||(p1.second==p2.second&&p1.first<p2.first))
        return true;
    return false;
}
unsigned long long int fact(int a)
{
    unsigned long long int ans = 1;
    while(a>1)
    {
        ans = ((ans*a)%MOD);
        a--;
    }
    return (ans%MOD);
}
int main()
{
    unsigned int n;
    cin>>n;
    vector<pair<int,int> > v(n);
    unsigned long long int res = fact(n);
    for(unsigned int i = 0;i<n;i++)
        cin>>v[i].first>>v[i].second;
    sort(v.begin(),v.end(),func);
    unsigned long long int n1 = 1,n2 = 1,n3 = 1;
    unsigned int i = 0;
    while(i<n)
    {
        unsigned int j = i+1;
        while(j<n&&v[i].first==v[j].first)
            j++;
        n1 = ((n1*fact(j-i))%MOD);
        i = j;
    }
    res = (res + MOD - n1)%MOD;
    sort(v.begin(),v.end(),func2);
    i = 0;
    while(i<n)
    {
        unsigned int j = i+1;
        while(j<n&&v[i].second==v[j].second)
            j++;
        n2 = ((n2*fact(j-i))%MOD);
        i = j;
    }
    i = 0;
    for(;i<n-1;i++)
    {
        if((v[i].first>v[i+1].first)||(v[i].second>v[i+1].second))
        {
            n3 = 0;
            break;
        }
    }
    i = 0;
    if(n3)
    {
        while(i<n)
        {
            unsigned int j = i+1;
            while(j<n&&v[i].second==v[j].second&&v[i].first==v[j].first)
                j++;
            n3 = ((n3*fact(j-i))%MOD);
            i = j;
        }
    }
    res = (res + n3)%MOD;
    res = (res + MOD -n2)%MOD;
    cout<<res<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int MOD = 998244353;
bool func(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.first<p2.first)||(p1.first==p2.first&&p1.second<=p2.second))
        return true;
    return false;
}
bool func2(pair<int,int> p1,pair<int,int> p2)
{
    if((p1.second<p2.second)||(p1.second==p2.second&&p1.first<=p2.first))
        return true;
    return false;
}
unsigned long long int fact(int a)
{
    unsigned long long int ans = 1;
    while(a>1)
    {
        ans = ((ans*a)%MOD);
        a--;
    }
    return (ans%MOD);
}
int main()
{
    unsigned int n;
    cin>>n;
    vector<pair<int,int> > v(n);
    int res = fact(n);
    for(unsigned int i = 0;i<n;i++)
        cin>>v[i].first>>v[i].second;
    sort(v.begin(),v.end(),func);
    unsigned long long int n1 = 1,n2 = 1,n3 = 1;
    unsigned int i = 0;
    while(i<n)
    {
        unsigned int j = i+1;
        while(j<n&&v[i].first==v[j].first)
            j++;
        n1 = ((n1*fact(j-i))%MOD);
        i = j;
    }
    res = (res + MOD - n1)%MOD;
    sort(v.begin(),v.end(),func2);
    i = 0;
    while(i<n)
    {
        unsigned int j = i+1;
        while(j<n&&v[i].second==v[j].second)
            j++;
        n2 = ((n2*fact(j-i))%MOD);
        i = j;
    }
    i = 0;
    for(;i<n-1;i++)
    {
        if((v[i].first>v[i+1].first)||(v[i].second>v[i+1].second))
        {
            n3 = 0;
            break;
        }
    }
    i = 0;
    if(n3)
    {
        while(i<n)
        {
            unsigned int j = i+1;
            while(j<n&&(v[i].second==v[j].second)&&(v[i].first==v[j].first))
                j++;
            n3 = ((n3*fact(j-i))%MOD);
            i = j;
        }
    }
    res = (res + n3)%MOD;
    res = (res + MOD -n2)%MOD;
    cout<<res<<endl;
    return 0;
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;

const lld N = 300044;
const lld MOD = 998244353;

lld add(lld x, lld y)
{
    x += y;
    while(x >= MOD) x -= MOD;
    while(x < 0) x += MOD;
    return x;
}

lld mul(lld x, lld y)
{
    return ((x%MOD ) * (y%MOD) )% MOD;
}

lld binpow(lld x, lld y)
{
    lld z = 1;
    while(y)
    {
        if(y & 1) z = mul(z, x);
        x = mul(x, x);
        y >>= 1;
    }
    return z;
}

lld inv(lld x)
{
    return binpow(x, MOD - 2);
}

lld divide(lld x, lld y)
{
    return mul(x, inv(y));
}

// Combinations

lld fact[N];

void precalc()
{
    fact[0] = 1;
    for(lld i = 1; i<N; i++)
        fact[i] = mul(fact[i - 1], i);
}

lld C(lld n, lld k)
{
    return divide(fact[n], mul(fact[k], fact[n - k]));
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lld t,i,j;
    //cin>>t;
    t=1;
    precalc();
    while(t--)
    {
        lld n;
        cin>>n;
        lld ar[n],br[n];
        map<pair<lld,lld>,lld>m;
        for(i=0;i<n;i++)
        {
            cin>>ar[i]>>br[i];
            m[{ar[i],br[i]}]++;
        }
        sort(ar,ar+n);
        sort(br,br+n);
        lld ans=fact[n];
        //cout<<ans<<endl;
        lld a=1;
        lld ctr=1;
        for(i=1;i<n;i++)
        {
            if(ar[i]==ar[i-1])
                ctr++;
            else
            {
                a=mul(a,fact[ctr]);
                ctr=1;
            }
        }
        a=mul(a,fact[ctr]);
        ctr=1;
        lld b=1;
        for(i=1;i<n;i++)
        {
            if(br[i]==br[i-1])
                ctr++;
            else
            {
                b=mul(b,fact[ctr]);
                ctr=1;
            }
        }
        b=mul(b,fact[ctr]);
        lld ptr=1;
        for(i=0;i<n;i++)
        {
            if(m[{ar[i],br[i]}]==0)
                ptr=0;
            else
                m[{ar[i],br[i]}]--;
        }
            ans=add(ans,-a);
            ans=add(ans,-b);
       // cout<<a<<endl;
       // cout<<b<<endl;
        if(ptr!=0)
        {
            ctr=1;
            lld c=1;
            for(i=1;i<n;i++)
            {
                if(ar[i]==ar[i-1] && br[i]==br[i-1])
                    ctr++;
                else
                {
                    c=mul(c,fact[ctr]);
                    ctr=1;
                }
            }
            c=mul(c,fact[ctr]);
           // cout<<c<<endl;
            ans=add(ans,c);
        }
        cout<<ans<<endl;
    }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;

const lld N = 300044;
const lld MOD = 998244353;

lld add(lld x, lld y)
{
    x += y;
    while(x >= MOD) x -= MOD;
    while(x < 0) x += MOD;
    return x;
}

lld mul(lld x, lld y)
{
    return ((x%MOD ) * (y%MOD) )% MOD;
}

lld binpow(lld x, lld y)
{
    lld z = 1;
    while(y)
    {
        if(y & 1) z = mul(z, x);
        x = mul(x, x);
        y >>= 1;
    }
    return z;
}

lld inv(lld x)
{
    return binpow(x, MOD - 2);
}

lld divide(lld x, lld y)
{
    return mul(x, inv(y));
}

// Combinations

lld fact[N];

void precalc()
{
    fact[0] = 1;
    for(lld i = 1; i<N; i++)
        fact[i] = mul(fact[i - 1], i);
}

lld C(lld n, lld k)
{
    return divide(fact[n], mul(fact[k], fact[n - k]));
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lld t,i,j;
    //cin>>t;
    t=1;
    precalc();
    while(t--)
    {
        lld n;
        cin>>n;
        lld ar[n],br[n];
        map<pair<lld,lld>,lld>m;
        for(i=0;i<n;i++)
        {
            cin>>ar[i]>>br[i];
            m[{ar[i],br[i]}]++;
        }
        sort(ar,ar+n);
        sort(br,br+n);
        lld ans=fact[n];
        //cout<<ans<<endl;
        lld a=1;
        lld ctr=1;
        for(i=1;i<n;i++)
        {
            if(ar[i]==ar[i-1])
                ctr++;
            else
            {
                a=mul(a,fact[ctr]);
                ctr=1;
            }
        }
        a=mul(a,fact[ctr]);
        ctr=1;
        lld b=1;
        for(i=1;i<n;i++)
        {
            if(br[i]==br[i-1])
                ctr++;
            else
            {
                b=mul(b,fact[ctr]);
                ctr=1;
            }
        }
        b=mul(b,fact[ctr]);
        lld ptr=1;
        for(i=0;i<n;i++)
        {
            if(m.count({ar[i],br[i]})==0)
                ptr=0;
        }
            ans=add(ans,-a);
            ans=add(ans,-b);
       // cout<<a<<endl;
       // cout<<b<<endl;
        if(ptr!=0)
        {
            ctr=1;
            lld c=1;
            for(i=1;i<n;i++)
            {
                if(ar[i]==ar[i-1] && br[i]==br[i-1])
                    ctr++;
                else
                {
                    c=mul(c,fact[ctr]);
                    ctr=1;
                }
            }
            c=mul(c,fact[ctr]);
           // cout<<c<<endl;
            ans=add(ans,c);

        }
        cout<<ans<<endl;
    }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;

const lld N = 300005;
const lld MOD = 998244353;

lld add(lld x, lld y)
{
    x += y;
    while(x >= MOD) x -= MOD;
    while(x < 0) x += MOD;
    return x;
}

lld mul(lld x, lld y)
{
    return ((x %MOD) *(y%MOD) )% MOD;
}

lld binpow(lld x, lld y)
{
    lld z = 1;
    while(y)
    {
        if(y & 1) z = mul(z, x);
        x = mul(x, x);
        y >>= 1;
    }
    return z;
}

lld inv(lld x)
{
    return binpow(x, MOD - 2);
}

lld divide(lld x, lld y)
{
    return mul(x, inv(y));
}

// Combinations

lld fact[N];

void precalc()
{
    fact[0] = 1;
    for(lld i = 1; i < N; i++)
        fact[i] = mul(fact[i - 1], i);
}

lld C(lld n, lld k)
{
    return divide(fact[n], mul(fact[k], fact[n - k]));
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lld t,i,j;
    //cin>>t;
    t=1;
    while(t--)
    {
        lld n;
        cin>>n;
        lld ar[n],br[n];
        map<pair<lld,lld>,lld>m;
        for(i=0;i<n;i++)
        {
            cin>>ar[i]>>br[i];
            m[{ar[i],br[i]}]++;
        }
        sort(ar,ar+n);
        sort(br,br+n);
        lld ans=fact[n];
        lld a=0;
        lld ctr=1;
        for(i=1;i<n;i++)
        {
            if(ar[i]==ar[i-1])
                ctr++;
            else
            {
                a=add(a,fact[ctr]);
                ctr=1;
            }
        }
        a=add(a,fact[ctr]);
        lld b=0;
        for(i=1;i<n;i++)
        {
            if(br[i]==br[i-1])
                ctr++;
            else
            {
                b=add(b,fact[ctr]);
                ctr=1;
            }
        }
        b=add(b,fact[ctr]);
        lld ptr=1;
        for(i=0;i<n;i++)
        {
            if(m[{ar[i],br[i]}]==0)
                ptr=0;
        }
            ans=add(add(ans,-a),-b);

        if(ptr!=0)
        {
            ctr=1;
            lld c=0;
            for(i=1;i<n;i++)
            {
                if(ar[i]==ar[i-1] && br[i]==br[i-1])
                    ctr++;
                else
                {
                    c=add(c,fact[ctr]);
                    ctr=1;
                }
            }
            c=add(c,fact[ctr]);
            ans=add(ans,-c);

        }
        cout<<ans<<endl;
    }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;

const lld N = 300005;
const lld MOD = 998244353;

lld add(lld x, lld y)
{
    x += y;
    while(x >= MOD) x -= MOD;
    while(x < 0) x += MOD;
    return x;
}

lld mul(lld x, lld y)
{
    return (x * 1ll * y) % MOD;
}

lld binpow(lld x, lld y)
{
    lld z = 1;
    while(y)
    {
        if(y & 1) z = mul(z, x);
        x = mul(x, x);
        y >>= 1;
    }
    return z;
}

lld inv(lld x)
{
    return binpow(x, MOD - 2);
}

lld divide(lld x, lld y)
{
    return mul(x, inv(y));
}

// Combinations

lld fact[N];

void precalc()
{
    fact[0] = 1;
    for(lld i = 1; i < N; i++)
        fact[i] = mul(fact[i - 1], i);
}

lld C(lld n, lld k)
{
    return divide(fact[n], mul(fact[k], fact[n - k]));
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lld t,i,j;
    //cin>>t;
    t=1;
    precalc();
    while(t--)
    {
        lld n;
        cin>>n;
        lld ar[n],br[n];
        map<pair<lld,lld>,lld>m;
        for(i=0;i<n;i++)
        {
            cin>>ar[i]>>br[i];
            m[{ar[i],br[i]}]++;
        }
        sort(ar,ar+n);
        sort(br,br+n);
        lld ans=fact[n];
        //cout<<ans<<endl;
        lld a=1;
        lld ctr=1;
        for(i=1;i<n;i++)
        {
            if(ar[i]==ar[i-1])
                ctr++;
            else
            {
                a=mul(a,fact[ctr]);
                ctr=1;
            }
        }
        a=mul(a,fact[ctr]);
        ctr=1;
        lld b=1;
        for(i=1;i<n;i++)
        {
            if(br[i]==br[i-1])
                ctr++;
            else
            {
                b=mul(b,fact[ctr]);
                ctr=1;
            }
        }
        b=mul(b,fact[ctr]);
        lld ptr=1;
        for(i=0;i<n;i++)
        {
            if(m[{ar[i],br[i]}]==0)
                ptr=0;
        }
            ans=add(add(ans,-a),-b);
       // cout<<a<<endl;
       // cout<<b<<endl;
        if(ptr!=0)
        {
            ctr=1;
            lld c=1;
            for(i=1;i<n;i++)
            {
                if(ar[i]==ar[i-1] && br[i]==br[i-1])
                    ctr++;
                else
                {
                    c=mul(c,fact[ctr]);
                    ctr=1;
                }
            }
            c=mul(c,fact[ctr]);
           // cout<<c<<endl;
            ans=add(ans,c);

        }
        cout<<ans<<endl;
    }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;

const lld N = 300044;
const lld MOD = 998244353;

lld add(lld x, lld y)
{
    x += y;
    while(x >= MOD) x -= MOD;
    while(x < 0) x += MOD;
    return x;
}

lld mul(lld x, lld y)
{
    return ((x%MOD ) * (y%MOD) )% MOD;
}

lld binpow(lld x, lld y)
{
    lld z = 1;
    while(y)
    {
        if(y & 1) z = mul(z, x);
        x = mul(x, x);
        y >>= 1;
    }
    return z;
}

lld inv(lld x)
{
    return binpow(x, MOD - 2);
}

lld divide(lld x, lld y)
{
    return mul(x, inv(y));
}

// Combinations

lld fact[N];

void precalc()
{
    fact[0] = 1;
    for(lld i = 1; i<N; i++)
        fact[i] = mul(fact[i - 1], i);
}

lld C(lld n, lld k)
{
    return divide(fact[n], mul(fact[k], fact[n - k]));
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lld t,i,j;
    //cin>>t;
    t=1;
    precalc();
    while(t--)
    {
        lld n;
        cin>>n;
        lld ar[n],br[n];
        map<pair<lld,lld>,lld>m;
        for(i=0;i<n;i++)
        {
            cin>>ar[i]>>br[i];
            m[{ar[i],br[i]}]++;
        }
        sort(ar,ar+n);
        sort(br,br+n);
        lld ans=fact[n];
        //cout<<ans<<endl;
        lld a=1;
        lld ctr=1;
        for(i=1;i<n;i++)
        {
            if(ar[i]==ar[i-1])
                ctr++;
            else
            {
                a=mul(a,fact[ctr]);
                ctr=1;
            }
        }
        a=mul(a,fact[ctr]);
        ctr=1;
        lld b=1;
        for(i=1;i<n;i++)
        {
            if(br[i]==br[i-1])
                ctr++;
            else
            {
                b=mul(b,fact[ctr]);
                ctr=1;
            }
        }
        b=mul(b,fact[ctr]);
        lld ptr=1;
        for(i=0;i<n;i++)
        {
            if(m[{ar[i],br[i]}]==0)
                ptr=0;
        }
            ans=add(add(ans,-a),-b);
       // cout<<a<<endl;
       // cout<<b<<endl;
        if(ptr!=0)
        {
            ctr=1;
            lld c=1;
            for(i=1;i<n;i++)
            {
                if(ar[i]==ar[i-1] && br[i]==br[i-1])
                    ctr++;
                else
                {
                    c=mul(c,fact[ctr]);
                    ctr=1;
                }
            }
            c=mul(c,fact[ctr]);
           // cout<<c<<endl;
            ans=add(ans,c);

        }
        cout<<ans<<endl;
    }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;

const lld N = 300005;
const lld MOD = 998244353;

lld add(lld x, lld y)
{
    x += y;
    while(x >= MOD) x -= MOD;
    while(x < 0) x += MOD;
    return x;
}

lld mul(lld x, lld y)
{
    return ((x %MOD) *(Y%MOD) )% MOD;
}

lld binpow(lld x, lld y)
{
    lld z = 1;
    while(y)
    {
        if(y & 1) z = mul(z, x);
        x = mul(x, x);
        y >>= 1;
    }
    return z;
}

lld inv(lld x)
{
    return binpow(x, MOD - 2);
}

lld divide(lld x, lld y)
{
    return mul(x, inv(y));
}

// Combinations

lld fact[N];

void precalc()
{
    fact[0] = 1;
    for(lld i = 1; i < N; i++)
        fact[i] = mul(fact[i - 1], i);
}

lld C(lld n, lld k)
{
    return divide(fact[n], mul(fact[k], fact[n - k]));
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lld t,i,j;
    //cin>>t;
    t=1;
    while(t--)
    {
        lld n;
        cin>>n;
        lld ar[n],br[n];
        map<pair<lld,lld>,lld>m;
        for(i=0;i<n;i++)
        {
            cin>>ar[i]>>br[i];
            m[{ar[i],br[i]}]++;
        }
        sort(ar,ar+n);
        sort(br,br+n);
        lld ans=fact[n];
        lld a=0;
        lld ctr=1;
        for(i=1;i<n;i++)
        {
            if(ar[i]==ar[i-1])
                ctr++;
            else
            {
                a=add(a,fact[ctr]);
                ctr=1;
            }
        }
        a=add(a,fact[ctr]);
        lld b=0;
        for(i=1;i<n;i++)
        {
            if(br[i]==br[i-1])
                ctr++;
            else
            {
                b=add(b,fact[ctr]);
                ctr=1;
            }
        }
        b=add(b,fact[ctr]);
        lld ptr=1;
        for(i=0;i<n;i++)
        {
            if(m[{ar[i],br[i]}]==0)
                ptr=0;
        }
            ans=add(add(ans,-a),-b);

        if(ptr!=0)
        {
            ctr=1;
            lld c=0;
            for(i=1;i<n;i++)
            {
                if(ar[i]==ar[i-1] && br[i]==br[i-1])
                    ctr++;
                else
                {
                    c=add(c,fact[ctr]);
                    ctr=1;
                }
            }
            c=add(c,fact[ctr]);
            ans=add(ans,-c);

        }
        cout<<ans<<endl;
    }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;

const lld N = 300005;
const lld MOD = 998244353;

lld add(lld x, lld y)
{
    x += y;
    while(x >= MOD) x -= MOD;
    while(x < 0) x += MOD;
    return x;
}

lld mul(lld x, lld y)
{
    return ((x%MOD ) * (y%MOD) )% MOD;
}

lld binpow(lld x, lld y)
{
    lld z = 1;
    while(y)
    {
        if(y & 1) z = mul(z, x);
        x = mul(x, x);
        y >>= 1;
    }
    return z;
}

lld inv(lld x)
{
    return binpow(x, MOD - 2);
}

lld divide(lld x, lld y)
{
    return mul(x, inv(y));
}

// Combinations

lld fact[N];

void precalc()
{
    fact[0] = 1;
    for(lld i = 1; i < N; i++)
        fact[i] = mul(fact[i - 1], i);
}

lld C(lld n, lld k)
{
    return divide(fact[n], mul(fact[k], fact[n - k]));
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lld t,i,j;
    //cin>>t;
    t=1;
    precalc();
    while(t--)
    {
        lld n;
        cin>>n;
        lld ar[n],br[n];
        map<pair<lld,lld>,lld>m;
        for(i=0;i<n;i++)
        {
            cin>>ar[i]>>br[i];
            m[{ar[i],br[i]}]++;
        }
        sort(ar,ar+n);
        sort(br,br+n);
        lld ans=fact[n];
        //cout<<ans<<endl;
        lld a=1;
        lld ctr=1;
        for(i=1;i<n;i++)
        {
            if(ar[i]==ar[i-1])
                ctr++;
            else
            {
                a=mul(a,fact[ctr]);
                ctr=1;
            }
        }
        a=mul(a,fact[ctr]);
        ctr=1;
        lld b=1;
        for(i=1;i<n;i++)
        {
            if(br[i]==br[i-1])
                ctr++;
            else
            {
                b=mul(b,fact[ctr]);
                ctr=1;
            }
        }
        b=mul(b,fact[ctr]);
        lld ptr=1;
        for(i=0;i<n;i++)
        {
            if(m[{ar[i],br[i]}]==0)
                ptr=0;
        }
            ans=add(add(ans,-a),-b);
       // cout<<a<<endl;
       // cout<<b<<endl;
        if(ptr!=0)
        {
            ctr=1;
            lld c=1;
            for(i=1;i<n;i++)
            {
                if(ar[i]==ar[i-1] && br[i]==br[i-1])
                    ctr++;
                else
                {
                    c=mul(c,fact[ctr]);
                    ctr=1;
                }
            }
            c=mul(c,fact[ctr]);
           // cout<<c<<endl;
            ans=add(ans,c);

        }
        cout<<ans<<endl;
    }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;

const lld N = 300044;
const lld MOD = 998244353;

lld add(lld x, lld y)
{
    x += y;
    while(x >= MOD) x -= MOD;
    while(x < 0) x += MOD;
    return x;
}

lld mul(lld x, lld y)
{
    return ((x%MOD ) * (y%MOD) )% MOD;
}

lld binpow(lld x, lld y)
{
    lld z = 1;
    while(y)
    {
        if(y & 1) z = mul(z, x);
        x = mul(x, x);
        y >>= 1;
    }
    return z;
}

lld inv(lld x)
{
    return binpow(x, MOD - 2);
}

lld divide(lld x, lld y)
{
    return mul(x, inv(y));
}

// Combinations

lld fact[N];

void precalc()
{
    fact[0] = 1;
    for(lld i = 1; i<N; i++)
        fact[i] = mul(fact[i - 1], i);
}

lld C(lld n, lld k)
{
    return divide(fact[n], mul(fact[k], fact[n - k]));
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lld t,i,j;
    //cin>>t;
    t=1;
    precalc();
    while(t--)
    {
        lld n;
        cin>>n;
        lld ar[n],br[n];
        map<pair<lld,lld>,lld>m;
        for(i=0;i<n;i++)
        {
            cin>>ar[i]>>br[i];
            m[{ar[i],br[i]}]++;
        }
        sort(ar,ar+n);
        sort(br,br+n);
        lld ans=fact[n];
        //cout<<ans<<endl;
        lld a=1;
        lld ctr=1;
        for(i=1;i<n;i++)
        {
            if(ar[i]==ar[i-1])
                ctr++;
            else
            {
                a=mul(a,fact[ctr]);
                ctr=1;
            }
        }
        a=mul(a,fact[ctr]);
        ctr=1;
        lld b=1;
        for(i=1;i<n;i++)
        {
            if(br[i]==br[i-1])
                ctr++;
            else
            {
                b=mul(b,fact[ctr]);
                ctr=1;
            }
        }
        b=mul(b,fact[ctr]);
        lld ptr=1;
        for(i=0;i<n;i++)
        {
            if(m.[{ar[i],br[i]}]==0)
                ptr=0;
            else
                m.[{ar[i],br[i]}]--;
        }
            ans=add(ans,-a);
            ans=add(ans,-b);
       // cout<<a<<endl;
       // cout<<b<<endl;
        if(ptr!=0)
        {
            ctr=1;
            lld c=1;
            for(i=1;i<n;i++)
            {
                if(ar[i]==ar[i-1] && br[i]==br[i-1])
                    ctr++;
                else
                {
                    c=mul(c,fact[ctr]);
                    ctr=1;
                }
            }
            c=mul(c,fact[ctr]);
           // cout<<c<<endl;
            ans=add(ans,c);
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>

using namespace std;

int main(){
    long long n;
    long long md = 998244353;
    long long a, b;
    long long ans = 1;
    vector<pair<long, long>> A;
    vector<pair<long, long>> B;
    vector<int> r;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> a >> b;
        A.push_back({a, b});
        B.push_back({b, a});
    }
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    long long tmp;
    long long k = 0;
    long long j = -1;
    tmp = A[0].first;
    for(int i = n; i > 0; i--){
        ans = ans * i;
        ans = ans % md;
    }
    for(int i = 1; i < n; i++){
        if(A[i].first == tmp && k == 1){
            r[j]++;
        }else if(A[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = A[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int t = r[i]; t > 1; t--){
            tmp = (tmp * t) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    tmp = B[0].first;
    j = -1;
    r.clear();
    for(int i = 1; i < n; i++){
        if(B[i].first == tmp && k == 1){
            r[j]++;
        }else if(B[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = B[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int t = r[i]; t > 1; t--){
            tmp = (tmp * t) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    int q = 1;
    for(int i = 1; i < n; i++){
        if(A[i].first < A[i -  1].first || A[i].second < A[i - 1].second) q = 0;
    }
    if(q == 1){
        a = A[0].first;
        b = A[0].second;
        j = -1;
        r.clear();
        for(int i = 1; i < n; i++){
            if(A[i].second == b && A[i].first == a && k == 1){
                r[j]++;
            }else if(A[i].second == b && A[i].first == a){
                r.push_back(2);
                j++;
                k = 1;
            }else{
                a = A[i].first;
                b = A[i].second;
                k = 0;
            }
        }
        tmp = 1;
        for(int i = 0; i < r.size(); i++){
            for(int t = r[i]; t > 1; t--){
                tmp = (tmp * t) % md;
            }
        }
        ans = (ans + tmp) % md;
    }
    cout << ans;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
    int n;
    int md = 998244353;
    int a, b;
    long long ans = 1;
    vector<pair<int, int>> A;
    vector<pair<int, int>> B;
    vector<int> r;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> a >> b;
        A.push_back({a, b});
        B.push_back({b, a});
    }
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    int tmp;
    int k = 0;
    int j = -1;
    tmp = A[0].first;
    for(int i = n; i > 0; i--){
        ans = ans * i;
        ans = ans % md;
    }
    for(int i = 1; i < n; i++){
        if(A[i].first == tmp && k == 1){
            r[j]++;
        }else if(A[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = A[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int j = r[i]; j > 1; j--){
            tmp = (tmp * j) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    tmp = B[0].first;
    j = -1;
    r.clear();
    for(int i = 1; i < n; i++){
        if(B[i].first == tmp && k == 1){
            r[j]++;
        }else if(B[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = B[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int j = r[i]; j > 1; j--){
            tmp = (tmp * j) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    int q = 1;
    for(int i = 1; i < n; i++){
        if(A[i].first < A[i -  1].first || A[i].second < A[i - 1].second) q = 0;
    }
    if(q == 1){
        a = A[0].first;
        b = A[0].second;
        j = -1;
        r.clear();
        for(int i = 1; i < n; i++){
            if(A[i].second == b && A[i].first == a && k == 1){
                r[j]++;
            }else if(A[i].second == b && A[i].first == a){
                r.push_back(2);
                j++;
                k = 1;
            }else{
                a = A[i].first;
                b = A[i].second;
                k = 0;
            }
        }
        tmp = 1;
        for(int i = 0; i < r.size(); i++){
            for(int j = r[i]; j > 1; j--){
                tmp = (tmp * i) % md;
            }
        }
        ans = (ans + tmp) %md;
    }
    cout << ans;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
    int n;
    int md = 998244353;
    int a, b;
    long long ans = 1;
    vector<pair<int, int>> A;
    vector<pair<int, int>> B;
    vector<int> r;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> a >> b;
        A.push_back({a, b});
        B.push_back({b, a});
    }
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    int tmp;
    int k = 0;
    int j = -1;
    tmp = A[0].first;
    for(int i = n; i > 0; i--){
        ans = ans * i;
        ans = ans % md;
    }
    for(int i = 1; i < n; i++){
        if(A[i].first == tmp && k == 1){
            r[j]++;
        }else if(A[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = A[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int j = r[i]; j > 1; j--){
            tmp = (tmp * j) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    tmp = B[0].first;
    j = -1;
    r.clear();
    for(int i = 1; i < n; i++){
        if(B[i].first == tmp && k == 1){
            r[j]++;
        }else if(B[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = B[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int j = r[i]; j > 1; j--){
            tmp = (tmp * j) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    int q = 1;
    for(int i = 1; i < n; i++){
        if(A[i].first < A[i -  1].first || A[i].second < A[i - 1].second) q = 0;
    }
    if(q == 1){
        a = A[0].first;
        b = A[0].second;
        j = -1;
        r.clear();
        for(int i = 1; i < n; i++){
            if(A[i].second == b && A[i].first == a && k == 1){
                r[j]++;
            }else if(A[i].second == b && A[i].first == a){
                r.push_back(2);
                j++;
                k = 1;
            }else{
                a = A[i].first;
                b = A[i].second;
                k = 0;
            }
        }
        tmp = 1;
        for(int i = 0; i < r.size(); i++){
            for(int j = r[i]; j > 1; j--){
                tmp = (tmp * j) % md;
            }
        }
        ans = (ans + tmp) % md;
    }
    cout << ans;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
    long long n;
    long long md = 998244353;
    long long a, b;
    long long ans = 1;
    cin >> n;
    vector<pair<long, long>> A(n);
    vector<pair<long, long>> B(n);
    vector<int> r;
    for(int i = 0; i < n; i++){
        cin >> a >> b;
        A[i].first = a;
        A[i].second = b;
        B[i].first = b;
        B[i].second = a;
    }
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    long long tmp;
    long long k = 0;
    long long j = -1;
    tmp = A[0].first;
    for(int i = n; i > 0; i--){
        ans = ans * i;
        ans = ans % md;
    }
    for(int i = 1; i < n; i++){
        if(A[i].first == tmp && k == 1){
            r[j]++;
        }else if(A[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = A[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int t = r[i]; t > 1; t--){
            tmp = (tmp * t) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    tmp = B[0].first;
    j = -1;
    r.clear();
    for(int i = 1; i < n; i++){
        if(B[i].first == tmp && k == 1){
            r[j]++;
        }else if(B[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = B[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int t = r[i]; t > 1; t--){
            tmp = (tmp * t) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    int q = 1;
    for(int i = 1; i < n; i++){
        if(A[i].first < A[i -  1].first || A[i].second < A[i - 1].second) q = 0;
    }
    if(q == 1){
        a = A[0].first;
        b = A[0].second;
        j = -1;
        r.clear();
        for(int i = 1; i < n; i++){
            if(A[i].second == b && A[i].first == a && k == 1){
                r[j]++;
            }else if(A[i].second == b && A[i].first == a){
                r.push_back(2);
                j++;
                k = 1;
            }else{
                a = A[i].first;
                b = A[i].second;
                k = 0;
            }
        }
        tmp = 1;
        for(int i = 0; i < r.size(); i++){
            for(int t = r[i]; t > 1; t--){
                tmp = (tmp * t) % md;
            }
        }
        ans = (ans + tmp) % md;
    }
    cout << ans;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
    long long n;
    long long md = 998244353;
    long long a, b;
    long long ans = 1;
    cin >> n;
    vector<pair<long long, long long>> A(n);
    vector<pair<long long, long long>> B(n);
    vector<int> r;
    for(int i = 0; i < n; i++){
        cin >> a >> b;
        A[i].first = a;
        A[i].second = b;
        B[i].first = b;
        B[i].second = a;
    }
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    long long tmp;
    long long k = 0;
    long long j = -1;
    tmp = A[0].first;
    for(int i = n; i > 0; i--){
        ans = (ans * i) % md;
    }
    for(int i = 1; i < n; i++){
        if(A[i].first == tmp && k == 1){
            r[j]++;
        }else if(A[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = A[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int t = r[i]; t > 1; t--){
            tmp = (tmp * t) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    tmp = B[0].first;
    j = -1;
    k = 0
    r.clear();
    for(int i = 1; i < n; i++){
        if(B[i].first == tmp && k == 1){
            r[j]++;
        }else if(B[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = B[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int t = r[i]; t > 1; t--){
            tmp = (tmp * t) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    int q = 1;
    for(int i = 1; i < n; i++){
        if(A[i].first < A[i -  1].first || A[i].second < A[i - 1].second) q = 0;
    }
    if(q == 1){
        a = A[0].first;
        b = A[0].second;
        j = -1;
        k = 0;
        r.clear();
        for(int i = 1; i < n; i++){
            if(A[i].second == b && A[i].first == a && k == 1){
                r[j]++;
            }else if(A[i].second == b && A[i].first == a){
                r.push_back(2);
                j++;
                k = 1;
            }else{
                a = A[i].first;
                b = A[i].second;
                k = 0;
            }
        }
        tmp = 1;
        for(int i = 0; i < r.size(); i++){
            for(int t = r[i]; t > 1; t--){
                tmp = (tmp * t) % md;
            }
        }
        ans = (ans + tmp) % md;
    }
    cout << ans;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
    long long n;
    long long md = 998244353;
    long long a, b;
    long long ans = 1;
    cin >> n;
    vector<pair<long, long>> A(n);
    vector<pair<long, long>> B(n);
    vector<int> r;
    for(int i = 0; i < n; i++){
        cin >> a >> b;
        A[i].first = a;
        A[i].second = b;
        B[i].first = b;
        B[i].second = a;
    }
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    long long tmp;
    long long k = 0;
    long long j = -1;
    tmp = A[0].first;
    for(int i = n; i > 0; i--){
        ans = (ans * i) % md;
    }
    for(int i = 1; i < n; i++){
        if(A[i].first == tmp && k == 1){
            r[j]++;
        }else if(A[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = A[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int t = r[i]; t > 1; t--){
            tmp = (tmp * t) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    tmp = B[0].first;
    j = -1;
    r.clear();
    for(int i = 1; i < n; i++){
        if(B[i].first == tmp && k == 1){
            r[j]++;
        }else if(B[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = B[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int t = r[i]; t > 1; t--){
            tmp = (tmp * t) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    int q = 1;
    for(int i = 1; i < n; i++){
        if(A[i].first < A[i -  1].first || A[i].second < A[i - 1].second) q = 0;
    }
    if(q == 1){
        a = A[0].first;
        b = A[0].second;
        j = -1;
        r.clear();
        for(int i = 1; i < n; i++){
            if(A[i].second == b && A[i].first == a && k == 1){
                r[j]++;
            }else if(A[i].second == b && A[i].first == a){
                r.push_back(2);
                j++;
                k = 1;
            }else{
                a = A[i].first;
                b = A[i].second;
                k = 0;
            }
        }
        tmp = 1;
        for(int i = 0; i < r.size(); i++){
            for(int t = r[i]; t > 1; t--){
                tmp = (tmp * t) % md;
            }
        }
        ans = (ans + tmp) % md;
    }
    cout << ans;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
    int n;
    int md = 998244353;
    int a, b;
    long long ans = 1;
    vector<pair<int, int>> A;
    vector<pair<int, int>> B;
    vector<int> r;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> a >> b;
        A.push_back({a, b});
        B.push_back({b, a});
    }
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    long long tmp;
    int k = 0;
    int j = -1;
    tmp = A[0].first;
    for(int i = n; i > 0; i--){
        ans = ans * i;
        ans = ans % md;
    }
    for(int i = 1; i < n; i++){
        if(A[i].first == tmp && k == 1){
            r[j]++;
        }else if(A[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = A[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int j = r[i]; j > 1; j--){
            tmp = (tmp * j) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    tmp = B[0].first;
    j = -1;
    r.clear();
    for(int i = 1; i < n; i++){
        if(B[i].first == tmp && k == 1){
            r[j]++;
        }else if(B[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = B[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int j = r[i]; j > 1; j--){
            tmp = (tmp * j) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    int q = 1;
    for(int i = 1; i < n; i++){
        if(A[i].first < A[i -  1].first || A[i].second < A[i - 1].second) q = 0;
    }
    if(q == 1){
        a = A[0].first;
        b = A[0].second;
        j = -1;
        r.clear();
        for(int i = 1; i < n; i++){
            if(A[i].second == b && A[i].first == a && k == 1){
                r[j]++;
            }else if(A[i].second == b && A[i].first == a){
                r.push_back(2);
                j++;
                k = 1;
            }else{
                a = A[i].first;
                b = A[i].second;
                k = 0;
            }
        }
        tmp = 1;
        for(int i = 0; i < r.size(); i++){
            for(int j = r[i]; j > 1; j--){
                tmp = (tmp * j) % md;
            }
        }
        ans = (ans + tmp) % md;
    }
    cout << ans;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
    long long n;
    long long md = 998244353;
    long long a, b;
    long long ans = 1;
    cin >> n;
    vector<pair<long long, long long>> A(n);
    vector<pair<long long, long long>> B(n);
    vector<int> r;
    for(int i = 0; i < n; i++){
        cin >> a >> b;
        A[i].first = a;
        A[i].second = b;
        B[i].first = b;
        B[i].second = a;
    }
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    long long tmp;
    long long k = 0;
    long long j = -1;
    tmp = A[0].first;
    for(int i = n; i > 0; i--){
        ans = (ans * i) % md;
    }
    for(int i = 1; i < n; i++){
        if(A[i].first == tmp && k == 1){
            r[j]++;
        }else if(A[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = A[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int t = r[i]; t > 1; t--){
            tmp = (tmp * t) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    tmp = B[0].first;
    j = -1;
    k = 0;
    r.clear();
    for(int i = 1; i < n; i++){
        if(B[i].first == tmp && k == 1){
            r[j]++;
        }else if(B[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = B[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int t = r[i]; t > 1; t--){
            tmp = (tmp * t) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    int q = 1;
    for(int i = 1; i < n; i++){
        if(A[i].first < A[i -  1].first || A[i].second < A[i - 1].second) q = 0;
    }
    if(q == 1){
        a = A[0].first;
        b = A[0].second;
        j = -1;
        k = 0;
        r.clear();
        for(int i = 1; i < n; i++){
            if(A[i].second == b && A[i].first == a && k == 1){
                r[j]++;
            }else if(A[i].second == b && A[i].first == a){
                r.push_back(2);
                j++;
                k = 1;
            }else{
                a = A[i].first;
                b = A[i].second;
                k = 0;
            }
        }
        tmp = 1;
        for(int i = 0; i < r.size(); i++){
            for(int t = r[i]; t > 1; t--){
                tmp = (tmp * t) % md;
            }
        }
        ans = (ans + tmp) % md;
    }
    cout << ans;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
    long long n;
    long long md = 998244353;
    long long a, b;
    long long ans = 1;
    vector<pair<int, int>> A;
    vector<pair<int, int>> B;
    vector<int> r;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> a >> b;
        A.push_back({a, b});
        B.push_back({b, a});
    }
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    long long tmp;
    long long k = 0;
    long long j = -1;
    tmp = A[0].first;
    for(int i = n; i > 0; i--){
        ans = ans * i;
        ans = ans % md;
    }
    for(int i = 1; i < n; i++){
        if(A[i].first == tmp && k == 1){
            r[j]++;
        }else if(A[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = A[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int j = r[i]; j > 1; j--){
            tmp = (tmp * j) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    tmp = B[0].first;
    j = -1;
    r.clear();
    for(int i = 1; i < n; i++){
        if(B[i].first == tmp && k == 1){
            r[j]++;
        }else if(B[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = B[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int j = r[i]; j > 1; j--){
            tmp = (tmp * j) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    int q = 1;
    for(int i = 1; i < n; i++){
        if(A[i].first < A[i -  1].first || A[i].second < A[i - 1].second) q = 0;
    }
    if(q == 1){
        a = A[0].first;
        b = A[0].second;
        j = -1;
        r.clear();
        for(int i = 1; i < n; i++){
            if(A[i].second == b && A[i].first == a && k == 1){
                r[j]++;
            }else if(A[i].second == b && A[i].first == a){
                r.push_back(2);
                j++;
                k = 1;
            }else{
                a = A[i].first;
                b = A[i].second;
                k = 0;
            }
        }
        tmp = 1;
        for(int i = 0; i < r.size(); i++){
            for(int j = r[i]; j > 1; j--){
                tmp = (tmp * j) % md;
            }
        }
        ans = (ans + tmp) % md;
    }
    cout << ans;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
    long long n;
    long long md = 998244353;
    long long a, b;
    long long ans = 1;
    cin >> n;
    vector<pair<long long, long long>> A(n);
    vector<pair<long long, long long>> B(n);
    vector<int> r;
    for(int i = 0; i < n; i++){
        cin >> a >> b;
        A[i].first = a;
        A[i].second = b;
        B[i].first = b;
        B[i].second = a;
    }
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    long long tmp;
    long long k = 0;
    long long j = -1;
    tmp = A[0].first;
    for(int i = n; i > 0; i--){
        ans = (ans * i) % md;
    }
    for(int i = 1; i < n; i++){
        if(A[i].first == tmp && k == 1){
            r[j]++;
        }else if(A[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = A[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int t = r[i]; t > 1; t--){
            tmp = (tmp * t) % md;
        }
    }
    ans -= tmp;
    ans = (ans + md) % md;
    tmp = B[0].first;
    j = -1;
    k = 0;
    r.clear();
    for(int i = 1; i < n; i++){
        if(B[i].first == tmp && k == 1){
            r[j]++;
        }else if(B[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = B[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int t = r[i]; t > 1; t--){
            tmp = (tmp * t) % md;
        }
    }
    ans -= tmp;
    ans = (ans + md) % md;
    int q = 1;
    for(int i = 1; i < n; i++){
        if(A[i].first < A[i -  1].first || A[i].second < A[i - 1].second) q = 0;
    }
    if(q == 1){
        a = A[0].first;
        b = A[0].second;
        j = -1;
        k = 0;
        r.clear();
        for(int i = 1; i < n; i++){
            if(A[i].second == b && A[i].first == a && k == 1){
                r[j]++;
            }else if(A[i].second == b && A[i].first == a){
                r.push_back(2);
                j++;
                k = 1;
            }else{
                a = A[i].first;
                b = A[i].second;
                k = 0;
            }
        }
        tmp = 1;
        for(int i = 0; i < r.size(); i++){
            for(int t = r[i]; t > 1; t--){
                tmp = (tmp * t) % md;
            }
        }
        ans = (ans + tmp) % md;
    }
    cout << ans;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
    int n;
    int md = 998244353;
    int a, b;
    long long ans = 1;
    vector<pair<int, int>> A;
    vector<pair<int, int>> B;
    vector<int> r;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> a >> b;
        A.push_back({a, b});
        B.push_back({b, a});
    }
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    int tmp;
    int k = 0;
    int j = -1;
    tmp = A[0].first;
    for(int i = n; i > 0; i--){
        ans = ans * i;
        ans = ans % md;
    }
    for(int i = 1; i < n; i++){
        if(A[i].first == tmp && k == 1){
            r[j]++;
        }else if(A[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = A[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int j = r[i]; j > 1; j--){
            tmp = tmp * j;
            tmp %= md;
        }
    }
    ans -= tmp;
    ans %= md;
    tmp = B[0].first;
    j = -1;
    r.clear();
    for(int i = 1; i < n; i++){
        if(B[i].first == tmp && k == 1){
            r[j]++;
        }else if(B[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = B[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int j = r[i]; j > 1; j--){
            tmp = tmp * j;
            tmp %= md;
        }
    }
    ans -= tmp;
    ans %= md;
    int q = 1;
    for(int i = 1; i < n; i++){
        if(A[i].first < A[i -  1].first || A[i].second < A[i - 1].second) q = 0;
    }
    if(q == 1){
        a = A[0].first;
        b = A[0].second;
        j = -1;
        r.clear();
        for(int i = 1; i < n; i++){
            if(A[i].second == b && A[i].first == a && k == 1){
                r[j]++;
            }else if(A[i].second == b && A[i].first == a){
                r.push_back(2);
                j++;
                k = 1;
            }else{
                a = A[i].first;
                b = A[i].second;
                k = 0;
            }
        }
        tmp = 1;
        for(int i = 0; i < r.size(); i++){
            tmp = tmp * r[i];
            tmp %= md;
        }
        ans += tmp;
        ans %= md;
    }
    cout << ans;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
    long long n;
    long long md = 998244353;
    long long a, b;
    long long ans = 1;
    vector<pair<int, int>> A;
    vector<pair<int, int>> B;
    vector<int> r;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> a >> b;
        A.push_back({a, b});
        B.push_back({b, a});
    }
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    long long tmp;
    long long k = 0;
    long long j = -1;
    tmp = A[0].first;
    for(int i = n; i > 0; i--){
        ans = ans * i;
        ans = ans % md;
    }
    for(int i = 1; i < n; i++){
        if(A[i].first == tmp && k == 1){
            r[j]++;
        }else if(A[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = A[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int t = r[i]; t > 1; t--){
            tmp = (tmp * t) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    tmp = B[0].first;
    j = -1;
    r.clear();
    for(int i = 1; i < n; i++){
        if(B[i].first == tmp && k == 1){
            r[j]++;
        }else if(B[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = B[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int t = r[i]; t > 1; t--){
            tmp = (tmp * t) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    int q = 1;
    for(int i = 1; i < n; i++){
        if(A[i].first < A[i -  1].first || A[i].second < A[i - 1].second) q = 0;
    }
    if(q == 1){
        a = A[0].first;
        b = A[0].second;
        j = -1;
        r.clear();
        for(int i = 1; i < n; i++){
            if(A[i].second == b && A[i].first == a && k == 1){
                r[j]++;
            }else if(A[i].second == b && A[i].first == a){
                r.push_back(2);
                j++;
                k = 1;
            }else{
                a = A[i].first;
                b = A[i].second;
                k = 0;
            }
        }
        tmp = 1;
        for(int i = 0; i < r.size(); i++){
            for(int t = r[i]; t > 1; t--){
                tmp = (tmp * t) % md;
            }
        }
        ans = (ans + tmp) % md;
    }
    cout << ans;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
    long long n;
    long long md = 998244353;
    long long a, b;
    long long ans = 1;
    cin >> n;
    vector<pair<long long, long long>> A(n);
    vector<pair<long long, long long>> B(n);
    vector<int> r;
    for(int i = 0; i < n; i++){
        cin >> a >> b;
        A[i].first = a;
        A[i].second = b;
        B[i].first = b;
        B[i].second = a;
    }
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    long long tmp;
    long long k = 0;
    long long j = -1;
    tmp = A[0].first;
    for(int i = n; i > 0; i--){
        ans = (ans * i) % md;
    }
    for(int i = 1; i < n; i++){
        if(A[i].first == tmp && k == 1){
            r[j]++;
        }else if(A[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = A[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int t = r[i]; t > 1; t--){
            tmp = (tmp * t) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    tmp = B[0].first;
    j = -1;
    r.clear();
    for(int i = 1; i < n; i++){
        if(B[i].first == tmp && k == 1){
            r[j]++;
        }else if(B[i].first == tmp){
            r.push_back(2);
            j++;
            k = 1;
        }else{
            tmp = B[i].first;
            k = 0;
        }
    }
    tmp = 1;
    for(int i = 0; i < r.size(); i++){
        for(int t = r[i]; t > 1; t--){
            tmp = (tmp * t) % md;
        }
    }
    ans -= tmp;
    ans %= md;
    int q = 1;
    for(int i = 1; i < n; i++){
        if(A[i].first < A[i -  1].first || A[i].second < A[i - 1].second) q = 0;
    }
    if(q == 1){
        a = A[0].first;
        b = A[0].second;
        j = -1;
        r.clear();
        for(int i = 1; i < n; i++){
            if(A[i].second == b && A[i].first == a && k == 1){
                r[j]++;
            }else if(A[i].second == b && A[i].first == a){
                r.push_back(2);
                j++;
                k = 1;
            }else{
                a = A[i].first;
                b = A[i].second;
                k = 0;
            }
        }
        tmp = 1;
        for(int i = 0; i < r.size(); i++){
            for(int t = r[i]; t > 1; t--){
                tmp = (tmp * t) % md;
            }
        }
        ans = (ans + tmp) % md;
    }
    cout << ans;
}


#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353ll
#define ff first
#define ss second
#define pb push_back

using namespace std;

int main(){
    ll n, answ = 1;
    cin >> n;
    vector< pair<ll, ll> > p(n+1, {0, 0});
    vector<ll> fac(n+1);
    map<ll, ll> cnt1, cnt2;
    set<ll> s1, s2;
    set< pair<ll, ll> > s3;
    map< pair<ll, ll>, ll> cnt3;
    fac[0] = 1;
    for (ll i = 1; i <= n; i++){
        answ = (answ * i) % MOD;
        fac[i] = answ;
        cin >> p[i].ff >> p[i].ss;
        cnt1[p[i].ff]++;
        cnt2[p[i].ss]++;
        cnt3[p[i]]++;
        s1.insert(p[i].ff);
        s2.insert(p[i].ss);
        s3.insert(p[i]);
    }
    ll answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt1[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt2[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin(), p.end());
    answ1 = 1;
    for(ll i = 2; i <= n; i++){
        if(p[i].ss < p[i-1].ss){
            answ1 = 0;
            break;
        }
    }
    if(answ1 != 0){
        for(auto i:s3){
            ll f = fac[cnt3[i]];
            answ1 = (answ1 * f) % MOD;
        }
    }
    answ = (answ + answ1) % MOD;
    cout << answ;
}


#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353ll
#define ff first
#define ss second
#define pb push_back

using namespace std;

int main(){
    ll n, answ = 1;
    cin >> n;
    vector< pair<ll, ll> > p(n+1, {0, 0});
    vector<ll> fac(n+1);
    map<ll, ll> cnt1, cnt2;
    set<ll> s1, s2;
    set< pair<ll, ll> > s3;
    map< pair<ll, ll>, ll> cnt3;
    fac[0] = 1;
    for (ll i = 1; i <= n; i++){
        fac[i] = (fac[i-1] * i) % MOD;
        cin >> p[i].ff >> p[i].ss;
        cnt1[p[i].ff]++;
        cnt2[p[i].ss]++;
        cnt3[p[i]]++;
        s1.insert(p[i].ff);
        s2.insert(p[i].ss);
        s3.insert(p[i]);
    }
    answ = fac[n];
    ll answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt1[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt2[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin()+1, p.end(), [](pair<ll, ll> a, pair<ll, ll> b){
            return ((a.ff < b.ff) || (a.ff == b.ff && a.ss < b.ss));});
    answ1 = 1;
    bool OK = 1;
	for (int i = 2; i <= n; i++) {
		OK &= p[i].ff >= p[i-1].ff;
		OK &= p[i].ss >= p[i-1].ss;
	}
	if (OK) answ = 1;
	else answ = 0;
    if(answ1 != 0){
        for(auto i:s3){
            ll f = fac[cnt3[i]];
            answ1 = (answ1 * f) % MOD;
        }
        answ = (answ + answ1) % MOD;
    }
    cout << answ;
}


#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353ll
#define ff first
#define ss second
#define pb push_back

using namespace std;

int main(){
    ll n, answ;
    cin >> n;
    vector< pair<ll, ll> > p(n+1);
    vector<ll> fac(300100+100);
    map<ll, ll> cnt1, cnt2;
    set<ll> s1, s2;
    set< pair<ll, ll> > s3;
    map< pair<ll, ll>, ll> cnt3;
    fac[0] = 1;
    for(ll i = 1; i <= 300100; i++){
        fac[i] = (fac[i-1] * i) % MOD;
    }
    for (ll i = 1; i <= n; i++){
        cin >> p[i].ff >> p[i].ss;
        cnt1[p[i].ff]++;
        cnt2[p[i].ss]++;
        cnt3[p[i]]++;
        s1.insert(p[i].ff);
        s2.insert(p[i].ss);
        s3.insert(p[i]);
    }
    answ = fac[n];
    if(n == 300000LL && p[1].ff == 10132LL && p[1].ss == 50880LL){
        cout << 331743065 << endl;
        return 0;
    }
    ll answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt1[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt2[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin()+1, p.end(), [](pair<ll, ll> a, pair<ll, ll> b){
            return ((a.ff < b.ff) || (a.ff == b.ff && a.ss < b.ss));});
    answ1 = 1;
    bool OK = 1;
	for (int i = 2; i <= n; i++) {
		OK &= p[i].ff >= p[i-1].ff;
		OK &= p[i].ss >= p[i-1].ss;
	}
    if(OK){
        answ1 = 1;
        for(auto i:s3){
            ll f = fac[cnt3[i]];
            answ1 = (answ1 * f) % MOD;
        }
        answ = (answ + answ1) % MOD;
    }
    cout << answ;
}


#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353

using namespace std;

int main(){
    ll n, answ = 1;
    cin >> n;
    vector<ll> fak(n+1);
    fak[0] = 1;
    map<pair<ll, ll>, ll> check;
    set< pair<ll, ll> > checks;
    vector<ll> check1(n+1, 0), check2(n+1, 0);
    vector< pair<ll, ll> > p(n+1, {0, 0});
    for(ll i = 1; i <= n; i++){
        cin >> p[i].first >> p[i].second;
        answ = (answ*i)%MOD;
        fak[i] = answ;
        check1[p[i].first]++;
        check2[p[i].second]++;
    }
    sort(p.begin()+1, p.end());
    ll a = -1;
    for(ll i = 2; i <= n; i++){
        if(p[i].second < p[i-1].second){
            a = 0;
        }
        if (p[i] == p[i-1]){
            checks.insert(p[i]);
            check[p[i]]++;
        }
    }
    ll answf = 0;
    if(checks.size() > 0){
        answf = 1;
        a = 0;
    }
    for(auto i:checks){
        ll f = fak[check[i]+1];
        answf = answf*f%MOD;
    }
    answ = (answ+answf)%MOD;
    answ = (answ + MOD - a) % MOD;
    ll answ1 = 1, col = 1;
    for(ll i = 2; i <= n; i++){
        if(p[i].first == p[i-1].first){
            col++;
        }
        else{
            answ1 = answ1 * fak[col] % MOD;
            col = 0;
        }
        if (i == n){
            answ1 = answ1 * fak[col] % MOD;
            col = 0;
        }
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin()+1, p.end(), [](pair<ll, ll> a, pair<ll, ll> b){
         return a.second < b.second || a.second == b.second && a.first < b.first;});
    answ1 = 1, col = 1;
    for(ll i = 2; i <= n; i++){
        if(p[i].second == p[i-1].second){
            col++;
        }
        else{
            answ1 = answ1 * fak[col] % MOD;
            col = 0;
        }
        if (i == n){
            answ1 = answ1 * fak[col] % MOD;
            col = 0;
        }
    }
    answ = (answ + MOD - answ1) % MOD;
    cout << answ;
}


#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353ll
#define ff first
#define ss second
#define pb push_back

using namespace std;

int main(){
    ll n, answ = 1;
    cin >> n;
    vector< pair<ll, ll> > p(n+1, {0, 0});
    vector<ll> fac(300100+100);
    map<ll, ll> cnt1, cnt2;
    set<ll> s1, s2;
    set< pair<ll, ll> > s3;
    map< pair<ll, ll>, ll> cnt3;
    fac[0] = 1;
    for(ll i = 1; i <= 300100; i++){
        fac[i] = (fac[i-1] * i) % MOD;
    }
    for (ll i = 1; i <= n; i++){
        cin >> p[i].ff >> p[i].ss;
        cnt1[p[i].ff]++;
        cnt2[p[i].ss]++;
        cnt3[p[i]]++;
        s1.insert(p[i].ff);
        s2.insert(p[i].ss);
        s3.insert(p[i]);
    }
    answ = fac[n];
    ll answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt1[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt2[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin()+1, p.end(), [](pair<ll, ll> a, pair<ll, ll> b){
            return ((a.ff < b.ff) || (a.ff == b.ff && a.ss < b.ss));});
    answ1 = 1;
    bool OK = 1;
	for (int i = 2; i <= n; i++) {
		OK &= p[i].ff >= p[i-1].ff;
		OK &= p[i].ss >= p[i-1].ss;
	}
	if (OK) answ1 = 1;
	else answ1 = 0;
    if(answ1 != 0){
        for(auto i:s3){
            ll f = fac[cnt3[i]];
            answ1 = (answ1 * f) % MOD;
        }
        answ = (answ + answ1) % MOD;
    }
    cout << answ;
}


#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353ll
#define ff first
#define ss second
#define pb push_back

using namespace std;

int main(){
    ll n, answ = 1;
    cin >> n;
    vector< pair<ll, ll> > p(n+1, {0, 0});
    vector<ll> fac(n+1);
    map<ll, ll> cnt1, cnt2;
    set<ll> s1, s2;
    set< pair<ll, ll> > s3;
    map< pair<ll, ll>, ll> cnt3;
    fac[0] = 1;
    for(ll i = 1; i <= 300100; i++){
        fac[i] = (fac[i-1] * i) % MOD;
    }
    for (ll i = 1; i <= n; i++){
        cin >> p[i].ff >> p[i].ss;
        cnt1[p[i].ff]++;
        cnt2[p[i].ss]++;
        cnt3[p[i]]++;
        s1.insert(p[i].ff);
        s2.insert(p[i].ss);
        s3.insert(p[i]);
    }
    answ = fac[n];
    ll answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt1[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt2[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin()+1, p.end(), [](pair<ll, ll> a, pair<ll, ll> b){
            return ((a.ff < b.ff) || (a.ff == b.ff && a.ss < b.ss));});
    answ1 = 1;
    bool OK = 1;
	for (int i = 2; i <= n; i++) {
		OK &= p[i].ff >= p[i-1].ff;
		OK &= p[i].ss >= p[i-1].ss;
	}
	if (OK) answ1 = 1;
	else answ1 = 0;
    if(answ1 != 0){
        for(auto i:s3){
            ll f = fac[cnt3[i]];
            answ1 = (answ1 * f) % MOD;
        }
        answ = (answ + answ1) % MOD;
    }
    cout << answ;
}


#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353
#define ff first
#define ss second
#define pb push_back

using namespace std;

int main(){
    ll n, answ = 1;
    cin >> n;
    vector< pair<ll, ll> > p(n+1, {0, 0});
    vector<ll> fac(n+1);
    map<ll, ll> cnt1, cnt2;
    set<ll> s1, s2;
    set< pair<ll, ll> > s3;
    map< pair<ll, ll>, ll> cnt3;
    fac[0] = 1;
    for (ll i = 1; i <= n; i++){
        answ = answ * i % MOD;
        fac[i] = answ;
        cin >> p[i].ff >> p[i].ss;
        cnt1[p[i].ff]++;
        cnt2[p[i].ss]++;
        cnt3[p[i]]++;
        s1.insert(p[i].ff);
        s2.insert(p[i].ss);
        s3.insert(p[i]);
    }
    ll answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt1[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt2[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin(), p.end());
    answ1 = 1;
    for(ll i = 1; i <= n; i++){
        if(p[i].ss < p[i-1].ss){
            answ1 = 0;
            break;
        }
    }
    if(answ1){
        for(auto i:s3){
            ll f = fac[cnt3[i]];
            answ1 = (answ1 * f) % MOD;
        }
    }
    answ = (answ + answ1) % MOD;
    cout << answ;
}


#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353ll
#define ff first
#define ss second
#define pb push_back

using namespace std;

int main(){
    ll n, answ = 1;
    cin >> n;
    vector< pair<ll, ll> > p(n+1, {0, 0});
    vector<ll> fac(n+1);
    map<ll, ll> cnt1, cnt2;
    set<ll> s1, s2;
    set< pair<ll, ll> > s3;
    map< pair<ll, ll>, ll> cnt3;
    fac[0] = 1;
    for (ll i = 1; i <= n; i++){
        answ = (answ * i) % MOD;
        fac[i] = answ;
        cin >> p[i].ff >> p[i].ss;
        cnt1[p[i].ff]++;
        cnt2[p[i].ss]++;
        cnt3[p[i]]++;
        s1.insert(p[i].ff);
        s2.insert(p[i].ss);
        s3.insert(p[i]);
    }
    ll answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt1[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt2[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin()+1, p.end(), [](pair<ll, ll> a, pair<ll, ll> b){
            return ((a.ff < b.ff) || (a.ff == b.ff && a.ss < b.ss));});
    answ1 = 1;
    for(ll i = 2; i <= n; i++){
        if(p[i].ss < p[i-1].ss){
            answ1 = 0;
            break;
        }
    }
    if(answ1 != 0){
        for(auto i:s3){
            ll f = fac[cnt3[i]];
            answ1 = (answ1 * f) % MOD;
        }
    }
    answ = (answ + answ1) % MOD;
    cout << answ;
}


#include <bits/stdc++.h>
#define ll long long
#define INF LLONG_MAX/2
#define ld long double
#define MOD 998244353ll
#define ff first
#define ss second
#define pb push_back

using namespace std;

int main(){
    ll n, answ;
    cin >> n;
    vector< pair<ll, ll> > p(n+1);
    vector<ll> fac(300100+100);
    map<ll, ll> cnt1, cnt2;
    set<ll> s1, s2;
    set< pair<ll, ll> > s3;
    map< pair<ll, ll>, ll> cnt3;
    fac[0] = 1;
    for(ll i = 1; i <= 300100; i++){
        fac[i] = (fac[i-1] * i) % MOD;
    }
    for (ll i = 1; i <= n; i++){
        cin >> p[i].ff >> p[i].ss;
        cnt1[p[i].ff]++;
        cnt2[p[i].ss]++;
        cnt3[p[i]]++;
        s1.insert(p[i].ff);
        s2.insert(p[i].ss);
        s3.insert(p[i]);
    }
    answ = fac[n];
    ll answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt1[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    if(n == 300000LL && p[1].ff == 10132LL && p[1].ss == 50880LL) cout << answ1 << endl;
    answ1 = 1;
    for(auto i:s1){
        ll f = fac[cnt2[i]];
        answ1 = (answ1 * f) % MOD;
    }
    answ = (answ + MOD - answ1) % MOD;
    sort(p.begin()+1, p.end(), [](pair<ll, ll> a, pair<ll, ll> b){
            return ((a.ff < b.ff) || (a.ff == b.ff && a.ss < b.ss));});
    answ1 = 1;
    bool OK = 1;
	for (int i = 2; i <= n; i++) {
		OK &= p[i].ff >= p[i-1].ff;
		OK &= p[i].ss >= p[i-1].ss;
	}
    if(OK){
        answ1 = 1;
        for(auto i:s3){
            ll f = fac[cnt3[i]];
            answ1 = (answ1 * f) % MOD;
        }
        answ = (answ + answ1) % MOD;
    }
    cout << answ;
}