#include<iostream>
#include<string>
#include<math.h>
#include<vector>
#include<algorithm>
#define MAX_N 200000
#define REP(i,n) for(auto i=0; i<(n); i++)
#define DEBUG(x) cout<< '>' << #x << ':'<< x << "\n";
#define FOR(i, a, b) for(int i =(a); i<=(b); i++)
#define FORD(i, a, b) for(int = (a); i>=(b); i--)
typedef long long i64;
typedef unsigned long long ui64;

using namespace std;

int main(){

	string page;
	cin>>page;
	int B = 0, u = 0, l = 0, b = 0, a = 0, s = 0, r =0;

	for(int i= 0; i< page.size(); i++){
		switch(page[i]){
			case 'B':
				B++;
				break;
			case 'u':
				u++;
				break;
			case 'l':
				l++;
			case 'b':
				b++;
				break;
			case 'a':
				a++;
				break;
			case 's':
				s++;
				break;
			case 'r':
				r++;
				break;
			default:
				break;
		}
	}

	cout<<min(min(min(B,u), min(l,b)),min(min(a,s),r));

	return 0;
}


#include<iostream>
#include<string>
#include<math.h>
#include<vector>
#include<algorithm>
#define MAX_N 200000
#define REP(i,n) for(auto i=0; i<(n); i++)
#define DEBUG(x) cout<< '>' << #x << ':'<< x << "\n";
#define FOR(i, a, b) for(int i =(a); i<=(b); i++)
#define FORD(i, a, b) for(int = (a); i>=(b); i--)
typedef long long i64;
typedef unsigned long long ui64;

using namespace std;

int main(){

	string page;
	cin>>page;
	int B = 0, u = 0, l = 0, b = 0, a = 0, s = 0, r =0;

	for(int i= 0; i< page.size(); i++){
		switch(page[i]){
			case 'B':
				B++;
				break;
			case 'u':
				u++;
				break;
			case 'l':
				l++;
			case 'b':
				b++;
				break;
			case 'a':
				a++;
				break;
			case 's':
				s++;
				break;
			case 'r':
				r++;
				break;
			default:
				break;
		}
	}

	cout<<min(min(min(B,u/2), min(l,b)),min(min(a/2,s),r));

	return 0;
}


#include<iostream>
#include<string>
#include<math.h>
#include<vector>
#include<algorithm>
#define MAX_N 200000
#define REP(i,n) for(auto i=0; i<(n); i++)
#define DEBUG(x) cout<< '>' << #x << ':'<< x << "\n";
#define FOR(i, a, b) for(int i =(a); i<=(b); i++)
#define FORD(i, a, b) for(int = (a); i>=(b); i--)
#include <map>

typedef long long i64;
typedef unsigned long long ui64;
//#include <bits/stdc++.h>
using namespace std;

int main(){

	map<char, int> m;
	string page;
	cin>>page;

	for(int x = 0 ;x < page.size() ; x++){
		m[page[x]]++;
	}

	int ans = m['B'];
	ans = min(ans, m['u']/2);
	ans = min(ans, m['a']/2);
	ans = min(ans, m['l']);
	ans = min(ans, m['b']);
	ans = min(ans, m['s']);
	ans = min(ans, m['r']);

	cout<<ans;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ll_s long
#define show1(a) cout<<a<<endl
#define show2(a,b) cout<<a<<" "<<b<<endl
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl
 ll min(ll a,ll b)
 {
     if(a<b)
     return a;
     else
     return b;
 }
 ll max(ll a,ll b)
 {
     if(a>b)
     return a;
     else
     return b;
 }
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second < b.second);
}

int main()
{
    ll n;
    cin >> n;

    ll t1,t2;
    vector<pair<ll,ll> > v;
    for(ll i=0;i<n;i++)
    {
        cin >> t1 >> t2;
        v.push_back(make_pair(t1,t2));
    }
    vector<pair<ll,ll> > v1 = v;
    sort(v1.begin(),v1.end());

int f=0;
ll fact[1000000];
fact[1] = 1;
for(ll i=2;i<=1000000;i++)
{
    fact[i] = i*fact[i-1];
    fact[i] = fact[i]%998244353;
}
vector<ll> v2,v3,v4;

    ll temp = 1;
        for(ll i=0;i<n-1;i++)
        {

            if(v[i].first == v[i+1].first)
            {
                temp++;
            }
            else
            {
                if(temp != 1)
                {
                    v2.push_back(temp);
                    temp = 1;
                }

            }

        }
        if(temp != 1)
        {
            v3.push_back(temp);
            temp = 1;
        }

        sort(v1.begin(),v1.end(),sortbysec);
        temp = 1;
                for(ll i=0;i<n-1;i++)
        {
            if(v1[i].second == v1[i+1].second)
            {
                temp++;
            }
            else
            {
                if(temp != 1)
                {
                    v3.push_back(temp);
                    temp = 1;
                }

            }

        }
        if(temp != 1)
        {
            v3.push_back(temp);
            temp = 1;
        }

        ll temp2=1;
        for(ll i=0;i<v2.size();i++)
        {
            temp2 = (temp2%998244353)*fact[v2[i]];
        }
        ll temp3 = 1;
        for(ll i=0;i<v3.size();i++)
        {
            temp3 = (temp3%998244353)*fact[v3[i]];
        }

        temp = 1;
        for(ll i=0;i<n-1;i++)
        {
            if(v[i].second == v[i+1].second && v[i].second == v[i+1].second)
            {
                temp++;
            }
            else
            {
                if(temp != 1)
                {
                    v4.push_back(temp);
                    temp = 1;
                }

            }

        }
        if(temp != 1)
        {
            v3.push_back(temp);
            temp = 1;
        }
        ll temp4 = 1;
        for(ll i=0;i<v4.size();i++)
        {
            temp4 = (temp4%998244353)*fact[v3[i]];
        }
        // cout << temp2 << " " << temp3 << " " << temp4 << " " << fact[n] << endl;
        // if(temp3 == 1)
        //     temp3 = 0;
        if(temp4 == 1)
            temp4 = 0;
        // if(temp2 == 1)
        //     temp2 = 0;

        if(temp2 == fact[n] || temp3 == fact[n])
            {
                cout << 0;
                return 0;
            }

        ll ans = ((fact[n]%998244353) - (temp3%998244353) - (temp2%998244353) + (temp4%998244353))%998244353;
        ans = ans%998244353 ;
        cout << ans;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ll_s long
#define show1(a) cout<<a<<endl
#define show2(a,b) cout<<a<<" "<<b<<endl
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl
 ll min(ll a,ll b)
 {
     if(a<b)
     return a;
     else
     return b;
 }
 ll max(ll a,ll b)
 {
     if(a>b)
     return a;
     else
     return b;
 }
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second < b.second);
}

int main()
{
    ll n;
    cin >> n;

    ll t1,t2;
    vector<pair<ll,ll> > v;
    for(ll i=0;i<n;i++)
    {
        cin >> t1 >> t2;
        v.push_back(make_pair(t1,t2));
    }
    vector<pair<ll,ll> > v1 = v;
    sort(v.begin(),v.end());

int f=0;
ll fact[n+10];
fact[1] = 1;
for(ll i=2;i<=n;i++)
{
    fact[i] = i*fact[i-1];
    fact[i] = fact[i]%998244353;
}
vector<ll> v2,v3,v4;

    ll temp = 1;
        for(ll i=0;i<n-1;i++)
        {

            if(v[i].first == v[i+1].first)
            {
                temp++;
            }
            else
            {
                if(temp != 1)
                {
                    v2.push_back(temp);
                    temp = 1;
                }

            }

        }
        if(temp != 1)
        {
            v2.push_back(temp);
            temp = 1;
        }

        sort(v1.begin(),v1.end(),sortbysec);
        temp = 1;
                for(ll i=0;i<n-1;i++)
        {
            if(v1[i].second == v1[i+1].second)
            {
                temp++;
            }
            else
            {
                if(temp != 1)
                {
                    v3.push_back(temp);
                    temp = 1;
                }

            }

        }
        if(temp != 1)
        {
            v3.push_back(temp);
            temp = 1;
        }

        ll temp2=1;
        for(ll i=0;i<v2.size();i++)
        {
            temp2 = (temp2%998244353)*fact[v2[i]];
        }
        ll temp3 = 1;
        for(ll i=0;i<v3.size();i++)
        {
            temp3 = (temp3%998244353)*fact[v3[i]];
        }

        temp = 1;
        ll flag =0 ;
        for(ll i=0;i<n-1;i++)
        {
            if(v[i].first == v[i+1].first && v[i].second == v[i+1].second)
            {
                temp++;
            }
            else
            {
                if(temp != 1)
                {
                    v4.push_back(temp);
                    temp = 1;
                }

            }

            if(v[i].first > v[i+1].first || v[i].second > v[i+1].second)
            {
                flag = -1;
                break;
            }

        }
        if(temp != 1)
        {
            v4.push_back(temp);
            temp = 1;
        }
        ll temp4 = 1;
        for(ll i=0;i<v4.size();i++)
        {
            temp4 = (temp4%998244353)*fact[v4[i]];
        }
         //cout << temp2 << " " << temp3 << " " << temp4 << " " << fact[n] << endl;
        // if(temp3 == 1)
        //     temp3 = 0;
        if(flag == -1)
            temp4 = 0;
        // if(temp2 == 1)
        //     temp2 = 0;

temp2 = temp2%998244353;
temp3 = temp3%998244353;
temp4 = temp4%998244353;
fact[n] = fact[n]%998244353;
 //cout << temp2 << " " << temp3 << " " << temp4 << " " << fact[n] << endl;

        // if(temp2 == fact[n] || temp3 == fact[n])
        //     {
        //         cout << 0;
        //         return 0;
        //     }

        lll ans = ((fact[n]%998244353) - (temp3%998244353) - (temp2%998244353) + (temp4%998244353))%998244353;
        ans = (ans+998244353+998244353)%998244353 ;
        cout << ans;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ll_s long
#define show1(a) cout<<a<<endl
#define show2(a,b) cout<<a<<" "<<b<<endl
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl
 ll min(ll a,ll b)
 {
     if(a<b)
     return a;
     else
     return b;
 }
 ll max(ll a,ll b)
 {
     if(a>b)
     return a;
     else
     return b;
 }
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second < b.second);
}

int main()
{
    ll n;
    cin >> n;

    ll t1,t2;
    vector<pair<ll,ll> > v;
    for(ll i=0;i<n;i++)
    {
        cin >> t1 >> t2;
        v.push_back(make_pair(t1,t2));
    }
    vector<pair<ll,ll> > v1 = v;
    sort(v1.begin(),v1.end());

int f=0;
ll fact[n+10];
fact[1] = 1;
for(ll i=2;i<=n;i++)
{
    fact[i] = i*fact[i-1];
    fact[i] = fact[i]%998244353;
}
vector<ll> v2,v3,v4;

    ll temp = 1;
        for(ll i=0;i<n-1;i++)
        {

            if(v[i].first == v[i+1].first)
            {
                temp++;
            }
            else
            {
                if(temp != 1)
                {
                    v2.push_back(temp);
                    temp = 1;
                }

            }

        }
        if(temp != 1)
        {
            v3.push_back(temp);
            temp = 1;
        }

        sort(v1.begin(),v1.end(),sortbysec);
        temp = 1;
                for(ll i=0;i<n-1;i++)
        {
            if(v1[i].second == v1[i+1].second)
            {
                temp++;
            }
            else
            {
                if(temp != 1)
                {
                    v3.push_back(temp);
                    temp = 1;
                }

            }

        }
        if(temp != 1)
        {
            v3.push_back(temp);
            temp = 1;
        }

        ll temp2=1;
        for(ll i=0;i<v2.size();i++)
        {
            temp2 = (temp2%998244353)*fact[v2[i]];
        }
        ll temp3 = 1;
        for(ll i=0;i<v3.size();i++)
        {
            temp3 = (temp3%998244353)*fact[v3[i]];
        }

        temp = 1;
        for(ll i=0;i<n-1;i++)
        {
            if(v[i].second == v[i+1].second && v[i].second == v[i+1].second)
            {
                temp++;
            }
            else
            {
                if(temp != 1)
                {
                    v4.push_back(temp);
                    temp = 1;
                }

            }

        }
        if(temp != 1)
        {
            v3.push_back(temp);
            temp = 1;
        }
        ll temp4 = 1;
        for(ll i=0;i<v4.size();i++)
        {
            temp4 = (temp4%998244353)*fact[v3[i]];
        }
         cout << temp2 << " " << temp3 << " " << temp4 << " " << fact[n] << endl;
        // if(temp3 == 1)
        //     temp3 = 0;
        if(temp4 == 1)
            temp4 = 0;
        // if(temp2 == 1)
        //     temp2 = 0;

temp2 = temp2%998244353;
temp3 = temp3%998244353;
temp4 = temp4%998244353;
fact[n] = fact[n]%998244353;
 cout << temp2 << " " << temp3 << " " << temp4 << " " << fact[n] << endl;

        if(temp2 == fact[n] || temp3 == fact[n])
            {
                cout << 0;
                return 0;
            }

        ll ans = ((fact[n]%998244353) - (temp3%998244353) - (temp2%998244353) + (temp4%998244353))%998244353;
        ans = ans%998244353 ;
        cout << ans;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ll_s long
#define show1(a) cout<<a<<endl
#define show2(a,b) cout<<a<<" "<<b<<endl
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl
 ll min(ll a,ll b)
 {
     if(a<b)
     return a;
     else
     return b;
 }
 ll max(ll a,ll b)
 {
     if(a>b)
     return a;
     else
     return b;
 }
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second < b.second);
}

int main()
{
    ll n;
    cin >> n;

    ll t1,t2;
    vector<pair<ll,ll> > v;
    for(ll i=0;i<n;i++)
    {
        cin >> t1 >> t2;
        v.push_back(make_pair(t1,t2));
    }
    vector<pair<ll,ll> > v1 = v;
    sort(v1.begin(),v1.end());

int f=0;
ll fact[10000000];
fact[1] = 1;
for(ll i=2;i<=1000000;i++)
{
    fact[i] = i*fact[i-1];
    fact[i] = fact[i]%998244353;
}
vector<ll> v2,v3,v4;

    ll temp = 1;
        for(ll i=0;i<n-1;i++)
        {

            if(v[i].first == v[i+1].first)
            {
                temp++;
            }
            else
            {
                if(temp != 1)
                {
                    v2.push_back(temp);
                    temp = 1;
                }

            }

        }
        if(temp != 1)
        {
            v3.push_back(temp);
            temp = 1;
        }

        sort(v1.begin(),v1.end(),sortbysec);
        temp = 1;
                for(ll i=0;i<n-1;i++)
        {
            if(v1[i].second == v1[i+1].second)
            {
                temp++;
            }
            else
            {
                if(temp != 1)
                {
                    v3.push_back(temp);
                    temp = 1;
                }

            }

        }
        if(temp != 1)
        {
            v3.push_back(temp);
            temp = 1;
        }

        ll temp2=1;
        for(ll i=0;i<v2.size();i++)
        {
            temp2 = (temp2%998244353)*fact[v2[i]];
        }
        ll temp3 = 1;
        for(ll i=0;i<v3.size();i++)
        {
            temp3 = (temp3%998244353)*fact[v3[i]];
        }

        temp = 1;
        for(ll i=0;i<n-1;i++)
        {
            if(v[i].second == v[i+1].second && v[i].second == v[i+1].second)
            {
                temp++;
            }
            else
            {
                if(temp != 1)
                {
                    v4.push_back(temp);
                    temp = 1;
                }

            }

        }
        if(temp != 1)
        {
            v3.push_back(temp);
            temp = 1;
        }
        ll temp4 = 1;
        for(ll i=0;i<v4.size();i++)
        {
            temp4 = (temp4%998244353)*fact[v3[i]];
        }
        // cout << temp2 << " " << temp3 << " " << temp4 << " " << fact[n] << endl;
        // if(temp3 == 1)
        //     temp3 = 0;
        if(temp4 == 1)
            temp4 = 0;
        // if(temp2 == 1)
        //     temp2 = 0;

        if(temp2 == fact[n] || temp3 == fact[n])
            {
                cout << 0;
                return 0;
            }

        ll ans = ((fact[n]%998244353) - (temp3%998244353) - (temp2%998244353) + (temp4%998244353))%998244353;
        ans = ans%998244353 ;
        cout << ans;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ll_s long
#define show1(a) cout<<a<<endl
#define show2(a,b) cout<<a<<" "<<b<<endl
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl
 ll min(ll a,ll b)
 {
     if(a<b)
     return a;
     else
     return b;
 }
 ll max(ll a,ll b)
 {
     if(a>b)
     return a;
     else
     return b;
 }
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second < b.second);
}

int main()
{
    ll n;
    cin >> n;

    ll t1,t2;
    vector<pair<ll,ll> > v;
    for(ll i=0;i<n;i++)
    {
        cin >> t1 >> t2;
        v.push_back(make_pair(t1,t2));
    }
    vector<pair<ll,ll> > v1 = v;
    sort(v1.begin(),v1.end());

int f=0;
ll fact[n+10];
fact[1] = 1;
for(ll i=2;i<=n;i++)
{
    fact[i] = i*fact[i-1];
    fact[i] = fact[i]%998244353;
}
vector<ll> v2,v3,v4;

    ll temp = 1;
        for(ll i=0;i<n-1;i++)
        {

            if(v[i].first == v[i+1].first)
            {
                temp++;
            }
            else
            {
                if(temp != 1)
                {
                    v2.push_back(temp);
                    temp = 1;
                }

            }

        }
        if(temp != 1)
        {
            v3.push_back(temp);
            temp = 1;
        }

        sort(v1.begin(),v1.end(),sortbysec);
        temp = 1;
                for(ll i=0;i<n-1;i++)
        {
            if(v1[i].second == v1[i+1].second)
            {
                temp++;
            }
            else
            {
                if(temp != 1)
                {
                    v3.push_back(temp);
                    temp = 1;
                }

            }

        }
        if(temp != 1)
        {
            v3.push_back(temp);
            temp = 1;
        }

        ll temp2=1;
        for(ll i=0;i<v2.size();i++)
        {
            temp2 = (temp2%998244353)*fact[v2[i]];
        }
        ll temp3 = 1;
        for(ll i=0;i<v3.size();i++)
        {
            temp3 = (temp3%998244353)*fact[v3[i]];
        }

        temp = 1;
        for(ll i=0;i<n-1;i++)
        {
            if(v[i].second == v[i+1].second && v[i].second == v[i+1].second)
            {
                temp++;
            }
            else
            {
                if(temp != 1)
                {
                    v4.push_back(temp);
                    temp = 1;
                }

            }

        }
        if(temp != 1)
        {
            v3.push_back(temp);
            temp = 1;
        }
        ll temp4 = 1;
        for(ll i=0;i<v4.size();i++)
        {
            temp4 = (temp4%998244353)*fact[v3[i]];
        }
        // cout << temp2 << " " << temp3 << " " << temp4 << " " << fact[n] << endl;
        // if(temp3 == 1)
        //     temp3 = 0;
        if(temp4 == 1)
            temp4 = 0;
        // if(temp2 == 1)
        //     temp2 = 0;

temp2 = temp2%998244353;
temp3 = temp2%998244353;
temp4 = temp2%998244353;
fact[n] = fact[n]%998244353;
        if(temp2 == fact[n] || temp3 == fact[n])
            {
                cout << 0;
                return 0;
            }

        ll ans = ((fact[n]%998244353) - (temp3%998244353) - (temp2%998244353) + (temp4%998244353))%998244353;
        ans = ans%998244353 ;
        cout << ans;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=3e5+50,mod=998244353;
int n,J[N];
struct node{int x,y;}a[N];
bool cmp1(node a,node b){return a.x!=b.x?a.x<b.x:a.y<b.y;}
bool cmp2(node a,node b){return a.y!=b.y?a.y<b.y:a.x<b.x;}
int main(){
	scanf("%d",&n);
	J[0]=1;
	for(int i=1;i<=n;i++) {
        scanf("%d%d",&a[i].x,&a[i].y);
        J[i]=1ll*J[i-1]*i%mod;
	}

	int ans=J[n],anss=1;
	sort(a+1,a+n+1,cmp1);
	for(int i=1,j;i<=n;i=j){
		j=i;
		while(j<=n&&a[j].x==a[i].x) j++;
		anss=1ll*anss*J[j-i]%mod;
	}
	ans=(ans-anss+mod)%mod;

	anss=1;
	sort(a+1,a+n+1,cmp2);
	for(int i=1,j;i<=n;i=j){
		j=i;
		while(j<=n&&a[j].y==a[i].y) j++;
		anss=1ll*anss*J[j-i]%mod;
	}
	ans=(ans-anss+mod)%mod;

	bool OK=1;
	for(int i=1;i<n;i++)if(a[i].x>a[i+1].x) OK=0;
	if(OK) {
        anss=1;
        for(int i=1,j;i<=n;i=j){
            j=i;
            while(j<=n&&a[j].y==a[i].y&&a[j].x==a[i].x) j++;
            anss=1ll*anss*J[j-i]%mod;
        }
        ans=(ans+anss)%mod;
	}
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=3e5+50,mod=998244353;
int n,ans,J[N],anss=1;
struct node{int x,y;}a[N];
bool cmp1(node a,node b){return a.x!=b.x?a.x<b.x:a.y<b.y;}
bool cmp2(node a,node b){return a.y!=b.y?a.y<b.y:a.x<b.x;}
int main(){
	scanf("%d",&n);
	J[0]=1;
	for(int i=1;i<=n;i++)J[i]=1ll*J[i-1]*i%mod;
	ans=J[n];
	for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y);
	sort(a+1,a+n+1,cmp1);
	for(int i=1,j;i<=n;i++){
		j=i;
		while(j<n&&a[j+1].x==a[j].x)j++;
		anss=1ll*anss*J[j-i+1]%mod;
		i=j;
	}
	ans=(ans-anss)%mod;
	if(n==300000) cout<<ans<<endl;
	anss=1;
	sort(a+1,a+n+1,cmp2);
	for(int i=1,j;i<=n;i++){
		j=i;
		while(j<n&&a[j+1].y==a[j].y)j++;
		anss=1ll*anss*J[j-i+1]%mod;
		i=j;
	}
	ans=(ans-anss)%mod;
	if(n==300000) cout<<ans<<endl;
	for(int i=1;i<n;i++)if(a[i].x>a[i+1].x)printf("%d\n",(ans+mod)%mod),exit(0);
	anss=1;
	for(int i=1,j;i<=n;i++){
		j=i;
		while(j<n&&a[j+1].y==a[j].y&&a[j+1].x==a[j].x)j++;
		anss=1ll*anss*J[j-i+1]%mod;
		i=j;
	}
	ans=(ans+anss)%mod;
	printf("%d\n",(ans+mod)%mod);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=3e5+50,mod=998244353;
int n,fac[N];
struct node{int a,b;}t[N];
bool cmp1(node p,node q){return p.a!=q.a?p.a<q.a:p.b<q.b;}
bool cmp2(node p,node q){return p.b!=q.b?p.b<q.b:p.a<q.a;}
int main(){
	scanf("%d",&n);
	fac[0]=1;
	for(int i=1;i<=n;i++) {
        scanf("%d%d",&t[i].a,&t[i].b);
        fac[i]=1ll*fac[i-1]*i%mod;
	}

        int ans=fac[n],tmp;
        sort(t+1,t+1+n,cmp1); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].a==t[l].a) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;

        sort(t+1,t+1+n,cmp2); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].b==t[l].b) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;

        bool OK=1;
        for(int i=1;i<n;i++) if(t[i+1].a<t[i].a) OK=0;
        if(OK) {
            tmp=1;
            for(int l=1,r;l<=n;l=r) {
                r=l;
                while(r<=n && t[r].a==t[l].a&&t[r].b==t[l].b) r++;
                tmp=1LL*tmp*fac[r-l]%mod;
            }
            ans=(ans+tmp)%mod;
        }
        printf("%d\n",ans);

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=3e5+5;
const int mod=998244353;
int n,fac[N];
struct NODE{ int a,b; }t[N];
bool cmp1(NODE & p,NODE & q){return p.a!=q.a?p.a<q.a:p.b<q.b;}
bool cmp2(NODE & p,NODE & q){return p.b!=q.b?p.b<q.b:p.a<q.a;}

int main()
{
    while(~scanf("%d",&n)) {
        fac[0]=1;
        for(int i=1;i<=n;i++) {
            scanf("%d%d",&t[i].a,&t[i].b);
            fac[i]=1LL*fac[i-1]*i%mod;
        }

        int ans=fac[n],tmp;
        sort(t+1,t+1+n,cmp1); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].a==t[l].a) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;

        sort(t+1,t+1+n,cmp2); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].b==t[l].b) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;

        bool OK=1;
        for(int i=1;i<n;i++) if(t[i+1].a<t[i].a) OK=0;
        if(OK) {
            tmp=1;
            for(int l=1,r;l<=n;l=r) {
                r=l;
                while(r<=n && t[r].a==t[l].a&&t[r].b==t[l].b) r++;
                tmp=1LL*tmp*fac[r-l]%mod;
            }
            ans=(ans+tmp)%mod;
        }
        printf("%d\n",ans);
    }

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=3e5+5;
const int mod=998244353;
int n,fac[N];
struct NODE{ int a,b; }t[N];
bool cmp1(NODE & p,NODE & q){return p.a!=q.a?p.a<q.a:p.b<q.b;}
bool cmp2(NODE & p,NODE & q){return p.b!=q.b?p.b<q.b:p.a<q.a;}

int main()
{
        scanf("%d",&n);
        fac[0]=1;
        for(int i=1;i<=n;i++) {
            scanf("%d%d",&t[i].a,&t[i].b);
            fac[i]=1ll*fac[i-1]*i%mod;
        }

        int ans=fac[n],tmp;
        sort(t+1,t+1+n,cmp1); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].a==t[l].a) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;

        sort(t+1,t+1+n,cmp2); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].b==t[l].b) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;

        bool OK=1;
        for(int i=1;i<n;i++) if(t[i+1].a<t[i].a) OK=0;
        if(OK) {
            tmp=1;
            for(int l=1,r;l<=n;l=r) {
                r=l;
                while(r<=n && t[r].a==t[l].a&&t[r].b==t[l].b) r++;
                tmp=1LL*tmp*fac[r-l]%mod;
            }
            ans=(ans+tmp)%mod;
        }
        printf("%d\n",ans);

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=3e5+50,mod=998244353;
int n,ans,J[N],anss=1;
struct node{int x,y;}a[N];
bool cmp1(node a,node b){return a.x!=b.x?a.x<b.x:a.y<b.y;}
bool cmp2(node a,node b){return a.y!=b.y?a.y<b.y:a.x<b.x;}
int main(){
	scanf("%d",&n);
	J[0]=1;
	for(int i=1;i<=n;i++) {
        scanf("%d%d",&a[i].x,&a[i].y);
        J[i]=1ll*J[i-1]*i%mod;
	}

	ans=J[n];
	sort(a+1,a+n+1,cmp1);
	for(int i=1,j;i<=n;i=j){
		j=i;
		while(j<=n&&a[j].x==a[i].x) j++;
		anss=1ll*anss*J[j-i]%mod;
	}
	ans=(ans-anss+mod)%mod;

	anss=1;
	sort(a+1,a+n+1,cmp2);
	for(int i=1,j;i<=n;i=j){
		j=i;
		while(j<=n&&a[j].y==a[i].y) j++;
		anss=1ll*anss*J[j-i]%mod;
	}
	ans=(ans-anss+mod)%mod;

	bool OK=1;
	for(int i=1;i<n;i++)if(a[i].x>a[i+1].x) OK=0;
	if(OK) {
        anss=1;
        for(int i=1,j;i<=n;i=j){
            j=i;
            while(j<=n&&a[j].y==a[i].y&&a[j].x==a[i].x) j++;
            anss=1ll*anss*J[j-i]%mod;
        }
        ans=(ans+anss)%mod;
	}
	printf("%d\n",(ans+mod)%mod);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=3e5+5,mod=998244353;
int n,fac[N];
struct node{int a,b;}t[N];
bool cmp1(node & p,node & q){return p.a!=q.a?p.a<q.a:p.b<q.b;}
bool cmp2(node & p,node & q){return p.b!=q.b?p.b<q.b:p.a<q.a;}
int main(){
	scanf("%d",&n);
	fac[0]=1;
	for(int i=1;i<=n;i++) {
        scanf("%d%d",&t[i].a,&t[i].b);
        fac[i]=1ll*fac[i-1]*i%mod;
	}

        int ans=fac[n],tmp;
        sort(t+1,t+1+n,cmp1); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].a==t[l].a) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;

        sort(t+1,t+1+n,cmp2); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].b==t[l].b) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;

        bool OK=1;
        for(int i=1;i<n;i++) if(t[i+1].a<t[i].a) OK=0;
        if(OK) {
            tmp=1;
            for(int l=1,r;l<=n;l=r) {
                r=l;
                while(r<=n && t[r].a==t[l].a&&t[r].b==t[l].b) r++;
                tmp=1LL*tmp*fac[r-l]%mod;
            }
            ans=(ans+tmp)%mod;
        }
        printf("%d\n",ans);

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
#define mem(i,j) memset(i,j,sizeof(i))
#define bug(args...) cout<<#args<<"->"<<args<<"\n";
const int N=3e5+5;
const int mod=998244853;

int n, fac[N];
struct NODE { int a,b; }t[N];
bool cmp1(NODE & p,NODE & q){return p.a!=q.a?p.a<q.a:p.b<q.b;}
bool cmp2(NODE & p,NODE & q){return p.b!=q.b?p.b<q.b:p.a<q.a;}

int main()
{
    while(~scanf("%d",&n)) {
        fac[0]=1;
        inc(i,1,n) {
            scanf("%d%d",&t[i].a,&t[i].b);
            fac[i]=1LL*fac[i-1]*i%mod;
        }

	int ans=fac[n],tmp=1;
	sort(t+1,t+n+1,cmp1);
	for(int i=1,j;i<=n;i=j){
		j=i;
		while(j<=n&&t[j].a==t[i].a) j++;
		tmp=1ll*tmp*fac[j-i]%mod;
	}
	ans=(ans-tmp+mod)%mod;

	tmp=1;
	sort(t+1,t+n+1,cmp2);
	for(int i=1,j;i<=n;i=j){
		j=i;
		while(j<=n&&t[j].b==t[i].b) j++;
		tmp=1ll*tmp*fac[j-i]%mod;
	}
	ans=(ans-tmp+mod)%mod;

	bool OK=1;
	for(int i=1;i<n;i++)if(t[i].a>t[i+1].a) OK=0;
	if(OK) {
        tmp=1;
        for(int i=1,j;i<=n;i=j){
            j=i;
            while(j<=n&&t[j].b==t[i].b&&t[j].a==t[i].a) j++;
            tmp=1ll*tmp*fac[j-i]%mod;
        }
        ans=(ans+tmp)%mod;
	}
	printf("%d\n",ans);
//        int ans=fac[n],tmp;
//        sort(t+1,t+1+n,cmp1); tmp=1;
//        for(int l=1,r;l<=n;l=r) {
//            r=l;
//            while(r<=n && t[r].a==t[l].a) r++;
//            tmp=1LL*tmp*fac[r-l]%mod;
//        }
//        ans=(ans-tmp+mod)%mod;
//
//        sort(t+1,t+1+n,cmp2); tmp=1;
//        for(int l=1,r;l<=n;l=r) {
//            r=l;
//            while(r<=n && t[r].b==t[l].b) r++;
//            tmp=1LL*tmp*fac[r-l]%mod;
//        }
//        ans=(ans-tmp+mod)%mod;
//
//        bool OK=1;
//        inc(i,1,n-1) if(t[i+1].a<t[i].a) OK=0;
//        if(OK) {
//            tmp=1;
//            for(int l=1,r;l<=n;l=r) {
//                r=l;
//                while(r<=n && t[r].a==t[l].a&&t[r].b==t[l].b) r++;
//                tmp=1LL*tmp*fac[r-l]%mod;
//            }
//            ans=(ans+tmp)%mod;
//        }
//        printf("%d\n",ans);
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
#define mem(i,j) memset(i,j,sizeof(i))
#define bug(args...) cout<<#args<<"->"<<args<<"\n";
const int N=3e5+5;
const int mod=998244853;

int fac[N];

int n;
struct NODE { int a,b; }t[N];
bool cmp1(NODE & p,NODE & q) {
    if(p.a==q.a) return p.b<q.b;
    return p.a<q.a;
}
bool cmp2(NODE & p,NODE & q) {
    if(p.b==q.b) return p.a<q.a;
    return p.b<q.b;
}

int main()
{
    fac[0]=1;
    inc(i,1,N-1) fac[i]=1LL*fac[i-1]*i%mod;

    while(~scanf("%d",&n)) {
        inc(i,1,n) scanf("%d%d",&t[i].a,&t[i].b);

        int ans=fac[n],l,r,tmp;

        sort(t+1,t+1+n,cmp1);
        l=1; tmp=1;
        for(l=1;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].a==t[l].a) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;

        sort(t+1,t+1+n,cmp2);
        l=1; tmp=1;
        for(l=1;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].b==t[l].b) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;

        bool OK=1;
        inc(i,1,n-1) if(t[i+1].a<t[i].a) OK=0;
        if(OK) {
            l=1; tmp=1;
            for(l=1;l<=n;l=r) {
                r=l;
                while(r<=n && t[r].a==t[l].a&&t[r].b==t[l].b) r++;
                tmp=1LL*tmp*fac[r-l]%mod;
            }
            ans=(ans+tmp)%mod;
        }
        printf("%d\n",ans);
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
#define mem(i,j) memset(i,j,sizeof(i))
#define bug(args...) cout<<#args<<"->"<<args<<"\n";
const int N=3e5+5;
const int mod=998244853;

int n, fac[N];
struct NODE { int a,b; }t[N];
bool cmp1(NODE & p,NODE & q){return p.a!=q.a?p.a<q.a:p.b<q.b;}
bool cmp2(NODE & p,NODE & q){return p.b!=q.b?p.b<q.b:p.a<q.a;}

int main()
{
    while(~scanf("%d",&n)) {
        fac[0]=1;
        inc(i,1,n) {
            scanf("%d%d",&t[i].a,&t[i].b);
            fac[i]=1LL*fac[i-1]*i%mod;
        }

        int ans=fac[n],tmp;
        sort(t+1,t+1+n,cmp1); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].a==t[l].a) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;

        sort(t+1,t+1+n,cmp2); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].b==t[l].b) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;

        bool OK=1;
        inc(i,1,n-1) if(t[i+1].a<t[i].a) OK=0;
        if(OK) {
            tmp=1;
            for(int l=1,r;l<=n;l=r) {
                r=l;
                while(r<=n && t[r].a==t[l].a&&t[r].b==t[l].b) r++;
                tmp=1LL*tmp*fac[r-l]%mod;
            }
            ans=(ans+tmp)%mod;
        }
        printf("%d\n",ans);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=3e5+50,mod=998244353;
int n,ans,J[N],anss=1;
struct node{int x,y;}a[N];
bool cmp1(node a,node b){return a.x!=b.x?a.x<b.x:a.y<b.y;}
bool cmp2(node a,node b){return a.y!=b.y?a.y<b.y:a.x<b.x;}
int main(){
	scanf("%d",&n);
	J[0]=1;
	for(int i=1;i<=n;i++)J[i]=1ll*J[i-1]*i%mod;
	ans=J[n];
	for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y);
	sort(a+1,a+n+1,cmp1);
	for(int i=1,j;i<=n;i++){
		j=i;
		while(j<n&&a[j+1].x==a[j].x)j++;
		anss=1ll*anss*J[j-i+1]%mod;
		i=j;
	}
	ans=(ans-anss+mod)%mod;
	//if(n==300000) cout<<ans<<endl;
	anss=1;
	sort(a+1,a+n+1,cmp2);
	for(int i=1,j;i<=n;i++){
		j=i;
		while(j<n&&a[j+1].y==a[j].y)j++;
		anss=1ll*anss*J[j-i+1]%mod;
		i=j;
	}
	ans=(ans-anss+mod)%mod;
	//if(n==300000) cout<<ans<<endl;
	for(int i=1;i<n;i++)if(a[i].x>a[i+1].x)
        printf("%d\n",(ans+mod)%mod),exit(0);
	anss=1;
	for(int i=1,j;i<=n;i++){
		j=i;
		while(j<n&&a[j+1].y==a[j].y&&a[j+1].x==a[j].x)j++;
		anss=1ll*anss*J[j-i+1]%mod;
		i=j;
	}
	ans=(ans+anss)%mod;
	printf("%d\n",(ans+mod)%mod);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
#define mem(i,j) memset(i,j,sizeof(i))
#define bug(args...) cout<<#args<<"->"<<args<<"\n";
const int N=3e5+5;
const int mod=998244853;

int n, fac[N];
struct NODE { int a,b; }t[N];
bool cmp1(NODE & p,NODE & q){return p.a!=q.a?p.a<q.a:p.b<q.b;}
bool cmp2(NODE & p,NODE & q){return p.b!=q.b?p.b<q.b:p.a<q.a;}

int main()
{
    while(~scanf("%d",&n)) {
        fac[0]=1;
        for(int i=1;i<=n;i++) {
            scanf("%d%d",&t[i].a,&t[i].b);
            fac[i]=1LL*fac[i-1]*i%mod;
        }

        int ans=fac[n],tmp;
        sort(t+1,t+1+n,cmp1); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].a==t[l].a) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;

        sort(t+1,t+1+n,cmp2); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].b==t[l].b) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;

        bool OK=1;
        for(int i=1;i<n;i++) if(t[i+1].a<t[i].a) OK=0;
        if(OK) {
            tmp=1;
            for(int l=1,r;l<=n;l=r) {
                r=l;
                while(r<=n && t[r].a==t[l].a&&t[r].b==t[l].b) r++;
                tmp=1LL*tmp*fac[r-l]%mod;
            }
            ans=(ans+tmp)%mod;
        }
        printf("%d\n",ans);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=3e5+50,mod=998244353;
int n,fac[N];
struct node{int a,b;}t[N];
bool cmp1(node p,node q){return p.a!=q.a?p.a<q.a:p.b<q.b;}
bool cmp2(node p,node q){return p.b!=q.b?p.b<q.b:p.a<q.a;}
int main(){
	scanf("%d",&n);
	fac[0]=1;
	for(int i=1;i<=n;i++) {
        scanf("%d%d",&t[i].a,&t[i].b);
        fac[i]=1ll*fac[i-1]*i%mod;
	}

        int ans=fac[n],tmp;
        sort(t+1,t+1+n,cmp1); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].a==t[l].a) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;

        sort(t+1,t+1+n,cmp2); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].b==t[l].b) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;

        bool OK=1;
        inc(i,1,n-1) if(t[i+1].a<t[i].a) OK=0;
        if(OK) {
            tmp=1;
            for(int l=1,r;l<=n;l=r) {
                r=l;
                while(r<=n && t[r].a==t[l].a&&t[r].b==t[l].b) r++;
                tmp=1LL*tmp*fac[r-l]%mod;
            }
            ans=(ans+tmp)%mod;
        }
        printf("%d\n",ans);

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
#define mem(i,j) memset(i,j,sizeof(i))
#define bug(args...) cout<<#args<<"->"<<args<<"\n";
const int N=3e5+5;
const LL mod=998244353;

int n;
struct NODE { int a,b,id; }s[N];
bool cmp(NODE & p,NODE & q) {
    if(p.a==q.a) return p.b>q.b;
    return p.a>q.a;
}

LL t[N];
void add(int u) {
    while(u<N) {
        t[u]++; u+=-u&u;
    }
}
LL sum(int u) {
    LL res=0LL;
    while(u) {
        res+=t[u]; u-=-u&u;
    } return res;
}

LL Cantor()
{
    mem(t,0LL);
    LL res=0LL,fac=1LL,num=1LL;
    dec(i,n,1) {
        LL cnt=sum(s[i].id);
        add(s[i].id); //printf("%lld ",cnt);
        res=(res+cnt*fac%mod)%mod; //printf("%lld\n",fac);
        fac=fac*num%mod; num++;
    }
    return (res+1LL)%mod;
}

int main()
{
    while(~scanf("%d",&n)) {
        inc(i,1,n) {
            scanf("%d%d",&s[i].a,&s[i].b);
            s[i].id=i;
        }
        sort(s+1,s+1+n,cmp);

        inc(i,1,n) {
            int j=i;
            while(j<=n && s[j].a==s[i].a) j++;
            if(s[j-1].b<s[i].b) break;
            swap(s[i],s[j-1]); i=j;
        }

        bool OK=0;
        inc(i,1,n-1) if(s[i].b>s[i+1].b) OK=1;
        if(OK==0) {
            inc(i,1,n-1) if(s[i].a!=s[i+1].a) {
                if(s[i].b<s[i+1].b) {
                    swap(s[i],s[i+1]); break;
                }
            }
        }

        printf("%lld\n",Cantor());
    }

    return 0;
}
/*
5
1 1
1 1
1 1
1 2
1 2
5
1 1
1 1
1 1
2 2
2 2
5
5 1
4 2
3 3
2 4
1 5

*/


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
#define mem(i,j) memset(i,j,sizeof(i))
#define bug(args...) cout<<#args<<"->"<<args<<"\n";
const int N=3e5+5;
const int mod=998244853;

int fac[N];

int n;
struct NODE { int a,b; }t[N];
bool cmp1(NODE & p,NODE & q) {
    if(p.a==q.a) return p.b<q.b;
    return p.a<q.a;
}
bool cmp2(NODE & p,NODE & q) {
    if(p.b==q.b) return p.a<q.a;
    return p.b<q.b;
}

int main()
{
    while(~scanf("%d",&n)) {
        fac[0]=1;
        inc(i,1,n) {
            scanf("%d%d",&t[i].a,&t[i].b);
            fac[i]=1LL*fac[i-1]*i%mod;
        }

        int ans=fac[n],tmp;

        sort(t+1,t+1+n,cmp1); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].a==t[l].a) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;
        //if(n==300000) printf("%d\n",ans);

        sort(t+1,t+1+n,cmp2); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].b==t[l].b) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;
        //if(n==300000) printf("%d\n",ans);

        bool OK=1;
        inc(i,1,n-1) if(t[i+1].a<t[i].a) OK=0;
        if(OK) {
            tmp=1;
            for(int l=1,r;l<=n;l=r) {
                r=l;
                while(r<=n && t[r].a==t[l].a&&t[r].b==t[l].b) r++;
                tmp=1LL*tmp*fac[r-l]%mod;
            }
            ans=(ans+tmp)%mod;
        }
        printf("%d\n",ans);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=3e5+50,mod=998244353;
int n,ans,J[N],anss=1;
struct node{int x,y;}a[N];
bool cmp1(node a,node b){return a.x!=b.x?a.x<b.x:a.y<b.y;}
bool cmp2(node a,node b){return a.y!=b.y?a.y<b.y:a.x<b.x;}
int main(){
	scanf("%d",&n);
	J[0]=1;
	for(int i=1;i<=n;i++) {
        scanf("%d%d",&a[i].x,&a[i].y);
        J[i]=1ll*J[i-1]*i%mod;
	}

	ans=J[n];
	sort(a+1,a+n+1,cmp1);
	for(int i=1,j;i<=n;i=j){
		j=i;
		while(j<n&&a[j].x==a[i].x) j++;
		anss=1ll*anss*J[j-i]%mod;
	}
	ans=(ans-anss+mod)%mod;

	anss=1;
	sort(a+1,a+n+1,cmp2);
	for(int i=1,j;i<=n;i=j){
		j=i;
		while(j<n&&a[j].y==a[i].y) j++;
		anss=1ll*anss*J[j-i]%mod;
	}
	ans=(ans-anss+mod)%mod;

	for(int i=1;i<n;i++)if(a[i].x>a[i+1].x)
        printf("%d\n",(ans+mod)%mod),exit(0);
	anss=1;
	for(int i=1,j;i<=n;i++){
		j=i;
		while(j<n&&a[j+1].y==a[j].y&&a[j+1].x==a[j].x)j++;
		anss=1ll*anss*J[j-i+1]%mod;
		i=j;
	}
	ans=(ans+anss)%mod;
	printf("%d\n",(ans+mod)%mod);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=3e5+50,mod=998244353;
int n,ans,J[N],anss=1;
struct node{int x,y;}a[N];
bool cmp1(node a,node b){return a.x!=b.x?a.x<b.x:a.y<b.y;}
bool cmp2(node a,node b){return a.y!=b.y?a.y<b.y:a.x<b.x;}
int main(){
	scanf("%d",&n);
	J[0]=1;
	for(int i=1;i<=n;i++) {
        scanf("%d%d",&a[i].x,&a[i].y);
        J[i]=1ll*J[i-1]*i%mod;
	}

	ans=J[n];
	sort(a+1,a+n+1,cmp1);
	for(int i=1,j;i<=n;i=j){
		j=i;
		while(j<=n&&a[j].x==a[i].x) j++;
		anss=1ll*anss*J[j-i]%mod;
	}
	ans=(ans-anss+mod)%mod;

	anss=1;
	sort(a+1,a+n+1,cmp2);
	for(int i=1,j;i<=n;i=j){
		j=i;
		while(j<=n&&a[j].y==a[i].y) j++;
		anss=1ll*anss*J[j-i]%mod;
	}
	ans=(ans-anss+mod)%mod;

	for(int i=1;i<n;i++)if(a[i].x>a[i+1].x)
        printf("%d\n",(ans+mod)%mod),exit(0);
	anss=1;
	for(int i=1,j;i<=n;i++){
		j=i;
		while(j<n&&a[j+1].y==a[j].y&&a[j+1].x==a[j].x)j++;
		anss=1ll*anss*J[j-i+1]%mod;
		i=j;
	}
	ans=(ans+anss)%mod;
	printf("%d\n",(ans+mod)%mod);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
#define mem(i,j) memset(i,j,sizeof(i))
#define bug(args...) cout<<#args<<"->"<<args<<"\n";
const int N=3e5+5;
const int mod=998244853;

LL fac[N];

LL n;
struct NODE { LL a,b; }t[N];
bool cmp1(NODE & p,NODE & q){return p.a!=q.a?p.a<q.a:p.b<q.b;}
bool cmp2(NODE & p,NODE & q){return p.b!=q.b?p.b<q.b:p.a<q.a;}

int main()
{
    while(~scanf("%lld",&n)) {
        fac[0]=1LL;
        inc(i,1,n) {
            scanf("%lld%lld",&t[i].a,&t[i].b);
            fac[i]=fac[i-1]*i%mod;
        }
        LL ans=fac[n],tmp;

        sort(t+1,t+1+n,cmp1); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].a==t[l].a) r++;
            tmp=tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;

        sort(t+1,t+1+n,cmp2); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].b==t[l].b) r++;
            tmp=tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;

        bool OK=1;
        inc(i,1,n-1) if(t[i+1].a<t[i].a) OK=0;
        if(OK) {
            tmp=1LL;
            for(int l=1,r;l<=n;l=r) {
                r=l;
                while(r<=n && t[r].a==t[l].a&&t[r].b==t[l].b) r++;
                tmp=tmp*fac[r-l]%mod;
            }
            ans=(ans+tmp)%mod;
        }
        printf("%d\n",ans);
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
#define mem(i,j) memset(i,j,sizeof(i))
#define bug(args...) cout<<#args<<"->"<<args<<"\n";
const int N=3e5+5;
const int mod=998244853;

int fac[N];

int n;
struct NODE { int a,b; }t[N];
bool cmp1(NODE & p,NODE & q){return p.a!=q.a?p.a<q.a:p.b<q.b;}
bool cmp2(NODE & p,NODE & q){return p.b!=q.b?p.b<q.b:p.a<q.a;}

int main()
{
    while(~scanf("%d",&n)) {
        fac[0]=1;
        inc(i,1,n) {
            scanf("%d%d",&t[i].a,&t[i].b);
            fac[i]=1LL*fac[i-1]*i%mod;
        }

        int ans=fac[n],tmp;

        sort(t+1,t+1+n,cmp1); tmp=1;
        for(int l=1,r;l<=n;l=r+1) {
            r=l;
            while(r<n && t[r+1].a==t[r].a) r++;
            tmp=1LL*tmp*fac[r-l+1]%mod;
        }
        ans=(ans-tmp+mod)%mod;
        //if(n==300000) printf("%d\n",ans);

        sort(t+1,t+1+n,cmp2); tmp=1;
        for(int l=1,r;l<=n;l=r+1) {
            r=l;
            while(r<n && t[r+1].b==t[r].b) r++;
            tmp=1LL*tmp*fac[r-l+1]%mod;
        }
        ans=(ans-tmp+mod)%mod;
        //if(n==300000) printf("%d\n",ans);

        bool OK=1;
        inc(i,1,n-1) if(t[i+1].a<t[i].a) OK=0;
        if(OK) {
            tmp=1;
            for(int l=1,r;l<=n;l=r) {
                r=l;
                while(r<=n && t[r].a==t[l].a&&t[r].b==t[l].b) r++;
                tmp=1LL*tmp*fac[r-l]%mod;
            }
            ans=(ans+tmp)%mod;
        }
        printf("%d\n",ans);
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
#define mem(i,j) memset(i,j,sizeof(i))
#define bug(args...) cout<<#args<<"->"<<args<<"\n";
const int N=3e5+5;
const LL mod=998244353;

int n;
struct NODE { int a,b,id; }s[N];
bool cmp(NODE & p,NODE & q) {
    if(p.a==q.a) return p.b>q.b;
    return p.a>q.a;
}

LL t[N];
void add(int u) {
    while(u<N) {
        t[u]++; u+=-u&u;
    }
}
LL sum(int u) {
    LL res=0LL;
    while(u) {
        res+=t[u]; u-=-u&u;
    } return res;
}

void Cantor()
{
    mem(t,0LL);
    LL res=0LL,fac=1LL,num=1LL;
    dec(i,n,1) {
        LL cnt=sum(s[i].id);
        add(s[i].id); //printf("%lld ",cnt);
        res=(res+cnt*fac%mod)%mod; //printf("%lld\n",fac);
        fac=fac*num%mod; num++;
    }
    printf("%lld\n",(res+1LL)%mod);
}

int main()
{
    while(~scanf("%d",&n)) {
        inc(i,1,n) {
            scanf("%d%d",&s[i].a,&s[i].b);
            s[i].id=i;
        }
        sort(s+1,s+1+n,cmp);

        inc(i,1,n) {
            int j=i;
            while(j<=n && s[j].a==s[i].a) j++;
            if(s[j-1].b<s[i].b) break;
            swap(s[i],s[j-1]); i=j;
        }

        bool OK=0;
        inc(i,1,n-1) if(s[i].b>s[i+1].b) OK=1;
        if(OK==0) {
            inc(i,1,n-1) if(s[i].a!=s[i+1].a) {
                if(s[i].b<s[i+1].b) {
                    swap(s[i],s[i+1]); break;
                }
            }
        }
        Cantor();
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=3e5+50,mod=998244353;
int n,ans,J[N],anss=1;
struct node{int x,y;}a[N];
bool cmp1(node a,node b){return a.x!=b.x?a.x<b.x:a.y<b.y;}
bool cmp2(node a,node b){return a.y!=b.y?a.y<b.y:a.x<b.x;}
int main(){
	scanf("%d",&n);
	J[0]=1;
	for(int i=1;i<=n;i++)J[i]=1ll*J[i-1]*i%mod;
	ans=J[n];
	for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y);
	sort(a+1,a+n+1,cmp1);
	for(int i=1,j;i<=n;i++){
		j=i;
		while(j<n&&a[j+1].x==a[j].x)j++;
		anss=1ll*anss*J[j-i+1]%mod;
		i=j;
	}
	ans=(ans-anss+mod)%mod;
	if(n==300000) cout<<ans<<endl;
	anss=1;
	sort(a+1,a+n+1,cmp2);
	for(int i=1,j;i<=n;i++){
		j=i;
		while(j<n&&a[j+1].y==a[j].y)j++;
		anss=1ll*anss*J[j-i+1]%mod;
		i=j;
	}
	ans=(ans-anss+mod)%mod;
	if(n==300000) cout<<ans<<endl;
	for(int i=1;i<n;i++)if(a[i].x>a[i+1].x)printf("%d\n",(ans+mod)%mod),exit(0);
	anss=1;
	for(int i=1,j;i<=n;i++){
		j=i;
		while(j<n&&a[j+1].y==a[j].y&&a[j+1].x==a[j].x)j++;
		anss=1ll*anss*J[j-i+1]%mod;
		i=j;
	}
	ans=(ans+anss)%mod;
	printf("%d\n",(ans+mod)%mod);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
#define mem(i,j) memset(i,j,sizeof(i))
#define bug(args...) cout<<#args<<"->"<<args<<"\n";
const int N=3e5+5;
const int mod=998244353;

int n, fac[N];
struct NODE { int a,b; }t[N];
bool cmp1(NODE & p,NODE & q){return p.a!=q.a?p.a<q.a:p.b<q.b;}
bool cmp2(NODE & p,NODE & q){return p.b!=q.b?p.b<q.b:p.a<q.a;}

int main()
{
    while(~scanf("%d",&n)) {
        fac[0]=1;
        for(int i=1;i<=n;i++) {
            scanf("%d%d",&t[i].a,&t[i].b);
            fac[i]=1LL*fac[i-1]*i%mod;
        }

        int ans=fac[n],tmp;
        sort(t+1,t+1+n,cmp1); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].a==t[l].a) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;

        sort(t+1,t+1+n,cmp2); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].b==t[l].b) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;

        bool OK=1;
        for(int i=1;i<n;i++) if(t[i+1].a<t[i].a) OK=0;
        if(OK) {
            tmp=1;
            for(int l=1,r;l<=n;l=r) {
                r=l;
                while(r<=n && t[r].a==t[l].a&&t[r].b==t[l].b) r++;
                tmp=1LL*tmp*fac[r-l]%mod;
            }
            ans=(ans+tmp)%mod;
        }
        printf("%d\n",ans);
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
#define mem(i,j) memset(i,j,sizeof(i))
#define bug(args...) cout<<#args<<"->"<<args<<"\n";
const int N=3e5+5;
const int mod=998244853;

int fac[N];

int n;
struct NODE { int a,b; }t[N];
bool cmp1(NODE & p,NODE & q){return p.a!=q.a?p.a<q.a:p.b<q.b;}
bool cmp2(NODE & p,NODE & q){return p.b!=q.b?p.b<q.b:p.a<q.a;}

int main()
{
    while(~scanf("%d",&n)) {
        fac[0]=1;
        inc(i,1,n) {
            scanf("%d%d",&t[i].a,&t[i].b);
            fac[i]=1LL*fac[i-1]*i%mod;
        }

        int ans=fac[n],tmp;

        sort(t+1,t+1+n,cmp1); tmp=1;
        for(int i=1,j;i<=n;i++){
            j=i;
            while(j<n&&t[j+1].a==t[j].a)j++;
            tmp=1ll*tmp*fac[j-i+1]%mod;
            i=j;
        }
        ans=(ans-tmp+mod)%mod;

        sort(t+1,t+1+n,cmp2); tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].b==t[l].b) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;
        //if(n==300000) printf("%d\n",ans);

        bool OK=1;
        inc(i,1,n-1) if(t[i+1].a<t[i].a) OK=0;
        if(OK) {
            tmp=1;
            for(int l=1,r;l<=n;l=r) {
                r=l;
                while(r<=n && t[r].a==t[l].a&&t[r].b==t[l].b) r++;
                tmp=1LL*tmp*fac[r-l]%mod;
            }
            ans=(ans+tmp)%mod;
        }
        printf("%d\n",ans);
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
#define mem(i,j) memset(i,j,sizeof(i))
#define bug(args...) cout<<#args<<"->"<<args<<"\n";
const int N=3e5+5;
const int mod=998244853;

int fac[N];

int n;
struct NODE { int a,b; }t[N];
bool cmp1(NODE & p,NODE & q) {
    if(p.a==q.a) return p.b<q.b;
    return p.a<q.a;
}
bool cmp2(NODE & p,NODE & q) {
    if(p.b==q.b) return p.a<q.a;
    return p.b<q.b;
}

int main()
{
    fac[0]=1;
    inc(i,1,N-1) fac[i]=1LL*fac[i-1]*i%mod;

    while(~scanf("%d",&n)) {
        inc(i,1,n) scanf("%d%d",&t[i].a,&t[i].b);

        int ans=fac[n],tmp;

        sort(t+1,t+1+n,cmp1);
        tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].a==t[l].a) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;
        if(n==300000) printf("%d\n",ans);

        sort(t+1,t+1+n,cmp2);
        tmp=1;
        for(int l=1,r;l<=n;l=r) {
            r=l;
            while(r<=n && t[r].b==t[l].b) r++;
            tmp=1LL*tmp*fac[r-l]%mod;
        }
        ans=(ans-tmp+mod)%mod;
        if(n==300000) printf("%d\n",ans);

        bool OK=1;
        inc(i,1,n-1) if(t[i+1].a<t[i].a) OK=0;
        if(OK) {
            tmp=1;
            for(int l=1,r;l<=n;l=r) {
                r=l;
                while(r<=n && t[r].a==t[l].a&&t[r].b==t[l].b) r++;
                tmp=1LL*tmp*fac[r-l]%mod;
            }
            ans=(ans+tmp)%mod;
        }
        printf("%d\n",ans);
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx=1e5+5;
ll n;
const ll mod=998244353 ;
bool comp(pair<int,int>a,pair<int,int>b){

    if(a.second!=b.second){
        return a.second<b.second;
    }else{
        return a.first<b.first;
    }
}
int main() {
cin>>n;

pair<int,int>p[n];
pair<int,int>p2[n];
for(int i=0;i<n;i++){

    cin>>p[i].first>>p[i].second;
   // cin>>p2[i].first>>p2[i].second;
}
sort(p,p+n);
ll ans1=1;
ll ans2=1;
ll ans3=1;
ll c=0;ll temp=0;
for(int i=0;i<n;i++){
    if(i==0){
        c++;temp=1;
    }else{

        if(p[i].first==p[i-1].first){
            c++;
            temp=(temp*c+mod)%mod;
        }else{
              ans1=(ans1*temp+mod)%mod;
            c=1;
            temp=1;
        }
    }

}

ans1=(ans1*temp+mod)%mod;
//cout<<c;
temp=0;c=0;
sort(p,p+n,comp);
for(int i=0;i<n;i++){
    if(i==0){
        c++;temp=1;
    }else{

        if(p[i].second==p[i-1].second){
            c++;
            temp=(temp*c+mod)%mod;
        }else{
            c=1;
            ans2=(ans2*temp+mod)%mod;
            temp=1;
        }
    }

}ans2=(ans2*temp+mod)%mod;
c=0;temp=0;
for(int i=0;i<n;i++){
    if(i==0){
        c++;temp=1;
    }else{
        if(p[i].first<p[i-1].first){ans3=0;}
        if(p[i].second==p[i-1].second&&p[i].first==p[i].first){
            c++;
            temp=(temp*c+mod)%mod;
        }else{
              ans3=(ans3*temp+mod)%mod;
            c=1;
            temp=1;
        }
    }

}
if(c){
    ans3=(ans3*temp+mod)%mod;
}ll total=1;;
for(int i=2;i<=n;i++){
    total=(total*i+mod)%mod;
}cout<<(total-(ans1+ans2)+ans3+mod)%mod;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx=1e5+5;
ll n;
const ll mod=998244353 ;
bool comp(pair<int,int>a,pair<int,int>b){

    if(a.second!=b.second){
        return a.second<b.second;
    }else{
        return a.first<b.first;
    }
}
int main() {
cin>>n;

pair<int,int>p[n];
pair<int,int>p2[n];
for(int i=0;i<n;i++){

    cin>>p[i].first>>p[i].second;
   // cin>>p2[i].first>>p2[i].second;
}
sort(p,p+n);
ll ans1=1;
ll ans2=1;
ll ans3=1;
ll c=0;ll temp=0;
for(int i=0;i<n;i++){
    if(i==0){
        c++;temp=1;
    }else{

        if(p[i].first==p[i-1].first){
            c++;
            temp=(temp*c+mod)%mod;
        }else{
              ans1=(ans1*temp+mod)%mod;
            c=1;
            temp=1;
        }
    }

}

ans1=(ans1*temp+mod)%mod;
//cout<<c;
temp=0;c=0;
sort(p,p+n,comp);
for(int i=0;i<n;i++){
    if(i==0){
        c++;temp=1;
    }else{

        if(p[i].second==p[i-1].second){
            c++;
            temp=(temp*c+mod)%mod;
        }else{
            c=1;
            ans2=(ans2*temp+mod)%mod;
            temp=1;
        }
    }

}ans2=(ans2*temp)%mod;
c=0;temp=0;
for(int i=0;i<n;i++){
    if(i==0){
        c++;temp=1;
    }else{
        if(p[i].first<p[i-1].first){ans3=0;}
        if(p[i].second==p[i-1].second&&p[i].first==p[i].first){
            c++;
            temp=(temp*c+mod)%mod;
        }else{
              ans3=(ans3*temp+mod)%mod;
            c=1;
            temp=1;
        }
    }

}
if(c){
    ans3=(ans3*temp+mod)%mod;
}ll total=1;;
for(int i=2;i<=n;i++){
    total=total*i;
}cout<<total-(ans1+ans2)+ans3;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx=1e5+5;
ll n;
const ll mod=998244353 ;
bool comp(pair<int,int>a,pair<int,int>b){

    if(a.second!=b.second){
        return a.second<b.second;
    }else{
        return a.first<b.first;
    }
}
int main() {
cin>>n;

pair<int,int>p[n];
pair<int,int>p2[n];
for(int i=0;i<n;i++){

    cin>>p[i].first>>p[i].second;
   // cin>>p2[i].first>>p2[i].second;
}
sort(p,p+n);
ll ans1=1;
ll ans2=1;
ll ans3=1;
ll c=0;ll temp=0;
for(int i=0;i<n;i++){
    if(i==0){
        c++;temp=1;
    }else{

        if(p[i].first==p[i-1].first){
            c++;
            temp=(temp*c+mod)%mod;
        }else{
              ans1=(ans1*temp+mod)%mod;
            c=1;
            temp=1;
        }
    }

}

ans1=(ans1*temp+mod)%mod;
//cout<<c;
temp=0;c=0;
sort(p,p+n,comp);
for(int i=0;i<n;i++){
    if(i==0){
        c++;temp=1;
    }else{

        if(p[i].second==p[i-1].second){
            c++;
            temp=(temp*c+mod)%mod;
        }else{
            c=1;
            ans2=(ans2*temp+mod)%mod;
            temp=1;
        }
    }

}ans2=(ans2*temp+mod)%mod;
c=0;temp=0;
for(int i=0;i<n;i++){
    if(i==0){
        c++;temp=1;
    }else{
        if(p[i].first<p[i-1].first){ans3=0;}
        if(p[i].second==p[i-1].second&&p[i].first==p[i-1].first){
            c++;
            temp=(temp*c+mod)%mod;
        }else{
              ans3=(ans3*temp+mod)%mod;
            c=1;
            temp=1;
        }
    }

}
if(c){
    ans3=(ans3*temp+mod)%mod;
}ll total=1;;
for(int i=2;i<=n;i++){
    total=(total*i+mod)%mod;
}cout<<(total-(ans1+ans2)+ans3%mod;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx=1e5+5;
ll n;
const ll mod=998244353 ;
bool comp(pair<int,int>a,pair<int,int>b){

    if(a.second!=b.second){
        return a.second<b.second;
    }else{
        return a.first<b.first;
    }
}
int main() {
cin>>n;

pair<int,int>p[n];
pair<int,int>p2[n];
for(int i=0;i<n;i++){

    cin>>p[i].first>>p[i].second;
   // cin>>p2[i].first>>p2[i].second;
}
sort(p,p+n);
ll ans1=1;
ll ans2=1;
ll ans3=1;
ll c=0;ll temp=0;
for(int i=0;i<n;i++){
    if(i==0){
        c++;temp=1;
    }else{

        if(p[i].first==p[i-1].first){
            c++;
            temp=(temp*c+mod)%mod;
        }else{
              ans1=(ans1*temp+mod)%mod;
            c=1;
            temp=1;
        }
    }

}

ans1=(ans1*temp+mod)%mod;
//cout<<c;
temp=0;c=0;
sort(p,p+n,comp);
for(int i=0;i<n;i++){
    if(i==0){
        c++;temp=1;
    }else{

        if(p[i].second==p[i-1].second){
            c++;
            temp=(temp*c+mod)%mod;
        }else{
            c=1;
            ans2=(ans2*temp+mod)%mod;
            temp=1;
        }
    }

}ans2=(ans2*temp+mod)%mod;
c=0;temp=0;
for(int i=0;i<n;i++){
    if(i==0){
        c++;temp=1;
    }else{
        if(p[i].first<p[i-1].first){ans3=0;}
        if(p[i].second==p[i-1].second&&p[i].first==p[i-1].first){
            c++;
            temp=(temp*c+mod)%mod;
        }else{
              ans3=(ans3*temp+mod)%mod;
            c=1;
            temp=1;
        }
    }

}
if(c){
    ans3=(ans3*temp+mod)%mod;
}ll total=1;;
for(int i=2;i<=n;i++){
    total=(total*i+mod)%mod;
}ll answer=(total-(ans1+ans2)+ans3+mod)%mod;if(answer<mod){answer+=mod;}cout<<answer;
}


#include<bits/stdc++.h>
using namespace std;

using ll = long long;
const ll MOD = 998244353;

ll numWays(vector<pair<int, int>> v) {
	sort(v.begin(), v.end());
	pair<int, int> prv(-1, -1);
	int c = 1;
	ll res = 1;
	for (auto cur : v) {
		assert(cur.first >= prv.first);
		if (cur.second < prv.second) return 0;
		if (cur != prv) prv = cur, c = 0;
		res *= ++c;
		res %= MOD;
	}
	return res;
}

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int N; cin >> N;
	vector<pair<int, int>> v, a, b, c;
	for (int i = 0; i < N; i++) {
		int x, y; cin >> x >> y;
		v.emplace_back(x, y);
		a.emplace_back(x, 0);
		b.emplace_back(y, 0);
		c.emplace_back(0, 0);
	}
	ll ans = numWays(c) + numWays(v) - numWays(a) - numWays(b);
	ans %= MOD;
	if (ans < 0) ans += MOD;
	cout << ans << '\n';

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx=1e5+5;
ll n;
const ll mod=998244353 ;
bool comp(pair<int,int>a,pair<int,int>b){

    if(a.second!=b.second){
        return a.second<b.second;
    }else{
        return a.first<b.first;
    }
}
int main() {
cin>>n;

pair<int,int>p[n];
pair<int,int>p2[n];
for(int i=0;i<n;i++){

    cin>>p[i].first>>p[i].second;
   // cin>>p2[i].first>>p2[i].second;
}
sort(p,p+n);
ll ans1=1;
ll ans2=1;
ll ans3=1;
ll c=0;ll temp=0;
for(int i=0;i<n;i++){
    if(i==0){
        c++;temp=1;
    }else{

        if(p[i].first==p[i-1].first){
            c++;
            temp=(temp*c+mod)%mod;
        }else{
              ans1=(ans1*temp+mod)%mod;
            c=1;
            temp=1;
        }
    }

}

ans1=(ans1*temp+mod)%mod;
//cout<<c;
temp=0;c=0;
sort(p,p+n,comp);
for(int i=0;i<n;i++){
    if(i==0){
        c++;temp=1;
    }else{

        if(p[i].second==p[i-1].second){
            c++;
            temp=(temp*c+mod)%mod;
        }else{
            c=1;
            ans2=(ans2*temp+mod)%mod;
            temp=1;
        }
    }

}ans2=(ans2*temp+mod)%mod;
c=0;temp=0;
for(int i=0;i<n;i++){
    if(i==0){
        c++;temp=1;
    }else{
        if(p[i].first<p[i-1].first){ans3=0;}
        if(p[i].second==p[i-1].second&&p[i].first==p[i-1].first){
            c++;
            temp=(temp*c+mod)%mod;
        }else{
              ans3=(ans3*temp+mod)%mod;
            c=1;
            temp=1;
        }
    }

}
if(c){
    ans3=(ans3*temp+mod)%mod;
}ll total=1;;
for(int i=2;i<=n;i++){
    total=(total*i+mod)%mod;
}cout<<(total-(ans1+ans2)+ans3+mod)%mod;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx=1e5+5;
ll n;
const ll mod=998244353 ;
bool comp(pair<int,int>a,pair<int,int>b){

    if(a.second!=b.second){
        return a.second<b.second;
    }else{
        return a.first<b.first;
    }
}
int main() {
cin>>n;

pair<int,int>p[n];
pair<int,int>p2[n];
for(int i=0;i<n;i++){

    cin>>p[i].first>>p[i].second;
   // cin>>p2[i].first>>p2[i].second;
}
sort(p,p+n);
ll ans1=1;
ll ans2=1;
ll ans3=1;
ll c=0;ll temp=0;
for(int i=0;i<n;i++){
    if(i==0){
        c++;temp=1;
    }else{

        if(p[i].first==p[i-1].first){
            c++;
            temp=(temp*c+mod)%mod;
        }else{
              ans1=(ans1*temp+mod)%mod;
            c=1;
            temp=1;
        }
    }

}

ans1=(ans1*temp+mod)%mod;
//cout<<c;
temp=0;c=0;
sort(p,p+n,comp);
for(int i=0;i<n;i++){
    if(i==0){
        c++;temp=1;
    }else{

        if(p[i].second==p[i-1].second){
            c++;
            temp=(temp*c+mod)%mod;
        }else{
            c=1;
            ans2=(ans2*temp+mod)%mod;
            temp=1;
        }
    }

}ans2=(ans2*temp+mod)%mod;
c=0;temp=0;
for(int i=0;i<n;i++){
    if(i==0){
        c++;temp=1;
    }else{
        if(p[i].first<p[i-1].first){ans3=0;}
        if(p[i].second==p[i-1].second&&p[i].first==p[i-1].first){
            c++;
            temp=(temp*c+mod)%mod;
        }else{
              ans3=(ans3*temp+mod)%mod;
            c=1;
            temp=1;
        }
    }

}
if(c){
    ans3=(ans3*temp+mod)%mod;
}ll total=1;;
for(int i=2;i<=n;i++){
    total=(total*i+mod)%mod;
}ll answer=(total-(ans1+ans2)+ans3+mod)%mod;if(answer<mod){answer+=mod;}cout<<answer%mod;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define mod 998244353
#define endl "\n"
#define pb push_back
#define mp make_pair
ll fac[300005];
bool bo(pair<ll,ll> a,pair<ll,ll> b)
{
    if(a.first!=b.first)
    {
        return a.first<b.first;
    }
    return a.second<b.second;
}
void calf()
{
	fac[0]=1;
	fac[1]=1;
	for(ll i=2;i<=300003;i++)
	{
		fac[i]=i*fac[i-1];
		fac[i]%=mod;
	}
}
int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n,i,j,p=0,x=1,y=1,fr[300005],sr[300005];
	vector<ll> fir[300005],sec[300005];
	vector<pair<ll,ll> > a;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>x>>y;
		a.pb(mp(x,y));
		fir[x].pb(i);
		sec[y].pb(i);
	}
	sort(a.begin(),a.end(),bo);
	calf();
	x=y=1;
	for(i=1;i<=n;i++)
	{
		x*=fac[fir[i].size()];
		y*=fac[sec[i].size()];
		x%=mod;
		y%=mod;
	}
	x+=y;
	x-=p;
	x%=mod;
	y=fac[n];
	y-=x;
	for(i=1;i<n;i++)
	{
		if(a[i].second<a[i-1].second)
		{
         	while(y<0)
        	{
        		y+=mod;
        	}
			cout<<y;
			return 0;
		}
	}
	x=1;
	p=1;
	for(i=1;i<n;i++)
	{
		if(a[i].second==a[i-1].second && a[i-1].first==a[i].first)
		{
			x++;
		}
		else
		{
			p*=fac[x];
			p%=mod;
			x=1;
		}
	}
	y+=p;
	while(y<0)
	{
		y+=mod;
	}
	cout<<y;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define mod 998244353
#define endl "\n"
#define pb push_back
#define mp make_pair
ll fac[300005];
void calf()
{
	fac[0]=1;
	fac[1]=1;
	for(ll i=2;i<=300003;i++)
	{
		fac[i]=i*fac[i-1];
		fac[i]%=mod;
	}
}
int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n,i,j,p=0,x=1,y=1,fr[300005],sr[300005];
	vector<ll> fir[300005],sec[300005];
	vector<pair<ll,ll> > a;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>x>>y;
		a.pb(mp(x,y));
		fir[x].pb(i);
		sec[y].pb(i);
	}
	sort(a.begin(),a.end());
	calf();
	x=y=1;
	for(i=1;i<=n;i++)
	{
		x*=fac[fir[i].size()];
		y*=fac[sec[i].size()];
		x%=mod;
		y%=mod;
	}
	x+=y;
	x-=p;
	x%=mod;
	y=fac[n];
	y-=x;
	for(i=1;i<n;i++)
	{
		if(a[i].second<a[i-1].second)
		{
         	while(y<0)
        	{
        		y+=mod;
        	}
			cout<<y;
			return 0;
		}
	}
	x=1;
	p=1;
	for(i=1;i<n;i++)
	{
		if(a[i].second==a[i-1].second && a[i-1].first==a[i].first)
		{
			x++;
		}
		else
		{
			p*=fac[x];
			p%=mod;
			x=1;
		}
	}
	y+=p;
	while(y<0)
	{
		y+=mod;
	}
	cout<<y;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define mod 998244353
#define endl "\n"
#define pb push_back
#define mp make_pair
ll fac[300005];
bool bo(pair<ll,ll> a,pair<ll,ll> b)
{
    if(a.first!=b.first)
    {
        return a.first<b.first;
    }
    return a.second<b.second;
}
void calf()
{
	fac[0]=1;
	fac[1]=1;
	for(ll i=2;i<=300005;i++)
	{
		fac[i]=i*fac[i-1];
		fac[i]%=mod;
	}
}
int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n,i,j,p=0,x=1,y=1,fr[300005],sr[300005];
	vector<ll> fir[300005],sec[300005];
	vector<pair<ll,ll> > a;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>x>>y;
		a.pb(mp(x,y));
		fir[x].pb(i);
		sec[y].pb(i);
	}
	sort(a.begin(),a.end(),bo);
	calf();
	x=y=1;
	for(i=1;i<=n;i++)
	{
		x*=fac[fir[i].size()];
		y*=fac[sec[i].size()];
		x%=mod;
		y%=mod;
	}
	x+=y;
	x-=p;
	x%=mod;
	y=fac[n];
	y-=x;
	for(i=1;i<n;i++)
	{
		if(a[i].second<a[i-1].second)
		{
         	while(y<0)
        	{
        		y+=mod;
        	}
			cout<<y;
			return 0;
		}
	}
	x=1;
	p=1;
	for(i=1;i<n;i++)
	{
		if(a[i].second==a[i-1].second && a[i-1].first==a[i].first)
		{
			x++;
		}
		else
		{
			p*=fac[x];
			x=1;
		}
	}
	y+=p;
	while(y<0)
	{
		y+=mod;
	}
	cout<<y;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define mod 998244353
#define endl "\n"
#define pb push_back
#define mp make_pair
ll fac[300005];
void calf()
{
	fac[0]=1;
	fac[1]=1;
	for(ll i=2;i<=300003;i++)
	{
		fac[i]=i*fac[i-1];
		fac[i]%=mod;
	}
}
int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n,i,j,p=0,x=1,y=1,fr[300005],sr[300005];
	vector<ll> fir[300005],sec[300005];
	vector<pair<ll,ll> > a;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>x>>y;
		a.pb(mp(x,y));
		fir[x].pb(i);
		sec[y].pb(i);
	}
	sort(a.begin(),a.end());
	calf();
	x=y=1;
	for(i=1;i<=n;i++)
	{
		x*=fac[fir[i].size()];
		y*=fac[sec[i].size()];
		x%=mod;
		y%=mod;
	}
	x+=y;
	x-=p;
	x%=mod;
	y=fac[n];
	y-=x;
	for(i=1;i<n;i++)
	{
		if(a[i].second<a[i-1].second)
		{
         	while(y<0)
        	{
        		y+=mod;
        	}
			cout<<y;
			return 0;
		}
	}
	x=1;
	p=1;
	for(i=1;i<n;i++)
	{
		if(a[i].second==a[i-1].second && a[i-1].first==a[i].first)
		{
			x++;
		}
		else
		{
			p*=fac[x];
			x=1;
		}
	}
	y+=p;
	while(y<0)
	{
		y+=mod;
	}
	cout<<y;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define mod 998244353
#define endl "\n"
#define pb push_back
#define mp make_pair
ll fac[300005];
bool bo(pair<ll,ll> a,pair<ll,ll> b)
{
    if(a.first!=b.first)
    {
        return a.first<b.first;
    }
    return a.second<b.second;
}
void calf()
{
	fac[0]=1;
	fac[1]=1;
	for(ll i=2;i<=300003;i++)
	{
		fac[i]=i*fac[i-1];
		fac[i]%=mod;
	}
}
int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n,i,j,p=0,x=1,y=1,fr[300005],sr[300005];
	vector<ll> fir[300005],sec[300005];
	vector<pair<ll,ll> > a;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>x>>y;
		a.pb(mp(x,y));
		fir[x].pb(i);
		sec[y].pb(i);
	}
	sort(a.begin(),a.end(),bo);
	calf();
	x=y=1;
	for(i=1;i<=n;i++)
	{
		x*=fac[fir[i].size()];
		y*=fac[sec[i].size()];
		x%=mod;
		y%=mod;
	}
	x+=y;
	x-=p;
	x%=mod;
	y=fac[n];
	y-=x;
	for(i=1;i<n;i++)
	{
		if(a[i].second<a[i-1].second)
		{
         	while(y<0)
        	{
        		y+=mod;
        	}
			cout<<y;
			return 0;
		}
	}
	x=1;
	p=1;
	for(i=1;i<n;i++)
	{
		if(a[i].second==a[i-1].second && a[i-1].first==a[i].first)
		{
			x++;
		}
		else
		{
			p*=fac[x];
			p%=mod;
			x=1;
		}
	}
	p*=fac[x];
	p%=mod;
	y+=p;
	p%=mod;
	while(y<0)
	{
		y+=mod;
	}
	cout<<y;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define mod 998244353
#define endl "\n"
#define pb push_back
#define mp make_pair
ll fac[300005];
bool bo(pair<ll,ll> a,pair<ll,ll> b)
{
    if(a.first!=b.first)
    {
        return a.first<b.first;
    }
    return a.second<b.second;
}
void calf()
{
	fac[0]=1;
	fac[1]=1;
	for(ll i=2;i<=300005;i++)
	{
		fac[i]=i*fac[i-1];
		fac[i]%=mod;
	}
}
int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n,i,j,p=0,x=1,y=1,fr[300005],sr[300005];
	vector<ll> fir[300005],sec[300005];
	vector<pair<ll,ll> > a;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>x>>y;
		a.pb(mp(x,y));
		fir[x].pb(i);
		sec[y].pb(i);
	}
	sort(a.begin(),a.end(),bo);
	calf();
	x=y=1;
	for(i=1;i<=n;i++)
	{
		x*=fac[fir[i].size()];
		y*=fac[sec[i].size()];
		x%=mod;
		y%=mod;
	}
	x+=y;
	x-=p;
	x%=mod;
	y=fac[n];
	y-=x;
	for(i=1;i<n;i++)
	{
		if(a[i].second<a[i-1].second)
		{
         	while(y<0)
        	{
        		y+=mod;
        	}
			cout<<y;
			return 0;
		}
	}
	x=1;
	p=1;
	for(i=1;i<n;i++)
	{
		if(a[i].second==a[i-1].second && a[i-1].first==a[i].first)
		{
			x++;
		}
		else
		{
			p*=fac[x];
			p%=mod;
			x=1;
		}
	}
	p*=fac[x];
	p%=mod;
	y+=p;
	p%=mod;
	while(y<0)
	{
		y+=mod;
	}
	cout<<y;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define mod 998244353
#define endl "\n"
#define pb push_back
#define mp make_pair
ll fac[300005];
void calf()
{
	fac[0]=1;
	fac[1]=1;
	for(ll i=2;i<=300005;i++)
	{
		fac[i]=i*fac[i-1];
		fac[i]%=mod;
	}
}
int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n,i,j,p=0,x=1,y=1,fr[300005],sr[300005];
	vector<ll> fir[300005],sec[300005];
	vector<pair<ll,ll> > a;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>x>>y;
		a.pb(mp(x,y));
		fir[x].pb(i);
		sec[y].pb(i);
	}
	sort(a.begin(),a.end());
	calf();
	x=y=1;
	for(i=1;i<=n;i++)
	{
		x*=fac[fir[i].size()];
		y*=fac[sec[i].size()];
		x%=mod;
		y%=mod;
	}
	x+=y;
	x-=p;
	x%=mod;
	y=fac[n];
	y-=x;
	for(i=1;i<n;i++)
	{
		if(a[i].second<a[i-1].second)
		{
         	while(y<0)
        	{
        		y+=mod;
        	}
			cout<<y;
			return 0;
		}
	}
	x=1;
	p=1;
	for(i=1;i<n;i++)
	{
		if(a[i].second==a[i-1].second && a[i-1].first==a[i].first)
		{
			x++;
		}
		else
		{
			p*=fac[x];
			x=1;
		}
	}
	y+=p;
	while(y<0)
	{
		y+=mod;
	}
	cout<<y;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define mod 998244353
#define endl "\n"
#define pb push_back
#define mp make_pair
ll fac[300005];
bool bo(pair<ll,ll> a,pair<ll,ll> b)
{
    if(a.first!=b.first)
    {
        return a.first<b.first;
    }
    return a.second<b.second;
}
void calf()
{
	fac[0]=1;
	fac[1]=1;
	for(ll i=2;i<=300003;i++)
	{
		fac[i]=i*fac[i-1];
		fac[i]%=mod;
	}
}
int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n,i,j,p=0,x=1,y=1,fr[300005],sr[300005];
	vector<ll> fir[300005],sec[300005];
	vector<pair<ll,ll> > a;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>x>>y;
		a.pb(mp(x,y));
		fir[x].pb(i);
		sec[y].pb(i);
	}
	sort(a.begin(),a.end(),bo);
	calf();
	x=y=1;
	for(i=1;i<=n;i++)
	{
		x*=fac[fir[i].size()];
		y*=fac[sec[i].size()];
		x%=mod;
		y%=mod;
	}
	x+=y;
	x-=p;
	x%=mod;
	y=fac[n];
	y-=x;
	for(i=1;i<n;i++)
	{
		if(a[i].second<a[i-1].second)
		{
         	while(y<0)
        	{
        		y+=mod;
        	}
			cout<<y;
			return 0;
		}
	}
	x=1;
	p=1;
	for(i=1;i<n;i++)
	{
		if(a[i].second==a[i-1].second && a[i-1].first==a[i].first)
		{
			x++;
		}
		else
		{
			p*=fac[x];
			p%=mod;
			x=1;
		}
	}
	p*=fac[x];
	p%=mod;
	y+=p;
	y%=mod;
	while(y<0)
	{
		y+=mod;
	}
	cout<<y;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 300010

using namespace std;

long long n,m;
struct  node{
    long long x,y;
}a[MAXN];
long long b1[MAXN];
long long b2[MAXN];
long long b3[MAXN];
long long fact[MAXN];
const long long mod=998244353;
bool cmp(node a,node b){
    if(a.x==b.x) return a.y<b.y;
    return a.x<b.x;
}
long long jie(long long n){
    long long sum=1;
    for(long long i=1;i<=n;i++){
        sum=(1LL*((1LL*sum%mod)*(1LL*i%mod)))%mod;
    }
    return sum;
}
int main() {
    ios::sync_with_stdio(0);
    long long n, m;
      //freopen("D:\\code\\c,c++\\test\\date1.txt", "r", stdin);
        cin >> n;
        for (long long i = 1; i <= n; i++) {
            cin >> a[i].x >> a[i].y;
            b1[a[i].x]++;
            b2[a[i].y]++;
            if (a[i].x == a[i].y) b3[a[i].x]++;
        }
        long long sum = 1;
        fact[0] = 1;
        for (long long i = 1; i <= n; i++) {
            sum = (1LL * ((1LL * sum % mod) * (1LL * i % mod))) % mod;
            fact[i] = sum;
        }
        long long sum1 = 1, sum2 = 1, sum3 = 1;
        for (long long i = 0; i <= n; i++) {
            if (b1[i]) sum1 = 1LL * ((1LL * sum1 % mod) * (1LL * fact[b1[i]] % mod)) % mod;
            if (b2[i]) sum2 = 1LL * ((1LL * sum2 % mod) * (1LL * fact[b2[i]] % mod)) % mod;
        }
        sort(a + 1, a + n + 1, cmp);
        int cnt = 1;
        for (int i = 1; i < n; i++) {
            if (a[i].y > a[i + 1].y) {
                sum3 = 0;
                break;
            }
            if (a[i].x == a[i + 1].x&&a[i].y == a[i + 1].y) {

                    cnt++;
            } else {
                sum3 = 1LL * ((1LL * sum3 % mod) * (1LL * fact[cnt] % mod)) % mod;
                cnt = 1;
            }
        }
        sum3 = 1LL * ((1LL * sum3 % mod) * (1LL * fact[cnt] % mod)) % mod;
        cout << ((1LL * sum % mod) - (1LL * sum1 % mod) - (1LL * sum2 % mod) + sum3 % mod + mod + mod) % mod << endl;
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 1000101

using namespace std;

int n,m;
struct  node{
    int x,y;
}a[MAXN];
int b1[MAXN];
int b2[MAXN];
const int mod=998244353;
bool cmp(node a,node b){
    return a.x<b.x;
}
int main() {
    ios::sync_with_stdio(0);
    int n,m;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].x>>a[i].y;

        if(!b1[a[i].x])
        b1[a[i].x]++;
        else if(a[i].x!=a[i].y){
            b1[a[i].x]++;
        }
        if(!b2[a[i].y])
        b2[a[i].y]++;
        else if(a[i].x!=a[i].y) {
            b2[a[i].y]++;
        }
    }
    int sum=1;
    int flag=0;
    for(int i=1;i<n;i++){
       if(a[i].x!=a[i+1].x&&a[i].y!=a[i+1].y)
           flag=1;
    }
    if(!flag){
        cout<<0<<endl;
        return 0;
    }
    for(int i=1;i<=n;i++){
        sum=(sum%mod)*(i%mod)%mod;
    }
    sort(a,a+n,cmp);
    int sum1=1,sum2=1;
    for(int i=1;i<=n;i++){
        if(b1[i])  sum1=(sum1%mod)*(b1[1]%mod)%mod;
        if(b2[i]) sum2=(sum2%mod)*(b2[i]%mod)%mod;
    }
     flag=1;
    for(int i=0;i<n-1;i++){
        if(a[i].y>a[i+1].y) {
            flag=0;
            break;
        }
    }
    if(flag){
        cout<<((sum%mod)-(sum1%mod)-(sum2%mod))%mod<<endl;
    }
    else{
        cout<<((sum%mod)-(sum1%mod)-(sum2%mod)-1)%mod<<endl;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 300010

using namespace std;

long long n,m;
struct  node{
    long long x,y;
}a[MAXN];
long long b1[MAXN];
long long b2[MAXN];
long long b3[MAXN];
long long fact[MAXN];
const long long mod=998244353;
bool cmp(node a,node b){
    if(a.x==b.x) return a.y<b.y;
    return a.x<b.x;
}
long long jie(long long n){
    long long sum=1;
    for(long long i=1;i<=n;i++){
        sum=(1LL*((1LL*sum%mod)*(1LL*i%mod)))%mod;
    }
    return sum;
}
int main() {
    ios::sync_with_stdio(0);
    long long n,m;
  //  freopen("D:\\code\\c,c++\\test\\date1.txt", "r", stdin);
    while(cin>>n) {
        memset(b1,0,sizeof(b1));
        memset(b2,0,sizeof(b2));
        memset(b3,0,sizeof(b3));
        for (long long i = 1; i <= n; i++) {
            cin >> a[i].x >> a[i].y;
            b1[a[i].x]++;
            b2[a[i].y]++;
            if (a[i].x == a[i].y) b3[a[i].x]++;
        }
        long long sum = 1;
        long long flag = 0;
        fact[0] = 1;
        for (long long i = 1; i <= n; i++) {
            sum = (1LL * ((1LL * sum % mod) * (1LL * i % mod))) % mod;
            fact[i] = sum;
        }
        long long sum1 = 1, sum2 = 1, sum3 = 1;
        for (long long i = 0; i <= n; i++) {
            if (b1[i]) sum1 = 1LL * ((1LL * sum1 % mod) * (1LL * fact[b1[i]] % mod)) % mod;
            if (b2[i]) sum2 = 1LL * ((1LL * sum2 % mod) * (1LL * fact[b2[i]] % mod)) % mod;
        }
        sort(a + 1, a + n + 1, cmp);
        int cnt = 1;
        for (int i = 1; i < n; i++) {
            if (a[i].y > a[i + 1].y) {
                sum3 = 0;
                break;
            }
            if (a[i].x == a[i + 1].x) {
                if (a[i].y == a[i + 1].y)
                    cnt++;
            } else {
                sum3 = 1LL * ((1LL * sum3 % mod) * (1LL * fact[cnt] % mod)) % mod;
                cnt = 1;
            }
        }
        sum3 = 1LL * ((1LL * sum3 % mod) * (1LL * fact[cnt] % mod)) % mod;
        cout << ((1LL * sum % mod) - (1LL * sum1 % mod) - (1LL * sum2 % mod) + sum3 % mod + mod + mod) % mod << endl;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 1000101

using namespace std;

long long n,m;
struct  node{
    long long x,y;
}a[MAXN];
long long b1[MAXN];
long long b2[MAXN];
const long long mod=998244353;
bool cmp(node a,node b){
    return a.x<b.x;
}
int main() {
    ios::sync_with_stdio(0);
    long long n,m;
    cin>>n;
    for(long long i=1;i<=n;i++){
        cin>>a[i].x>>a[i].y;

        if(!b1[a[i].x])
        b1[a[i].x]++;
        else if(a[i].x!=a[i].y){
            b1[a[i].x]++;
        }
        if(!b2[a[i].y])
        b2[a[i].y]++;
        else if(a[i].x!=a[i].y) {
            b2[a[i].y]++;
        }
    }
    long long sum=1;
    long long flag=0;
    for(long long i=1;i<n;i++){
       if(a[i].x!=a[i+1].x&&a[i].y!=a[i+1].y)
           flag=1;
    }
    if(!flag){
        cout<<0<<endl;
        return 0;
    }
    for(long long i=1;i<=n;i++){
        sum=((sum%mod)*(i%mod))%mod;
    }
    cout<<sum<<endl;
    sort(a,a+n,cmp);
    long long sum1=1,sum2=1;
    for(long long i=1;i<=n;i++){
        if(b1[i])  sum1=((sum1%mod)*(b1[1]%mod))%mod;
        if(b2[i]) sum2=((sum2%mod)*(b2[i]%mod))%mod;
    }
     flag=1;
    for(long long i=0;i<n-1;i++){
        if(a[i].y>a[i+1].y) {
            flag=0;
            break;
        }
    }
    if(flag){
        cout<<((sum%mod)-(sum1%mod)-(sum2%mod))%mod<<endl;
    }
    else{
        cout<<((sum%mod)-(sum1%mod)-(sum2%mod)-1)%mod<<endl;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 100010

using namespace std;

long long n,m;
struct  node{
    long long x,y;
}a[MAXN];
long long b1[MAXN];
long long b2[MAXN];
const long long mod=998244353;
bool cmp(node a,node b){
    return a.x<b.x;
}
int main() {
    ios::sync_with_stdio(0);
    long long n,m;
    cin>>n;
    for(long long i=1;i<=n;i++){
        cin>>a[i].x>>a[i].y;

        if(!b1[a[i].x])
        b1[a[i].x]++;
        else if(a[i].x!=a[i].y){
            b1[a[i].x]++;
        }
        if(!b2[a[i].y])
        b2[a[i].y]++;
        else if(a[i].x!=a[i].y) {
            b2[a[i].y]++;
        }
    }
    long long sum=1;
    long long flag=0;
    for(long long i=1;i<n;i++){
       if(a[i].x!=a[i+1].x&&a[i].y!=a[i+1].y)
           flag=1;
    }
    if(!flag){
        cout<<0<<endl;
        return 0;
    }
    for(long long i=1;i<=n;i++){
        sum=(1LL*((1LL*sum%mod)*(1LL*i%mod)))%mod;
    }
    //cout<<sum<<endl;
    sort(a,a+n,cmp);
    long long sum1=1,sum2=1;
    for(long long i=1;i<=n;i++){
        if(b1[i])  sum1=1LL*((1LL*sum1%mod)*(1LL*b1[1]%mod))%mod;
        if(b2[i]) sum2=1LL*((1LL*sum2%mod)*(1LL*b2[i]%mod))%mod;
    }
    flag=1;
    for(long long i=0;i<n-1;i++){
        if(a[i].y>a[i+1].y) {
            flag=0;
            break;
        }
    }
    if(flag){
        cout<<((1LL*sum%mod)-(1LL*sum1%mod)-(1LL*sum2%mod))%mod<<endl;
    }
    else{
        cout<<((1LL*sum%mod)-(1LL*sum1%mod)-(1LL*sum2%mod)-1)%mod<<endl;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 1000101

using namespace std;

long long n,m;
struct  node{
    long long x,y;
}a[MAXN];
long long b1[MAXN];
long long b2[MAXN];
const long long mod=998244353;
bool cmp(node a,node b){
    return a.x<b.x;
}
int main() {
    ios::sync_with_stdio(0);
    long long n,m;
    cin>>n;
    for(long long i=1;i<=n;i++){
        cin>>a[i].x>>a[i].y;

        if(!b1[a[i].x])
        b1[a[i].x]++;
        else if(a[i].x!=a[i].y){
            b1[a[i].x]++;
        }
        if(!b2[a[i].y])
        b2[a[i].y]++;
        else if(a[i].x!=a[i].y) {
            b2[a[i].y]++;
        }
    }
    long long sum=1;
    long long flag=0;
    for(long long i=1;i<n;i++){
       if(a[i].x!=a[i+1].x&&a[i].y!=a[i+1].y)
           flag=1;
    }
    if(!flag){
        cout<<0<<endl;
        return 0;
    }
    for(long long i=1;i<=n;i++){
        sum=((sum%mod)*(i%mod))%mod;
    }
    //cout<<sum<<endl;
    sort(a,a+n,cmp);
    long long sum1=1,sum2=1;
    for(long long i=1;i<=n;i++){
        if(b1[i])  sum1=((sum1%mod)*(b1[1]%mod))%mod;
        if(b2[i]) sum2=((sum2%mod)*(b2[i]%mod))%mod;
    }
     flag=1;
    for(long long i=0;i<n-1;i++){
        if(a[i].y>a[i+1].y) {
            flag=0;
            break;
        }
    }
    if(flag){
        cout<<((sum%mod)-(sum1%mod)-(sum2%mod))%mod<<endl;
    }
    else{
        cout<<((sum%mod)-(sum1%mod)-(sum2%mod)-1)%mod<<endl;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 300010

using namespace std;

long long n,m;
struct  node{
    long long x,y;
}a[MAXN];
long long b1[MAXN];
long long b2[MAXN];
long long b3[MAXN];
long long fact[MAXN];
const long long mod=998244353;
bool cmp(node a,node b){
    if(a.x==b.x) return a.y<b.y;
    return a.x<b.x;
}
long long jie(long long n){
    long long sum=1;
    for(long long i=1;i<=n;i++){
        sum=(1LL*((1LL*sum%mod)*(1LL*i%mod)))%mod;
    }
    return sum;
}
int main() {
    ios::sync_with_stdio(0);
    long long n, m;
      //freopen("D:\\code\\c,c++\\test\\date1.txt", "r", stdin);
    while (cin >> n) {
        memset(b1, 0, sizeof(b1));
        memset(b2, 0, sizeof(b2));
        memset(b3, 0, sizeof(b3));
        for (long long i = 1; i <= n; i++) {
            cin >> a[i].x >> a[i].y;
            b1[a[i].x]++;
            b2[a[i].y]++;
            if (a[i].x == a[i].y) b3[a[i].x]++;
        }
        long long sum = 1;
        fact[0] = 1;
        for (long long i = 1; i <= n; i++) {
            sum = (1LL * ((1LL * sum % mod) * (1LL * i % mod))) % mod;
            fact[i] = sum;
        }
        long long sum1 = 1, sum2 = 1, sum3 = 1;
        for (long long i = 0; i <= n; i++) {
            if (b1[i]) sum1 = 1LL * ((1LL * sum1 % mod) * (1LL * fact[b1[i]] % mod)) % mod;
            if (b2[i]) sum2 = 1LL * ((1LL * sum2 % mod) * (1LL * fact[b2[i]] % mod)) % mod;
        }
        sort(a + 1, a + n + 1, cmp);
        int cnt = 1;
        for (int i = 1; i < n; i++) {
            if (a[i].y > a[i + 1].y) {
                sum3 = 0;
                break;
            }
            if (a[i].x == a[i + 1].x&&(a[i].y == a[i + 1].y)) {
                    cnt++;
            } else {
                sum3 = 1LL * ((1LL * sum3 % mod) * (1LL * fact[cnt] % mod)) % mod;
                cnt = 1;
            }
        }
        sum3 = 1LL * ((1LL * sum3 % mod) * (1LL * fact[cnt] % mod)) % mod;
        if(n==300000){
            cout<<sum<<" "<<sum1<<" "<<sum2<<" "<<sum3<<" "<<endl;
        }
        cout << ((1LL * sum % mod) - (1LL * sum1 % mod) - (1LL * sum2 % mod) + sum3 % mod + mod + mod) % mod << endl;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 1000101

using namespace std;

long long n,m;
struct  node{
    long long x,y;
}a[MAXN];
long long b1[MAXN];
long long b2[MAXN];
const long long mod=998244353;
bool cmp(node a,node b){
    return a.x<b.x;
}
int main() {
    ios::sync_with_stdio(0);
    long long n,m;
    cin>>n;
    for(long long i=1;i<=n;i++){
        cin>>a[i].x>>a[i].y;

        if(!b1[a[i].x])
        b1[a[i].x]++;
        else if(a[i].x!=a[i].y){
            b1[a[i].x]++;
        }
        if(!b2[a[i].y])
        b2[a[i].y]++;
        else if(a[i].x!=a[i].y) {
            b2[a[i].y]++;
        }
    }
    long long sum=1;
    long long flag=0;
    for(long long i=1;i<n;i++){
       if(a[i].x!=a[i+1].x&&a[i].y!=a[i+1].y)
           flag=1;
    }
    if(!flag){
        cout<<0<<endl;
        return 0;
    }
    for(long long i=1;i<=n;i++){
        sum=(1LL*((1LL*sum%mod)*(1LL*i%mod)))%mod;
    }
    //cout<<sum<<endl;
    sort(a,a+n,cmp);
    long long sum1=1,sum2=1;
    for(long long i=1;i<=n;i++){
        if(b1[i])  sum1=1LL*((1LL*sum1%mod)*(1LL*b1[1]%mod))%mod;
        if(b2[i]) sum2=1LL*((1LL*sum2%mod)*(1LL*b2[i]%mod))%mod;
    }
    flag=1;
    for(long long i=0;i<n-1;i++){
        if(a[i].y>a[i+1].y) {
            flag=0;
            break;
        }
    }
    if(flag){
        cout<<((1LL*sum%mod)-(1LL*sum1%mod)-(1LL*sum2%mod))%mod<<endl;
    }
    else{
        cout<<((1LL*sum%mod)-(1LL*sum1%mod)-(1LL*sum2%mod)-1)%mod<<endl;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 300010

using namespace std;

long long n,m;
struct  node{
    long long x,y;
}a[MAXN];
long long b1[MAXN];
long long b2[MAXN];
long long b3[MAXN];
long long fact[MAXN];
const long long mod=998244353;
bool cmp(node a,node b){
    if(a.x==b.x) return a.y<b.y;
    return a.x<b.x;
}
long long jie(long long n){
    long long sum=1;
    for(long long i=1;i<=n;i++){
        sum=(1LL*((1LL*sum%mod)*(1LL*i%mod)))%mod;
    }
    return sum;
}
int main() {
    ios::sync_with_stdio(0);
    long long n, m;
      //freopen("D:\\code\\c,c++\\test\\date1.txt", "r", stdin);
    cin >> n;
        for (long long i = 1; i <= n; i++) {
            cin >> a[i].x >> a[i].y;
            b1[a[i].x]++;
            b2[a[i].y]++;
            if (a[i].x == a[i].y) b3[a[i].x]++;
        }
        long long sum = 1;
        fact[0] = 1;
        for (long long i = 1; i <= n; i++) {
            sum = (1LL * ((1LL * sum % mod) * (1LL * i % mod))) % mod;
            fact[i] = sum;
        }
        long long sum1 = 1, sum2 = 1, sum3 = 1;
        for (long long i = 0; i <= n; i++) {
            if (b1[i]) sum1 = 1LL * ((1LL * sum1 % mod) * (1LL * fact[b1[i]] % mod)) % mod;
            if (b2[i]) sum2 = 1LL * ((1LL * sum2 % mod) * (1LL * fact[b2[i]] % mod)) % mod;
        }
        sort(a + 1, a + n + 1, cmp);
        int cnt = 1;
        for (int i = 1; i < n; i++) {
            if (a[i].y > a[i + 1].y) {
                sum3 = 0;
                break;
            }
            if (a[i].x == a[i + 1].x) {
                if (a[i].y == a[i + 1].y)
                    cnt++;
            } else {
                sum3 = 1LL * ((1LL * sum3 % mod) * (1LL * fact[cnt] % mod)) % mod;
                cnt = 1;
            }
        }
        sum3 = 1LL * ((1LL * sum3 % mod) * (1LL * fact[cnt] % mod)) % mod;
        cout << ((1LL * sum % mod) - (1LL * sum1 % mod) - (1LL * sum2 % mod) + sum3 % mod + mod + mod) % mod << endl;
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 300010

using namespace std;

long long n,m;
struct  node{
    long long x,y;
}a[MAXN];
long long b1[MAXN];
long long b2[MAXN];
long long b3[MAXN];
long long fact[MAXN];
const long long mod=998244353;
bool cmp(node a,node b){
    return a.x<b.x;
}
long long jie(long long n){
    long long sum=1;
    for(long long i=1;i<=n;i++){
        sum=(1LL*((1LL*sum%mod)*(1LL*i%mod)))%mod;
    }
    return sum;
}
int main() {
    ios::sync_with_stdio(0);
    long long n,m;
   // freopen("D:\\code\\c,c++\\test\\date1.txt", "r", stdin);
    cin>>n;
    for(long long i=1;i<=n;i++){
        cin>>a[i].x>>a[i].y;
        b1[a[i].x]++;
        b2[a[i].y]++;
        if(a[i].x==a[i].y) b3[a[i].x]++;
    }
    long long sum=1;
    long long flag=0;
    for(long long i=1;i<n;i++){
       if(a[i].x!=a[i+1].x&&a[i].y!=a[i+1].y)
           flag=1;
    }
    if(!flag){
        cout<<0<<endl;
        return 0;
    }
    fact[0]=1;
    for(long long i=1;i<=n;i++){
        sum=(1LL*((1LL*sum%mod)*(1LL*i%mod)))%mod;
        fact[i]=sum;
    }

    long long sum1=1,sum2=1,sum3=1;
    for(long long i=0;i<=n;i++){
        if(b1[i])  sum1=1LL*((1LL*sum1%mod)*(1LL*fact[b1[i]]%mod))%mod;
        if(b2[i]) sum2=1LL*((1LL*sum2%mod)*(1LL*fact[b2[i]]%mod))%mod;
    }
    for(long long i=0;i<=n;i++){
        if(b3[i])  sum3=1LL*((1LL*sum3%mod)*(1LL*fact[b3[i]]%mod))%mod;
    }
//    cout<<sum1<<endl;
//    cout<<sum2<<endl;   cout<<sum<<endl;
//    cout<<sum3<<endl;   //cout<<sum<<endl;
    if(sum3==1) sum3=0;
    cout<<((1LL*sum%mod)-(1LL*sum1%mod)-(1LL*sum2%mod)+sum3%mod+mod+mod)%mod<<endl;

    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 300010

using namespace std;

long long n,m;
struct  node{
    long long x,y;
}a[MAXN];
long long b1[MAXN];
long long b2[MAXN];
long long b3[MAXN];
long long fact[MAXN];
const long long mod=998244353;
bool cmp(node a,node b){
    return a.x<b.x;
}
long long jie(long long n){
    long long sum=1;
    for(long long i=1;i<=n;i++){
        sum=(1LL*((1LL*sum%mod)*(1LL*i%mod)))%mod;
    }
    return sum;
}
int main() {
    ios::sync_with_stdio(0);
    long long n,m;
    freopen("D:\\code\\c,c++\\test\\date1.txt", "r", stdin);
    cin>>n;
    for(long long i=1;i<=n;i++){
        cin>>a[i].x>>a[i].y;
        b1[a[i].x]++;
        b2[a[i].y]++;
        if(a[i].x==a[i].y) b3[a[i].x]++;
    }
    long long sum=1;
    long long flag=0;
    for(long long i=1;i<n;i++){
       if(a[i].x!=a[i+1].x&&a[i].y!=a[i+1].y)
           flag=1;
    }
    if(!flag){
        cout<<0<<endl;
        return 0;
    }
    fact[0]=1;
    for(long long i=1;i<=n;i++){
        sum=(1LL*((1LL*sum%mod)*(1LL*i%mod)))%mod;
        fact[i]=sum;
    }

    long long sum1=1,sum2=1,sum3=1;
    for(long long i=0;i<=n;i++){
        if(b1[i])  sum1=1LL*((1LL*sum1%mod)*(1LL*fact[b1[i]]%mod))%mod;
        if(b2[i]) sum2=1LL*((1LL*sum2%mod)*(1LL*fact[b2[i]]%mod))%mod;
    }
    for(long long i=0;i<=n;i++){
        if(b3[i])  sum3=1LL*((1LL*sum3%mod)*(1LL*fact[b3[i]]%mod))%mod;
    }
//    cout<<sum1<<endl;
//    cout<<sum2<<endl;   cout<<sum<<endl;
//    cout<<sum3<<endl;   //cout<<sum<<endl;
    if(sum3==1) sum3=0;
    cout<<((1LL*sum%mod)-(1LL*sum1%mod)-(1LL*sum2%mod)+sum3%mod+mod+mod)%mod<<endl;

    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 300010

using namespace std;

long long n,m;
struct  node{
    long long x,y;
}a[MAXN];
long long b1[MAXN];
long long b2[MAXN];
long long b3[MAXN];
long long fact[MAXN];
const long long mod=998244353;
bool cmp(node a,node b){
    if(a.x==b.x) return a.y<b.y;
    return a.x<b.x;
}
long long jie(long long n){
    long long sum=1;
    for(long long i=1;i<=n;i++){
        sum=(1LL*((1LL*sum%mod)*(1LL*i%mod)))%mod;
    }
    return sum;
}
int main() {
    ios::sync_with_stdio(0);
    long long n, m;
      //freopen("D:\\code\\c,c++\\test\\date1.txt", "r", stdin);
    while (cin >> n) {
        memset(b1, 0, sizeof(b1));
        memset(b2, 0, sizeof(b2));
        memset(b3, 0, sizeof(b3));
        for (long long i = 1; i <= n; i++) {
            cin >> a[i].x >> a[i].y;
            b1[a[i].x]++;
            b2[a[i].y]++;
            if (a[i].x == a[i].y) b3[a[i].x]++;
        }
        long long sum = 1;
        fact[0] = 1;
        for (long long i = 1; i <= n; i++) {
            sum = (1LL * ((1LL * sum % mod) * (1LL * i % mod))) % mod;
            fact[i] = sum;
        }
        long long sum1 = 1, sum2 = 1, sum3 = 1;
        for (long long i = 0; i <= n; i++) {
            if (b1[i]) sum1 = 1LL * ((1LL * sum1 % mod) * (1LL * fact[b1[i]] % mod)) % mod;
            if (b2[i]) sum2 = 1LL * ((1LL * sum2 % mod) * (1LL * fact[b2[i]] % mod)) % mod;
        }
        sort(a + 1, a + n + 1, cmp);
        int cnt = 1;
        for (int i = 1; i < n; i++) {
            if (a[i].y > a[i + 1].y) {
                sum3 = 0;
                break;
            }
            if (a[i].x == a[i + 1].x&&(a[i].y == a[i + 1].y)) {
                    cnt++;
            } else {
                sum3 = 1LL * ((1LL * sum3 % mod) * (1LL * fact[cnt] % mod)) % mod;
                cnt = 1;
            }
        }
        sum3 = 1LL * ((1LL * sum3 % mod) * (1LL * fact[cnt] % mod)) % mod;
        cout << ((1LL * sum % mod) - (1LL * sum1 % mod) - (1LL * sum2 % mod) + sum3 % mod + mod + mod) % mod << endl;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 300010

using namespace std;

long long n,m;
struct  node{
    long long x,y;
}a[MAXN];
long long b1[MAXN];
long long b2[MAXN];
long long b3[MAXN];
long long fact[MAXN];
const long long mod=998244353;
bool cmp(node a,node b){
    if(a.x==b.x) return a.y<b.y;
    return a.x<b.x;
}
long long jie(long long n){
    long long sum=1;
    for(long long i=1;i<=n;i++){
        sum=(1LL*((1LL*sum%mod)*(1LL*i%mod)))%mod;
    }
    return sum;
}
int main() {
    ios::sync_with_stdio(0);
    long long n, m;
      //freopen("D:\\code\\c,c++\\test\\date1.txt", "r", stdin);
    while (cin >> n) {
        memset(b1, 0, sizeof(b1));
        memset(b2, 0, sizeof(b2));
        memset(b3, 0, sizeof(b3));
        for (long long i = 1; i <= n; i++) {
            cin >> a[i].x >> a[i].y;
            b1[a[i].x]++;
            b2[a[i].y]++;
            if (a[i].x == a[i].y) b3[a[i].x]++;
        }
        long long sum = 1;
        fact[0] = 1;
        for (long long i = 1; i <= n; i++) {
            sum = (1LL * ((1LL * sum % mod) * (1LL * i % mod))) % mod;
            fact[i] = sum;
        }
        long long sum1 = 1, sum2 = 1, sum3 = 1;
        for (long long i = 0; i <= n; i++) {
            if (b1[i]) sum1 = 1LL * ((1LL * sum1 % mod) * (1LL * fact[b1[i]] % mod)) % mod;
            if (b2[i]) sum2 = 1LL * ((1LL * sum2 % mod) * (1LL * fact[b2[i]] % mod)) % mod;
        }
        sort(a + 1, a + n + 1, cmp);
        int cnt = 1;
        for (int i = 1; i < n; i++) {
            if (a[i].y > a[i + 1].y) {
                sum3 = 0;
                break;
            }
            if (a[i].x == a[i + 1].x) {
                if (a[i].y == a[i + 1].y)
                    cnt++;
            } else {
                sum3 = 1LL * ((1LL * sum3 % mod) * (1LL * fact[cnt] % mod)) % mod;
                cnt = 1;
            }
        }
        sum3 = 1LL * ((1LL * sum3 % mod) * (1LL * fact[cnt] % mod)) % mod;
        if(n==300000){
            cout<<sum<<" "<<sum1<<" "<<sum2<<" "<<sum3<<" "<<endl;
        }
        cout << ((1LL * sum % mod) - (1LL * sum1 % mod) - (1LL * sum2 % mod) + sum3 % mod + mod + mod) % mod << endl;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 1000101

using namespace std;

long long n,m;
struct  node{
    long long x,y;
}a[MAXN];
long long b1[MAXN];
long long b2[MAXN];
const long long mod=998244353;
bool cmp(node a,node b){
    return a.x<b.x;
}
int main() {
    ios::sync_with_stdio(0);
    long long n,m;
    cin>>n;
    for(long long i=1;i<=n;i++){
        cin>>a[i].x>>a[i].y;

        if(!b1[a[i].x])
        b1[a[i].x]++;
        else if(a[i].x!=a[i].y){
            b1[a[i].x]++;
        }
        if(!b2[a[i].y])
        b2[a[i].y]++;
        else if(a[i].x!=a[i].y) {
            b2[a[i].y]++;
        }
    }
    long long sum=1;
    long long flag=0;
    for(long long i=1;i<n;i++){
       if(a[i].x!=a[i+1].x&&a[i].y!=a[i+1].y)
           flag=1;
    }
    if(!flag){
        cout<<0<<endl;
        return 0;
    }
    for(long long i=1;i<=n;i++){
        sum=(sum%mod)*(i%mod)%mod;
    }
    sort(a,a+n,cmp);
    long long sum1=1,sum2=1;
    for(long long i=1;i<=n;i++){
        if(b1[i])  sum1=(sum1%mod)*(b1[1]%mod)%mod;
        if(b2[i]) sum2=(sum2%mod)*(b2[i]%mod)%mod;
    }
     flag=1;
    for(long long i=0;i<n-1;i++){
        if(a[i].y>a[i+1].y) {
            flag=0;
            break;
        }
    }
    if(flag){
        cout<<((sum%mod)-(sum1%mod)-(sum2%mod))%mod<<endl;
    }
    else{
        cout<<((sum%mod)-(sum1%mod)-(sum2%mod)-1)%mod<<endl;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 300010

using namespace std;

long long n,m;
struct  node{
    long long x,y;
}a[MAXN];
long long b1[MAXN];
long long b2[MAXN];
long long b3[MAXN];
long long fact[MAXN];
const long long mod=998244353;
bool cmp(node a,node b){
    if(a.x==b.x) return a.y<b.y;
    return a.x<b.x;
}
long long jie(long long n){
    long long sum=1;
    for(long long i=1;i<=n;i++){
        sum=(1LL*((1LL*sum%mod)*(1LL*i%mod)))%mod;
    }
    return sum;
}
int main() {
    ios::sync_with_stdio(0);
    long long n,m;
  //  freopen("D:\\code\\c,c++\\test\\date1.txt", "r", stdin);
    cin>>n;
    for(long long i=1;i<=n;i++){
        cin>>a[i].x>>a[i].y;
        b1[a[i].x]++;
        b2[a[i].y]++;
        if(a[i].x==a[i].y) b3[a[i].x]++;
    }
    long long sum=1;
    long long flag=0;
    for(long long i=1;i<n;i++){
       if(a[i].x!=a[i+1].x&&a[i].y!=a[i+1].y)
           flag=1;
    }
    if(!flag){
        cout<<0<<endl;
        return 0;
    }
    fact[0]=1;
    for(long long i=1;i<=n;i++){
        sum=(1LL*((1LL*sum%mod)*(1LL*i%mod)))%mod;
        fact[i]=sum;
    }

    long long sum1=1,sum2=1,sum3=1;
    for(long long i=0;i<=n;i++){
        if(b1[i])  sum1=1LL*((1LL*sum1%mod)*(1LL*fact[b1[i]]%mod))%mod;
        if(b2[i]) sum2=1LL*((1LL*sum2%mod)*(1LL*fact[b2[i]]%mod))%mod;
    }
    sort(a+1,a+n+1,cmp);
    int cnt=1;
    for(int i=1;i<n;i++){
        if(a[i].y>a[i+1].y){
            sum3=0;
            break;
        }
        if(a[i].x==a[i+1].x){
            if(a[i].y==a[i+1].y)
            cnt++;
        }
        else{
            sum3=1LL*((1LL*sum3%mod)*(1LL*fact[cnt]%mod))%mod;
            cnt=1;
        }
    }
    if(sum3==1) sum3=0;
    cout<<((1LL*sum%mod)-(1LL*sum1%mod)-(1LL*sum2%mod)+sum3%mod+mod+mod)%mod<<endl;

    return 0;
}