#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 long long 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(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(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);
    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;
}


#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];
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<bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define SQR (x) (x)*(x)

const ll base=998244353;
const double pi=3.1415926535897932;
const ll oo=9223372036854775807;
const long inf=2147483647;

pii a[300002];
ll b[300002],c[300002],ans,ans1,ans2,temp;
ll f[300002];
ll xr;
int n;
int main()
{
	ios_base::sync_with_stdio(false);
 	#ifndef ONLINE_JUDGE
 	freopen("CPD.inp","r",stdin);
// 	freopen("CPD.out","w",stdout);
	#endif
	cin >> n;
	for(int i=1;i<=n;i++) cin >> a[i].fi >> a[i].se,b[i]=a[i].fi,c[i]=a[i].se;
	f[0]=1;
	for(ll i=1;i<=n;i++) f[i] = (f[i-1]%base * (i%base))%base;
   sort(b+1,b+1+n);sort(c+1,c+1+n);
   ans=f[n];ans1=ans2=temp=1;
   xr = 1;
   for(int i=1;i<=n;i++)
      if (b[i]==b[i+1]) xr++; else {ans1 = ((ans1%base)*(f[xr]%base))%base;xr=1;}
   xr = 1;
   for(int i=1;i<=n;i++)
      if (c[i]==c[i+1]) xr++; else {ans2 = ((ans2%base)*(f[xr]%base))%base;xr=1;}
   ans = (ans%base - ans1%base - ans2%base + base)%base;
   sort(a+1,a+1+n);
   xr=1;
   for(int i=1;i<=n;i++)
      if (a[i].se==a[i+1].se) xr++; else {temp = ((temp%base) * (f[xr]%base))%base;xr=1;}
   for(int i=1;i<=n;i++)
      if (a[i].se>a[i+1].se) temp=0;
   cout << (ans%base+temp%base+base)%base;

	return 0;
}

//I think therefore I am

//PRO TIP: THE BEST WAY TO SOLVE THE PROBLEM IS TO HAVE THE RIGHT ANSWER!

// A chief event of life is the day in which we have encountered a mind that startled us.


#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define SQR (x) (x)*(x)

const ll base=998244353;
const double pi=3.1415926535897932;
const ll oo=9223372036854775807;
const long inf=2147483647;

pii a[300002];
ll b[300002],c[300002],ans,ans1,ans2,temp;
ll f[300002];
ll xr;
int n;
int main()
{
	ios_base::sync_with_stdio(false);
 	#ifndef ONLINE_JUDGE
 	freopen("CPD.inp","r",stdin);
// 	freopen("CPD.out","w",stdout);
	#endif
	cin >> n;
	for(int i=1;i<=n;i++) cin >> a[i].fi >> a[i].se,b[i]=a[i].fi,c[i]=a[i].se;
	f[0]=1;
	for(ll i=1;i<=n;i++) f[i] = (f[i-1]%base * (i%base))%base;
   sort(b+1,b+1+n);sort(c+1,c+1+n);
   ans=f[n];ans1=ans2=temp=1;
   xr = 1;
   for(int i=1;i<=n;i++)
      if (b[i]==b[i+1]) xr++; else {ans1 = ((ans1%base)*(f[xr]%base))%base;xr=1;}
   xr = 1;
   for(int i=1;i<=n;i++)
      if (c[i]==c[i+1]) xr++; else {ans2 = ((ans2%base)*(f[xr]%base))%base;xr=1;}
   ans = (ans%base - ans1%base - ans2%base + base)%base;
   sort(a+1,a+1+n);
   xr=1;
   for(int i=1;i<=n;i++)
      if (a[i].se==a[i+1].se) xr++; else {temp = ((temp%base) * (f[xr]%base))%base;xr=1;}
   for(int i=1;i<=n-1;i++)
      if (a[i].se>a[i+1].se) temp=0;
   cout << (ans%base+temp%base+base)%base;

	return 0;
}

//I think therefore I am

//PRO TIP: THE BEST WAY TO SOLVE THE PROBLEM IS TO HAVE THE RIGHT ANSWER!

// A chief event of life is the day in which we have encountered a mind that startled us.


#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define SQR (x) (x)*(x)

const ll base=998244353;
const double pi=3.1415926535897932;
const ll oo=9223372036854775807;
const long inf=2147483647;

pii a[300002];
ll b[300002],c[300002],ans,ans1,ans2,temp;
ll f[300002];
ll xr;
int n;
int main()
{
	ios_base::sync_with_stdio(false);
 	#ifndef ONLINE_JUDGE
 	freopen("CPD.inp","r",stdin);
 //	freopen("CPD.out","w",stdout);
	#endif
	cin >> n;
	for(int i=1;i<=n;i++) cin >> a[i].fi >> a[i].se,b[i]=a[i].fi,c[i]=a[i].se;
	f[0]=1;
	for(ll i=1;i<=n;i++) f[i] = (1ll*f[i-1] * i)%base;
   sort(b+1,b+1+n);sort(c+1,c+1+n);
   ans=f[n];ans1=ans2=temp=1;xr = 1;
   for(int i=1;i<=n;i++) if (b[i]==b[i+1]) xr++; else {ans1 = ((ans1%base)*(f[xr]%base))%base;xr=1;}
   ans = (ans-ans1+base)%base;xr = 1;ans1=1;
   for(int i=1;i<=n;i++) if (c[i]==c[i+1]) xr++; else {ans1 = ((ans1%base)*(f[xr]%base))%base;xr=1;}
   ans = (ans-ans1+base)%base;
   sort(a+1,a+1+n);
   xr=1;
   for(int i=1;i<=n;i++) if (a[i].se==a[i+1].se && a[i].fi==a[i+1].fi) xr++; else {temp = ((temp%base) * (f[xr]%base))%base;xr=1;}
   for(int i=1;i<=n-1;i++)
      if (a[i].se>a[i+1].se) temp=0;
   cout << (ans+temp)%base;

	return 0;
}

//I think therefore I am

//PRO TIP: THE BEST WAY TO SOLVE THE PROBLEM IS TO HAVE THE RIGHT ANSWER!

// A chief event of life is the day in which we have encountered a mind that startled us.


#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define SQR (x) (x)*(x)

const ll base=998244353;
const double pi=3.1415926535897932;
const ll oo=9223372036854775807;
const long inf=2147483647;

pii a[300001];
ll b[300001],c[300001],ans,ans1,ans2,temp;
ll f[300001];
ll xr;
int n;
int main()
{
	ios_base::sync_with_stdio(false);
 	#ifndef ONLINE_JUDGE
 	freopen("CPD.inp","r",stdin);
// 	freopen("CPD.out","w",stdout);
	#endif
	cin >> n;
	for(int i=1;i<=n;i++) cin >> a[i].fi >> a[i].se,b[i]=a[i].fi,c[i]=a[i].se;
	f[0]=1;
	for(ll i=1;i<=n;i++)
      f[i] = (f[i-1]%base * (i%base))%base;
   sort(b+1,b+1+n);
   sort(c+1,c+1+n);
   ans=f[n];
   ans1=ans2=temp=1;
   xr = 1;
   for(int i=1;i<=n-1;i++)
      if (b[i]==b[i+1])
         {
            xr++;
            ans1 = ((ans1%base) *(xr%base)) % base;
         } else xr=1;
   xr = 1;
   for(int i=1;i<=n-1;i++)
      if (c[i]==c[i+1])
         {
            xr++;
            ans2 = ((ans2%base)*(xr%base))%base;
         } else xr=1;
   ans = (ans%base - ans1%base - ans2%base + base)%base;
   sort(a+1,a+1+n);
   xr=1;
   for(int i=1;i<=n-1;i++)
      if (a[i].se==a[i+1].se)
         {
            xr++;
            temp = ((temp%base) * (xr%base))%base;
         } else xr=1;
   for(int i=1;i<=n-1;i++)
      if (a[i].se>a[i+1].se) temp=0;

   cout << (ans%base+temp%base+base)%base;

	return 0;
}

//I think therefore I am

//PRO TIP: THE BEST WAY TO SOLVE THE PROBLEM IS TO HAVE THE RIGHT ANSWER!

// A chief event of life is the day in which we have encountered a mind that startled us.


#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define SQR (x) (x)*(x)

const ll base=998244353;
const double pi=3.1415926535897932;
const ll oo=9223372036854775807;
const long inf=2147483647;

pii a[300002];
ll b[300002],c[300002],ans,ans1,ans2,temp;
ll f[300002];
ll xr;
int n;
int main()
{
	ios_base::sync_with_stdio(false);
 	#ifndef ONLINE_JUDGE
 	freopen("CPD.inp","r",stdin);
 	freopen("CPD.out","w",stdout);
	#endif
	cin >> n;
	for(int i=1;i<=n;i++) cin >> a[i].fi >> a[i].se,b[i]=a[i].fi,c[i]=a[i].se;
	f[0]=1;
	for(ll i=1;i<=n;i++) f[i] = ((f[i-1]%base) * (i%base))%base;
   sort(b+1,b+1+n);sort(c+1,c+1+n);
   ans=f[n];ans1=ans2=temp=1;
   xr = 1;
   for(int i=1;i<=n;i++)
      if (b[i]==b[i+1]) xr++; else {ans1 = ((ans1%base)*(f[xr]%base))%base;xr=1;}
   ans = (ans%base-ans1%base+base)%base;
   xr = 1;ans1=1;
   for(int i=1;i<=n;i++)
      if (c[i]==c[i+1]) xr++; else {ans1 = ((ans1%base)*(f[xr]%base))%base;xr=1;}
   ans = (ans%base-ans1%base+base)%base;
   sort(a+1,a+1+n);
   xr=1;
   for(int i=1;i<=n;i++)
      if (a[i].se==a[i+1].se) xr++; else {temp = ((temp%base) * (f[xr]%base))%base;xr=1;}
   for(int i=1;i<=n-1;i++)
      if (a[i].se>a[i+1].se) temp=0;
   cout << (ans%base+temp%base+base)%base;

	return 0;
}

//I think therefore I am

//PRO TIP: THE BEST WAY TO SOLVE THE PROBLEM IS TO HAVE THE RIGHT ANSWER!

// A chief event of life is the day in which we have encountered a mind that startled us.


#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define SQR (x) (x)*(x)

const ll base=998244353;
const double pi=3.1415926535897932;
const ll oo=9223372036854775807;
const long inf=2147483647;

pii a[300001];
ll b[300001],c[300001],ans,ans1,ans2,temp;
ll f[300001];
ll xr;
int n;
int main()
{
	ios_base::sync_with_stdio(false);
 	#ifndef ONLINE_JUDGE
 	freopen("CPD.inp","r",stdin);
// 	freopen("CPD.out","w",stdout);
	#endif
	cin >> n;
	for(int i=1;i<=n;i++) cin >> a[i].fi >> a[i].se,b[i]=a[i].fi,c[i]=a[i].se;
	f[0]=1;
	for(ll i=1;i<=n;i++)
      f[i] = (f[i-1]%base * (i%base))%base;
   sort(b+1,b+1+n);sort(c+1,c+1+n);
   ans=f[n];ans1=ans2=temp=1;
   xr = 1;
   for(int i=1;i<=n-1;i++)
      if (b[i]==b[i+1]) xr++; else {ans1 = ((ans1%base)*(f[xr]%base))%base;xr=1;}
   xr = 1;
   for(int i=1;i<=n-1;i++)
      if (c[i]==c[i+1]) xr++; else {ans2 = ((ans2%base)*(f[xr]%base))%base;xr=1;}
   ans = (ans%base - ans1%base - ans2%base + base)%base;
   sort(a+1,a+1+n);
   xr=1;
   for(int i=1;i<=n-1;i++)
      if (a[i].se==a[i+1].se) xr++; else {temp = ((temp%base) * (f[xr]%base))%base;xr=1;}
   for(int i=1;i<=n-1;i++)
      if (a[i].se>a[i+1].se) temp=0;
   cout << (ans%base+temp%base+base)%base;

	return 0;
}

//I think therefore I am

//PRO TIP: THE BEST WAY TO SOLVE THE PROBLEM IS TO HAVE THE RIGHT ANSWER!

// A chief event of life is the day in which we have encountered a mind that startled us.


#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define SQR (x) (x)*(x)

const ll base=998244353;
const double pi=3.1415926535897932;
const ll oo=9223372036854775807;
const long inf=2147483647;

pii a[300001];
ll b[300001],c[300001],ans,ans1,ans2,temp;
ll f[300001];
ll xr;
int n;
int main()
{
	ios_base::sync_with_stdio(false);
 	#ifndef ONLINE_JUDGE
 	freopen("CPD.inp","r",stdin);
// 	freopen("CPD.out","w",stdout);
	#endif
	cin >> n;
	for(int i=1;i<=n;i++) cin >> a[i].fi >> a[i].se,b[i]=a[i].fi,c[i]=a[i].se;
	f[0]=1;
	for(ll i=1;i<=n;i++)
      f[i] = (f[i-1]%base * (i%base))%base;
   sort(b+1,b+1+n);
   sort(c+1,c+1+n);
   ans=f[n];
   ans1=ans2=temp=1;
   xr = 1;
   for(int i=1;i<=n;i++)
      if (b[i]==b[i+1])
         {
            xr++;
            ans1 = ((ans1%base) *(xr%base)) % base;
         } else xr=1;
   xr = 1;
   for(int i=1;i<=n;i++)
      if (c[i]==c[i+1])
         {
            xr++;
            ans2 = ((ans2%base)*(xr%base))%base;
         } else xr=1;
   ans = (ans%base - ans1%base - ans2%base + base)%base;
   sort(a+1,a+1+n);
   xr=1;
   for(int i=1;i<=n;i++)
      if (a[i].se==a[i+1].se)
         {
            xr++;
            temp = ((temp%base) * (xr%base))%base;
         } else xr=1;
   for(int i=1;i<=n-1;i++)
      if (a[i].se>a[i+1].se) temp=0;

   cout << (ans%base+temp%base+base)%base;

	return 0;
}

//I think therefore I am

//PRO TIP: THE BEST WAY TO SOLVE THE PROBLEM IS TO HAVE THE RIGHT ANSWER!

// A chief event of life is the day in which we have encountered a mind that startled us.


#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define SQR (x) (x)*(x)

const ll base=998244353;
const double pi=3.1415926535897932;
const ll oo=9223372036854775807;
const long inf=2147483647;

pii a[300002];
ll b[300002],c[300002],ans,ans1,ans2,temp;
ll f[300002];
ll xr;
int n;
int main()
{
	ios_base::sync_with_stdio(false);
 	#ifndef ONLINE_JUDGE
 	freopen("CPD.inp","r",stdin);
// 	freopen("CPD.out","w",stdout);
	#endif
	cin >> n;
	for(int i=1;i<=n;i++) cin >> a[i].fi >> a[i].se,b[i]=a[i].fi,c[i]=a[i].se;
	f[0]=1;
	for(ll i=1;i<=n;i++) f[i] = (f[i-1]%base * (i%base))%base;
   sort(b+1,b+1+n);sort(c+1,c+1+n);
   ans=f[n];ans1=ans2=temp=1;
   xr = 1;
   for(int i=1;i<=n;i++)
      if (b[i]==b[i+1]) xr++; else {ans1 = ((ans1%base)*(f[xr]%base))%base;xr=1;}
   xr = 1;
   for(int i=1;i<=n;i++)
      if (c[i]==c[i+1]) xr++; else {ans2 = ((ans2%base)*(f[xr]%base))%base;xr=1;}
   ans = (ans%base - ans1%base - ans2%base + base)%base;
   sort(a+1,a+1+n);
   xr=1;
   for(int i=1;i<=n-1;i++)
      if (a[i].se==a[i+1].se) xr++; else {temp = ((temp%base) * (f[xr]%base))%base;xr=1;}
   for(int i=1;i<=n-1;i++)
      if (a[i].se>a[i+1].se) temp=0;
   cout << (ans%base+temp%base+base)%base;

	return 0;
}

//I think therefore I am

//PRO TIP: THE BEST WAY TO SOLVE THE PROBLEM IS TO HAVE THE RIGHT ANSWER!

// A chief event of life is the day in which we have encountered a mind that startled us.


#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
#define mkp(a, b) make_pair(a, b)
#define F first
#define S second
#define pii pair<int, int>
#define mem0(a) memset(a, 0, sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define MID(l, r) (l + ((r - l) >> 1))
#define ll(o) (o << 1)
#define rr(o) (o << 1 | 1)
#define ep(a) emplace_back(a)
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 301000;
const double pi = acos(-1.0);
int T, n, m;
struct Node
{
	int x, y;
} p[maxn];
const LL mod = 998244353;
LL jie[maxn];
bool cmp1(Node &a, Node &b)
{
	if (a.x != b.x)
		return a.x < b.x;
	return a.y < b.y;
}
bool cmp2(Node &a, Node &b)
{
	if (a.y != b.y)
		return a.y < b.y;
	return a.x < b.x;
}
int main()
{
	while (cin >> n)
	{
		jie[0] = 1;
		for (int i = 1; i <= n; i++)
		{
			jie[i] = (jie[i - 1] * i * 1ll) % mod;
			scanf("%d%d", &p[i].x, &p[i].y);
		}
		LL ans = jie[n];
		sort(p + 1, p + 1 + n, cmp1);
		LL sub = 1;
		for (int i = 1; i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].x == p[i + len].x)
				len++;
			i += len;
			sub = (sub * jie[len]) % mod;
		}
		ans = (ans - sub + mod) % mod;
		bool f = 0;
		for (int i = 2; i <= n; i++)
		{
			if (p[i - 1].y <= p[i].y)
			{
				if (i == n)
					ans++;
			}
			else
				break;
		}
		LL sub2 = 1;
		sort(p + 1, p + 1 + n, cmp2);
		for (int i = 1; i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].y == p[i + len].y)
				len++;
			i += len;
			//cout<<len<<" -> "<<jie[len]<<endl;
			sub2 = (sub2 * jie[len]) % mod;
		}
		LL add = 0;
		bool flag = 1;
		for (int i = 2; i <= n; i++)
		{
			if (p[i - 1].x > p[i].x || p[i - 1].y > p[i].y)
			{
				flag = 0;
				break;
			}
		}
		for (int i = 1; flag && i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].x == p[i + len].x && p[i].y == p[i + len].y)
				len++;
			i += len;
			if (len > 1)
			{
				if (add == 0)
					add = 1;
				add = (add * jie[len]) % mod;
			}
		}
		ans = (ans + add - sub2 + mod) % mod;
		//cout <<add<<" "<<sub<<" "<<sub2<<" "<<jie[n]<<" "<< ans << endl;
		cout << ans << endl;
	}
//	system("pause");
	return 0;
}
/*
10
1 2
1 1
1 2
2 1
1 2
2 2
2 1
1 2
1 2
2 2
10
1 2
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
#define mkp(a, b) make_pair(a, b)
#define F first
#define S second
#define pii pair<int, int>
#define mem0(a) memset(a, 0, sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define MID(l, r) (l + ((r - l) >> 1))
#define ll(o) (o << 1)
#define rr(o) (o << 1 | 1)
#define ep(a) emplace_back(a)
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 301000;
const double pi = acos(-1.0);
int T, n, m;
struct Node
{
	int x, y;
} p[maxn];
const LL mod = 998244353;
LL jie[maxn];
bool cmp1(Node &a, Node &b)
{
	if (a.x != b.x)
		return a.x < b.x;
	return a.y < b.y;
}
bool cmp2(Node &a, Node &b)
{
	if (a.y != b.y)
		return a.y < b.y;
	return a.x < b.x;
}
int main()
{
	while (cin >> n)
	{
		jie[0] = 1;
		for (int i = 1; i <= n; i++)
		{
			jie[i] = (jie[i - 1] * i * 1ll) % mod;
			scanf("%d%d", &p[i].x, &p[i].y);
		}
		LL ans = jie[n];
		sort(p + 1, p + 1 + n, cmp1);
		LL sub = 1;
		for (int i = 1; i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].x == p[i + len].x)
				len++;
			i += len;
			sub = (sub * jie[len]) % mod;
		}
		ans = (ans - sub + mod) % mod;
		bool f = 0;
		for (int i = 2; i <= n; i++)
		{
			if (p[i - 1].y < p[i].y)
			{
				if (i == n)
					ans++;
			}
			else
				break;
		}
		LL sub2 = 1;
		sort(p + 1, p + 1 + n, cmp2);
		for (int i = 1; i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].y == p[i + len].y)
				len++;
			i += len;
			//cout<<len<<" -> "<<jie[len]<<endl;
			sub2 = (sub2 * jie[len]) % mod;
		}
		LL add = 0;
		bool flag = 1;
		for (int i = 2; i <= n; i++)
		{
			if (p[i - 1].x > p[i].x || p[i - 1].y > p[i].y)
			{
				flag = 0;
				break;
			}
		}
		for (int i = 1; flag && i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].x == p[i + len].x && p[i].y == p[i + len].y)
				len++;
			i += len;
			if (len > 1)
			{
				if (add == 0)
					add = 1;
				add = (add * jie[len]) % mod;
			}
		}
		ans = (ans + add - sub2 + mod) % mod;
		//cout <<add<<" "<<sub<<" "<<sub2<<" "<<jie[n]<<" "<< ans << endl;
		cout << ans << endl;
	}
//	system("pause");
	return 0;
}
/*
10
1 2
1 1
1 2
2 1
1 2
2 2
2 1
1 2
1 2
2 2
10
1 2
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
#define mkp(a, b) make_pair(a, b)
#define F first
#define S second
#define pii pair<int, int>
#define mem0(a) memset(a, 0, sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define MID(l, r) (l + ((r - l) >> 1))
#define ll(o) (o << 1)
#define rr(o) (o << 1 | 1)
#define ep(a) emplace_back(a)
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 301000;
const double pi = acos(-1.0);
int T, n, m;
struct Node
{
	int x, y;
} p[maxn];
const LL mod = 998244353;
LL jie[maxn];
bool cmp1(Node &a, Node &b)
{
	if (a.x != b.x)
		return a.x < b.x;
	return a.y < b.y;
}
bool cmp2(Node &a, Node &b)
{
	if (a.y != b.y)
		return a.y < b.y;
	return a.x < b.x;
}
int main()
{
	while (cin >> n)
	{
		jie[0] = 1;
		for (int i = 1; i <= n; i++)
		{
			jie[i] = (jie[i - 1] * i * 1ll) % mod;
			scanf("%d%d", &p[i].x, &p[i].y);
		}
		LL ans = jie[n];
		sort(p + 1, p + 1 + n, cmp1);
		LL sub = 1;
		for (int i = 1; i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].x == p[i + len].x)
				len++;
			i += len;
			sub = (sub * jie[len]) % mod;
		}
		ans = (ans - sub + mod) % mod;
		bool f = 0;
		bool jia=0;
		for (int i = 2; i <= n; i++)
		{
			if (p[i - 1].y < p[i].y)
			{
				if (i == n)
					jia=1;
					ans++;
			}
			else
				break;
		}
		LL sub2 = 1;
		sort(p + 1, p + 1 + n, cmp2);
		for (int i = 1; i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].y == p[i + len].y)
				len++;
			i += len;
			//cout<<len<<" -> "<<jie[len]<<endl;
			sub2 = (sub2 * jie[len]) % mod;
		}
		for (int i = 2; !jia&&i <= n; i++)
		{
			if (p[i - 1].x < p[i].x)
			{
				if (i == n)
					{jia=1;
					ans++;
					}
			}
			else
				break;
		}
		LL add = 0;
		bool flag = 1;
		for (int i = 2; i <= n; i++)
		{
			if (p[i - 1].x > p[i].x || p[i - 1].y > p[i].y)
			{
				flag = 0;
				break;
			}
		}
		for (int i = 1; flag && i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].x == p[i + len].x && p[i].y == p[i + len].y)
				len++;
			i += len;
			if (len > 1)
			{
				if (add == 0)
					add = 1;
				add = (add * jie[len]) % mod;
			}
		}
		ans = (ans + add - sub2 + mod) % mod;
		//cout <<add<<" "<<sub<<" "<<sub2<<" "<<jie[n]<<" "<< ans << endl;
		cout << ans << endl;
	}
	//system("pause");
	return 0;
}
/*
10
1 2
1 1
1 2
2 1
1 2
2 2
2 1
1 2
1 2
2 2
10
1 2
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
#define mkp(a, b) make_pair(a, b)
#define F first
#define S second
#define pii pair<int, int>
#define mem0(a) memset(a, 0, sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define MID(l, r) (l + ((r - l) >> 1))
#define ll(o) (o << 1)
#define rr(o) (o << 1 | 1)
#define ep(a) emplace_back(a)
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 301000;
const double pi = acos(-1.0);
int T, n, m;
struct Node
{
	int x, y;
} p[maxn];
const LL mod = 998244353;
LL jie[maxn];
bool cmp1(Node &a, Node &b)
{
	if (a.x != b.x)
		return a.x < b.x;
	return a.y < b.y;
}
bool cmp2(Node &a, Node &b)
{
	if (a.y != b.y)
		return a.y < b.y;
	return a.x < b.x;
}
int main()
{
	while (cin >> n)
	{
		jie[0] = 1;
		for (int i = 1; i <= n; i++)
		{
			jie[i] = (jie[i - 1] * i * 1ll) % mod;
			scanf("%d%d", &p[i].x, &p[i].y);
		}
		LL ans=jie[n];
		sort(p+1,p+1+n,cmp1);
		LL sub1=1;
		for(int i=1;i<=n;i++)
		{
			int len=1;
			while(i+len<=n&&p[i].x==p[i+len].x)len++;
			sub1=(sub1*jie[len])%mod;
			i+=len-1;
		}
		sort(p+1,p+1+n,cmp2);

		LL sub2=1;
		for(int i=1;i<=n;i++)
		{
			int len=1;
			while(i+len<=n&&p[i].y==p[i+len].y)len++;
			sub2=(sub2*jie[len])%mod;
			i+=len-1;
		}
		bool f=0;
		//cout<<"1 ->"<<p[1].x<<" "<<p[1].y<<endl;
		for(int i=2;i<=n;i++)
		{
			//cout<<i<<" ->"<<p[i].x<<" "<<p[i].y<<endl;
			if(p[i].x<p[i-1].x)f=1;
		}
		LL add=1;
		if(f)add=0;
		for(int i=1;!f&&i<=n;i++)
		{
			int len=1;
			while(i+len<=n&&p[i].x==p[i+len].x&&p[i].y==p[i+len].y)len++;
			add=(add*jie[len])%mod;
			i+=len-1;
		}
		cout<<(ans+add-sub1-sub2+mod+mod)%mod<<endl;
	}
//	system("pause");
	return 0;
}
/*
10
1 2
1 1
1 2
2 1
1 2
2 2
2 1
1 2
1 2
2 2
10
1 2
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
#define mkp(a, b) make_pair(a, b)
#define F first
#define S second
#define pii pair<int, int>
#define mem0(a) memset(a, 0, sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define MID(l, r) (l + ((r - l) >> 1))
#define ll(o) (o << 1)
#define rr(o) (o << 1 | 1)
#define ep(a) emplace_back(a)
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 301000;
const double pi = acos(-1.0);
int T, n, m;
struct Node
{
	int x, y;
} p[maxn];
const LL mod = 998244353;
LL jie[maxn];
bool cmp1(Node &a, Node &b)
{
	if (a.x != b.x)
		return a.x < b.x;
	return a.y < b.y;
}
bool cmp2(Node &a, Node &b)
{
	if (a.y != b.y)
		return a.y < b.y;
	return a.x < b.x;
}
int main()
{
	while (cin >> n)
	{
		jie[0] = 1;
		for (int i = 1; i <= n; i++)
		{
			jie[i] = (jie[i - 1] * i * 1ll) % mod;
			scanf("%d%d", &p[i].x, &p[i].y);
		}
		LL ans = jie[n];
		sort(p + 1, p + 1 + n, cmp1);
		LL sub = 1;
		for (int i = 1; i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].x == p[i + len].x)
				len++;
			i += len;
			sub = (sub * jie[len]) % mod;
		}
		ans = (ans - sub + mod) % mod;
		bool f = 0;
		bool jia=0;
		for (int i = 2; i <= n; i++)
		{
			if (p[i - 1].y < p[i].y)
			{
				if (i == n){
					jia=1;
					ans++;
				}
			}
			else
				break;
		}
		LL sub2 = 1;
		sort(p + 1, p + 1 + n, cmp2);
		for (int i = 1; i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].y == p[i + len].y)
				len++;
			i += len;
			//cout<<len<<" -> "<<jie[len]<<endl;
			sub2 = (sub2 * jie[len]) % mod;
		}
		for (int i = 2; !jia&&i <= n; i++)
		{
			if (p[i - 1].x < p[i].x)
			{
				if (i == n)
					{jia=1;
					ans++;
					}
			}
			else
				break;
		}
		LL add = 0;
		bool flag = 1;
		for (int i = 2; i <= n; i++)
		{
			if (p[i - 1].x > p[i].x || p[i - 1].y > p[i].y)
			{
				flag = 0;
				break;
			}
		}
		for (int i = 1; flag && i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].x == p[i + len].x && p[i].y == p[i + len].y)
				len++;
			i += len;
			if (len > 1)
			{
				if (add == 0)
					add = 1;
				add = (add * jie[len]) % mod;
			}
		}
		ans = (ans + add - sub2 + mod) % mod;
		if(n==1)ans=0;
		//cout <<add<<" "<<sub<<" "<<sub2<<" "<<jie[n]<<" "<< ans << endl;
		cout << ans << endl;
	}
//	system("pause");
	return 0;
}
/*
10
1 2
1 1
1 2
2 1
1 2
2 2
2 1
1 2
1 2
2 2
10
1 2
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
#define mkp(a, b) make_pair(a, b)
#define F first
#define S second
#define pii pair<int, int>
#define mem0(a) memset(a, 0, sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define MID(l, r) (l + ((r - l) >> 1))
#define ll(o) (o << 1)
#define rr(o) (o << 1 | 1)
#define ep(a) emplace_back(a)
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 301000;
const double pi = acos(-1.0);
int T, n, m;
struct Node
{
	int x, y;
} p[maxn];
const LL mod = 998244353;
LL jie[maxn];
bool cmp1(Node &a, Node &b)
{
	if (a.x != b.x)
		return a.x < b.x;
	return a.y < b.y;
}
bool cmp2(Node &a, Node &b)
{
	if (a.y != b.y)
		return a.y < b.y;
	return a.x < b.x;
}
int main()
{
	while (cin >> n)
	{
		jie[0] = 1;
		for (int i = 1; i <= n; i++)
		{
			jie[i] = (jie[i - 1] * i * 1ll) % mod;
			scanf("%d%d", &p[i].x, &p[i].y);
		}
		LL ans=jie[n];
		sort(p+1,p+1+m,cmp1);
		LL sub1=1;
		for(int i=1;i<=n;i++)
		{
			int len=1;
			while(i+len<=n&&p[i].x==p[i+len].x)len++;
			sub1=(sub1*jie[len])%mod;
			i+=len-1;
		}
		sort(p+1,p+1+m,cmp2);
		LL sub2=1;
		for(int i=1;i<=n;i++)
		{
			int len=1;
			while(i+len<=n&&p[i].y==p[i+len].y)len++;
			sub2=(sub2*jie[len])%mod;
			i+=len-1;
		}
		bool f=0;
		//cout<<"1 ->"<<p[1].x<<" "<<p[1].y<<endl;
		for(int i=2;i<=n;i++)
		{
			//cout<<i<<" ->"<<p[i].x<<" "<<p[i].y<<endl;
			if(p[i].x<p[i-1].x)f=1;
		}
		LL add=1;
		if(f)add=0;
		for(int i=1;!f&&i<=n;i++)
		{
			int len=1;
			while(i+len<=n&&p[i].x==p[i+len].x&&p[i].y==p[i+len].y)len++;
			add=(add*jie[len])%mod;
			i+=len-1;
		}
		cout<<(ans+add-sub1-sub2+mod+mod)%mod<<endl;
	}
//	system("pause");
	return 0;
}
/*
10
1 2
1 1
1 2
2 1
1 2
2 2
2 1
1 2
1 2
2 2
10
1 2
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
#define mkp(a, b) make_pair(a, b)
#define F first
#define S second
#define pii pair<int, int>
#define mem0(a) memset(a, 0, sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define MID(l, r) (l + ((r - l) >> 1))
#define ll(o) (o << 1)
#define rr(o) (o << 1 | 1)
#define ep(a) emplace_back(a)
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 301000;
const double pi = acos(-1.0);
int T, n, m;
struct Node
{
	int x, y;
} p[maxn];
const LL mod = 998244353;
LL jie[maxn];
bool cmp1(Node &a, Node &b)
{
	if (a.x != b.x)
		return a.x < b.x;
	return a.y < b.y;
}
bool cmp2(Node &a, Node &b)
{
	if (a.y != b.y)
		return a.y < b.y;
	return a.x < b.x;
}
int main()
{
	while (cin >> n)
	{
		jie[0] = 1;
		for (int i = 1; i <= n; i++)
		{
			jie[i] = (jie[i - 1] * i * 1ll) % mod;
			scanf("%d%d", &p[i].x, &p[i].y);
		}
		LL ans = jie[n];
		sort(p + 1, p + 1 + n, cmp1);
		LL sub = 1;
		for (int i = 1; i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].x == p[i + len].x)
				len++;
			i += len;
			sub = (sub * jie[len]) % mod;
		}
		ans = (ans - sub + mod) % mod;
		bool f = 0;
		bool jia=0;
		for (int i = 2; i <= n; i++)
		{
			if (p[i - 1].y < p[i].y)
			{
				if (i == n){
					jia=1;
					ans++;
				}
			}
			else
				break;
		}
		LL sub2 = 1;
		sort(p + 1, p + 1 + n, cmp2);
		for (int i = 1; i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].y == p[i + len].y)
				len++;
			i += len;
			//cout<<len<<" -> "<<jie[len]<<endl;
			sub2 = (sub2 * jie[len]) % mod;
		}
		for (int i = 2; !jia&&i <= n; i++)
		{
			if (p[i - 1].x < p[i].x)
			{
				if (i == n)
					{jia=1;
					ans++;
					}
			}
			else
				break;
		}
		LL add = 0;
		bool flag = 1;
		for (int i = 2; i <= n; i++)
		{
			if (p[i - 1].x > p[i].x || p[i - 1].y > p[i].y)
			{
				flag = 0;
				break;
			}
		}
		for (int i = 1; flag && i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].x == p[i + len].x && p[i].y == p[i + len].y)
				len++;
			i += len;
			if (len > 1)
			{
				if (add == 0)
					add = 1;
				add = (add * jie[len]) % mod;
			}
		}
		ans = (ans + add - sub2 + mod) % mod;
		if(i==1)ans=0;
		//cout <<add<<" "<<sub<<" "<<sub2<<" "<<jie[n]<<" "<< ans << endl;
		cout << ans << endl;
	}
//	system("pause");
	return 0;
}
/*
10
1 2
1 1
1 2
2 1
1 2
2 2
2 1
1 2
1 2
2 2
10
1 2
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
#define mkp(a, b) make_pair(a, b)
#define F first
#define S second
#define pii pair<int, int>
#define mem0(a) memset(a, 0, sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define MID(l, r) (l + ((r - l) >> 1))
#define ll(o) (o << 1)
#define rr(o) (o << 1 | 1)
#define ep(a) emplace_back(a)
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 301000;
const double pi = acos(-1.0);
int T, n, m;
struct Node
{
	int x, y;
} p[maxn];
const LL mod = 998244353;
LL jie[maxn];
bool cmp1(Node &a, Node &b)
{
	if (a.x != b.x)
		return a.x < b.x;
	return a.y < b.y;
}
bool cmp2(Node &a, Node &b)
{
	if (a.y != b.y)
		return a.y < b.y;
	return a.x < b.x;
}
int main()
{
	while (cin >> n)
	{
		jie[0] = 1;
		for (int i = 1; i <= n; i++)
		{
			jie[i] = (jie[i - 1] * i * 1ll) % mod;
			scanf("%d%d", &p[i].x, &p[i].y);
		}
		LL ans = jie[n];
		sort(p + 1, p + 1 + n, cmp1);
		LL sub = 1;
		for (int i = 1; i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].x == p[i + len].x)
				len++;
			i += len;
			sub = (sub * jie[len]) % mod;
		}
		ans = (ans - sub + mod) % mod;
		bool f = 0;
		bool jia=0;
		for (int i = 2; i <= n; i++)
		{
			if (p[i - 1].y <= p[i].y)
			{
				if (i == n){
					jia=1;
					ans++;
				}
			}
			else
				break;
		}
		LL sub2 = 1;
		sort(p + 1, p + 1 + n, cmp2);
		for (int i = 1; i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].y == p[i + len].y)
				len++;
			i += len;
			//cout<<len<<" -> "<<jie[len]<<endl;
			sub2 = (sub2 * jie[len]) % mod;
		}
		for (int i = 2; !jia&&i <= n; i++)
		{
			if (p[i - 1].x <= p[i].x)
			{
				if (i == n)
					{jia=1;
					ans++;
					}
			}
			else
				break;
		}
		LL add = 0;
		bool flag = 1;
		for (int i = 2; i <= n; i++)
		{
			if (p[i - 1].x > p[i].x || p[i - 1].y > p[i].y)
			{
				flag = 0;
				break;
			}
		}
		for (int i = 1; flag && i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].x == p[i + len].x && p[i].y == p[i + len].y)
				len++;
			i += len;
			if (len > 1)
			{
				if (add == 0)
					add = 1;
				add = (add * jie[len]) % mod;
			}
		}
		ans = (ans + add - sub2 + mod) % mod;
		//cout <<add<<" "<<sub<<" "<<sub2<<" "<<jie[n]<<" "<< ans << endl;
		cout << ans << endl;
	}
//	system("pause");
	return 0;
}
/*
10
1 2
1 1
1 2
2 1
1 2
2 2
2 1
1 2
1 2
2 2
10
1 2
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
#define mkp(a, b) make_pair(a, b)
#define F first
#define S second
#define pii pair<int, int>
#define mem0(a) memset(a, 0, sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define MID(l, r) (l + ((r - l) >> 1))
#define ll(o) (o << 1)
#define rr(o) (o << 1 | 1)
#define ep(a) emplace_back(a)
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 301000;
const double pi = acos(-1.0);
int T, n, m;
struct Node
{
	int x, y;
} p[maxn];
const LL mod = 998244353;
LL jie[maxn];
bool cmp1(Node &a, Node &b)
{
	if (a.x != b.x)
		return a.x < b.x;
	return a.y < b.y;
}
bool cmp2(Node &a, Node &b)
{
	if (a.y != b.y)
		return a.y < b.y;
	return a.x < b.x;
}
int main()
{
	while (cin >> n)
	{
		jie[0] = 1;
		for (int i = 1; i <= n; i++)
		{
			jie[i] = (jie[i - 1] * i * 1ll) % mod;
			scanf("%d%d", &p[i].x, &p[i].y);
		}
		LL ans = jie[n];
		sort(p + 1, p + 1 + n, cmp1);
		LL sub = 1;
		for (int i = 1; i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].x == p[i + len].x)
				len++;
			i += len;
			sub = (sub * jie[len]) % mod;
		}
		ans = (ans - sub + mod) % mod;
		bool f = 0;
		bool jia=0;
		for (int i = 2; i <= n; i++)
		{
			if (p[i - 1].y < p[i].y)
			{
				if (i == n){
					jia=1;
					ans++;
				}
			}
			else
				break;
		}
		LL sub2 = 1;
		sort(p + 1, p + 1 + n, cmp2);
		for (int i = 1; i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].y == p[i + len].y)
				len++;
			i += len;
			//cout<<len<<" -> "<<jie[len]<<endl;
			sub2 = (sub2 * jie[len]) % mod;
		}
		for (int i = 2; !jia&&i <= n; i++)
		{
			if (p[i - 1].x < p[i].x)
			{
				if (i == n)
					{jia=1;
					ans++;
					}
			}
			else
				break;
		}
		LL add = 0;
		bool flag = 1;
		for (int i = 2; i <= n; i++)
		{
			if (p[i - 1].x > p[i].x || p[i - 1].y > p[i].y)
			{
				flag = 0;
				break;
			}
		}
		for (int i = 1; flag && i <= n;)
		{
			int len = 0;
			while (i + len <= n && p[i].x == p[i + len].x && p[i].y == p[i + len].y)
				len++;
			i += len;
			if (len > 1)
			{
				if (add == 0)
					add = 1;
				add = (add * jie[len]) % mod;
			}
		}
		ans = (ans + add - sub2 + mod) % mod;
		//cout <<add<<" "<<sub<<" "<<sub2<<" "<<jie[n]<<" "<< ans << endl;
		cout << ans << endl;
	}
	//system("pause");
	return 0;
}
/*
10
1 2
1 1
1 2
2 1
1 2
2 2
2 1
1 2
1 2
2 2
10
1 2
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
*/


#include<bits/stdc++.h>
using namespace std;
#define maxn 1005
#define ll long long
#define inf 1000000009
#define IOS ios::sync_with_stdio(false)
#define mod  998244353

vector<pair<ll,ll> >ff;
bool cmp(pair<ll,ll>a,pair<ll,ll> b)
{
    if(a.second==b.second)
        return a.first<b.first;
    return a.second<b.second;
}

int main()
{
    IOS;
    ll n;
    cin>>n;
    ll per=1;
    for(ll i=1; i<=n; i++)
    {
        per*=i;
        per%=mod;
    }

    for(ll i=0; i<n; i++)
    {
        ll x,y;
        cin>>x>>y;
        ff.push_back(make_pair(x,y));
    }

    sort(ff.begin(),ff.end());
    bool flag=true;
    for(ll i=0; i<ff.size()-1; i++)
        if(ff[i].second>ff[i+1].second)
        {
            flag=false;
            break;
        }

    ll cnt3=1;
    for(ll i=0; i<ff.size()-1;)
    {
        ll sum=1,cc=2;
        pair<ll,ll> now=ff[i];
        if(i+1<ff.size()&&now==ff[i+1])
        {
            while(ff[++i]==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt3*=sum;
        cnt3%=mod;
    }

    ll cnt1=1;
    for(ll i=0; i<ff.size()-1;)
    {
        ll sum=1,cc=2;
        ll now=ff[i].first;
        if(now==ff[i+1].first)
        {
            while(i+1<ff.size()&&ff[++i].first==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt1*=sum;
        cnt1%=mod;
    }

    sort(ff.begin(),ff.end(),cmp);
    ll cnt2=1;
    for(ll i=0; i<ff.size()-1;)
    {
        ll sum=1,cc=2;
        ll now=ff[i].second;
        if(now==ff[i+1].second)
        {
            while(i+1<ff.size()&&ff[++i].second==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt2*=sum;
        cnt2%=mod;
    }

    if(!flag)
        cnt3=0;
//cout<<per<<" "<<cnt1<<" "<<cnt2<<" "<<cnt3<<endl;
    cout<<(((per-cnt1)%mod-cnt2)%mod+cnt3)%mod;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 1005
#define ll long long
#define inf 1000000009
#define IOS ios::sync_with_stdio(false)
#define mod  998244353

vector<pair<ll,ll> >ff;

bool cmp(pair<ll,ll>a,pair<ll,ll> b)
{
    return a.second<b.second;
}

int main()
{
    IOS;
    ll n;
    cin>>n;
    ll per=1;
    for(ll i=1; i<=n; i++)
    {
        per*=i;
        per%=mod;
    }

    for(ll i=0; i<n; i++)
    {
        ll x,y;
        cin>>x>>y;
        ff.push_back(make_pair(x,y));
    }

    sort(ff.begin(),ff.end());
    bool flag=true;
    for(ll i=0; i<ff.size()-1; i++)
        if(ff[i].second>ff[i+1].second)
        {
            flag=false;
            break;
        }

    ll cnt3=1;
    for(ll i=0; i<ff.size();)
    {
        ll sum=1,cc=2;
        pair<ll,ll> now=ff[i];
        if(i+1<ff.size()&&now==ff[i+1])
        {
            while(i<ff.size()&&ff[++i]==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt3*=sum;
        cnt3%=mod;
    }

    ll cnt1=1;
    for(ll i=0; i<ff.size();)
    {
        ll sum=1,cc=2;
        ll now=ff[i].first;
        if(i+1<ff.size()&&now==ff[i+1].first)
        {
            while(i<ff.size()&&ff[++i].first==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt1*=sum;
        cnt1%=mod;
    }

    sort(ff.begin(),ff.end(),cmp);
    ll cnt2=1;
    for(ll i=0; i<ff.size();)
    {
        ll sum=1,cc=2;
        ll now=ff[i].second;
        if(i+1<ff.size()&&now==ff[i+1].second)
        {
            while(i<ff.size()&&ff[++i].second==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt2*=sum;
        cnt2%=mod;
    }

    //cout<<per<<" "<<cnt1<<" "<<cnt2<<" "<<cnt3<<endl;

    if(!flag)
        cnt3=0;

    cout<<(((per-cnt1)%mod-cnt2)%mod+cnt3)%mod;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 1005
#define ll long long
#define inf 1000000009
#define IOS ios::sync_with_stdio(false)
#define mod  998244353

vector<pair<ll,ll> >ff;

bool cmp(pair<ll,ll>a,pair<ll,ll> b)
{
    if(a.second==b.second)
        return a.first<b.first;
    return a.second<b.second;
}

int main()
{
    IOS;
    ll n;
    cin>>n;
    ll per=1;
    for(ll i=1; i<=n; i++)
    {
        per*=i;
        per%=mod;
    }

    for(ll i=0; i<n; i++)
    {
        ll x,y;
        cin>>x>>y;
        x%=mod,y%=mod;
        ff.push_back(make_pair(x,y));
    }

    sort(ff.begin(),ff.end());
    bool flag=true;
    for(ll i=0; i<ff.size()-1; i++)
        if(ff[i].second>ff[i+1].second)
        {
            flag=false;
            break;
        }

    ll cnt3=1;
    for(ll i=0; i<ff.size();)
    {
        ll sum=1,cc=2;
        pair<ll,ll> now=ff[i];
        if(i+1<ff.size()&&now==ff[i+1])
        {
            while(i<ff.size()&&ff[++i]==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt3*=sum;
        cnt3%=mod;
    }

    ll cnt1=1;
    for(ll i=0; i<ff.size();)
    {
        ll sum=1,cc=2;
        ll now=ff[i].first;
        if(i+1<ff.size()&&now==ff[i+1].first)
        {
            while(i<ff.size()&&ff[++i].first==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt1*=sum;
        cnt1%=mod;
    }

    sort(ff.begin(),ff.end(),cmp);
    ll cnt2=1;
    for(ll i=0; i<ff.size();)
    {
        ll sum=1,cc=2;
        ll now=ff[i].second;
        if(i+1<ff.size()&&now==ff[i+1].second)
        {
            while(i<ff.size()&&ff[++i].second==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt2*=sum;
        cnt2%=mod;
    }

    //cout<<per<<" "<<cnt1<<" "<<cnt2<<" "<<cnt3<<endl;

    if(!flag)
        cnt3=0;

    cout<<(((per-cnt1)%mod-cnt2)%mod+cnt3)%mod;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 1005
#define ll long long
#define inf 1000000009
#define IOS ios::sync_with_stdio(false)
#define mod  998244353

vector<pair<ll,ll> >ff;

bool cmp(pair<ll,ll>a,pair<ll,ll> b)
{
    if(a.second==b.second)
        return a.first<b.first;
    return a.second<b.second;
}

int main()
{
    IOS;
    ll n;
    cin>>n;
    ll per=1;
    for(ll i=1; i<=n; i++)
    {
        per*=i;
        per%=mod;
    }

    for(ll i=0; i<n; i++)
    {
        ll x,y;
        cin>>x>>y;
        x%=mod,y%=mod;
        ff.push_back(make_pair(x,y));
    }

    sort(ff.begin(),ff.end());
    bool flag=true;
    for(ll i=0; i<ff.size()-1; i++)
        if(ff[i].second>ff[i+1].second)
        {
            flag=false;
            break;
        }

    ll cnt3=1;
    for(ll i=0; i<ff.size();)
    {
        ll sum=1,cc=2;
        pair<ll,ll> now=ff[i];
        if(i+1<ff.size()&&now==ff[i+1])
        {
            while(i+1<ff.size()&&ff[++i]==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt3*=sum;
        cnt3%=mod;
    }

    ll cnt1=1;
    for(ll i=0; i<ff.size();)
    {
        ll sum=1,cc=2;
        ll now=ff[i].first;
        if(i+1<ff.size()&&now==ff[i+1].first)
        {
            while(i+1<ff.size()&&ff[++i].first==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt1*=sum;
        cnt1%=mod;
    }

    sort(ff.begin(),ff.end(),cmp);
    ll cnt2=1;
    for(ll i=0; i<ff.size();)
    {
        ll sum=1,cc=2;
        ll now=ff[i].second;
        if(i+1<ff.size()&&now==ff[i+1].second)
        {
            while(i+1<ff.size()&&ff[++i].second==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt2*=sum;
        cnt2%=mod;
    }

    //cout<<per<<" "<<cnt1<<" "<<cnt2<<" "<<cnt3<<endl;

    if(!flag)
        cnt3=0;

    cout<<(((per-cnt1)%mod-cnt2)%mod+cnt3)%mod;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 1005
#define ll long long
#define inf 1000000009
#define IOS ios::sync_with_stdio(false)
#define mod  998244353

vector<pair<int,int> >ff;

bool cmp(pair<int,int>a,pair<int,int> b)
{
    return a.second<b.second;
}

int main()
{
    IOS;
    int n;
    cin>>n;
    ll per=1;
    for(ll i=1; i<=n; i++)
    {
        per*=i;
        per%=mod;
    }

    for(int i=0; i<n; i++)
    {
        int x,y;
        cin>>x>>y;
        ff.push_back(make_pair(x,y));
    }

    sort(ff.begin(),ff.end());
    bool flag=true;
    for(ll i=0; i<ff.size()-1; i++)
    {
        if(ff[i].second>ff[i+1].second)
        {
            flag=false;
            break;
        }
    }

    ll cnt1=1;
    for(ll i=0; i<ff.size();)
    {
        ll sum=1,cc=2;
        ll now=ff[i].first;
        if(i+1<ff.size()&&now==ff[i+1].first)
        {
            while(i<ff.size()&&ff[++i].first==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt1*=sum;
        cnt1%=mod;
    }

    sort(ff.begin(),ff.end(),cmp);
    ll cnt2=1;
    for(ll i=0; i<ff.size();)
    {
        ll sum=1,cc=2;
        ll now=ff[i].second;
        if(i+1<ff.size()&&now==ff[i+1].second)
        {
            while(i<ff.size()&&ff[++i].second==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt2*=sum;
        cnt2%=mod;
    }
    //cout<<per<<" "<<cnt1<<" "<<cnt2<<endl;
    if(flag)
        cout<<(per-cnt1)%mod;
    else
        cout<<(per-cnt1-cnt2)%mod;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 1005
#define ll long long
#define inf 1000000009
#define IOS ios::sync_with_stdio(false)
#define mod  998244353

vector<pair<ll,ll> >ff;
bool cmp(pair<ll,ll>a,pair<ll,ll> b)
{
    if(a.second==b.second)
        return a.first<b.first;
    return a.second<b.second;
}

int main()
{
    IOS;
    ll n;
    cin>>n;
    ll per=1LL;
    for(ll i=1; i<=n; i++)
    {
        per*=i;
        per%=mod;
    }

    for(ll i=0; i<n; i++)
    {
        ll x,y;
        cin>>x>>y;
        ff.push_back(make_pair(x,y));
    }

    sort(ff.begin(),ff.end());
    bool flag=true;
    for(ll i=0; i<ff.size()-1; i++)
        if(ff[i].second>ff[i+1].second)
        {
            flag=false;
            break;
        }

    ll cnt3=1LL;
    for(ll i=0; i<ff.size()-1;)
    {
        ll sum=1,cc=1;
        pair<ll,ll> now=ff[i];
        if(now==ff[i+1])
        {
            while(ff[i]==now&&i<ff.size())
            {
                i++;
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt3*=sum;
        cnt3%=mod;
    }

    ll cnt1=1LL;
    for(ll i=0; i<ff.size()-1;)
    {
        ll sum=1,cc=1;
        ll now=ff[i].first;
        if(now==ff[i+1].first)
        {
            while(i<ff.size()&&ff[i].first==now)
            {
                i++;
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt1*=sum;
        cnt1%=mod;
    }

    sort(ff.begin(),ff.end(),cmp);
    ll cnt2=1LL;
    for(ll i=0; i<ff.size()-1;)
    {
        ll sum=1,cc=1;
        ll now=ff[i].second;
        if(now==ff[i+1].second)
        {
            while(i<ff.size()&&ff[i].second==now)
            {
                i++;
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt2*=sum;
        cnt2%=mod;
    }

    if(!flag)
        cnt3=0LL;
    //cout<<per<<" "<<cnt1<<" "<<cnt2<<" "<<cnt3<<endl;
    cout<<(((per-cnt1)%mod-cnt2)%mod+cnt3)%mod;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 1005
#define ll long long
#define inf 1000000009
#define IOS ios::sync_with_stdio(false)
#define mod  998244353

vector<pair<ll,ll> >ff;
bool cmp(pair<ll,ll>a,pair<ll,ll> b)
{
    if(a.second==b.second)
        return a.first<b.first;
    return a.second<b.second;
}

int main()
{
    IOS;
    ll n;
    cin>>n;
    ll per=1LL;
    for(ll i=1; i<=n; i++)
    {
        per*=i;
        per%=mod;
    }

    for(ll i=0; i<n; i++)
    {
        ll x,y;
        cin>>x>>y;
        ff.push_back(make_pair(x,y));
    }

    sort(ff.begin(),ff.end());
    bool flag=true;
    for(ll i=0; i<ff.size()-1; i++)
        if(ff[i].second>ff[i+1].second)
        {
            flag=false;
            break;
        }

    ll cnt3=1LL;
    for(ll i=0; i<ff.size()-1;)
    {
        ll sum=1,cc=1;
        pair<ll,ll> now=ff[i];
        if(now==ff[i+1])
        {
            while(ff[i]==now&&i<ff.size())
            {
                i++;
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt3*=sum;
        cnt3%=mod;
    }

    ll cnt1=1LL;
    for(ll i=0; i<ff.size()-1;)
    {
        ll sum=1,cc=1;
        ll now=ff[i].first;
        if(now==ff[i+1].first)
        {
            while(i<ff.size()&&ff[i].first==now)
            {
                i++;
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt1*=sum;
        cnt1%=mod;
    }

    sort(ff.begin(),ff.end(),cmp);
    ll cnt2=1LL;
    for(ll i=0; i<ff.size()-1;)
    {
        ll sum=1,cc=1;
        ll now=ff[i].second;
        if(now==ff[i+1].second)
        {
            while(i<ff.size()&&ff[i].second==now)
            {
                i++;
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt2*=sum;
        cnt2%=mod;
    }

    if(!flag)
        cnt3=0LL;
    //cout<<per<<" "<<cnt1<<" "<<cnt2<<" "<<cnt3<<endl;
    cout<<(((per+mod-cnt1)%mod+mod-cnt2)%mod+cnt3)%mod;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 1005
#define ll long long
#define inf 1000000009
#define IOS ios::sync_with_stdio(false)
#define mod  998244353

vector<pair<int,int> >ff;

bool cmp(pair<int,int>a,pair<int,int> b)
{
    return a.second<b.second;
}

int main()
{
    IOS;
    int n;
    cin>>n;
    ll per=1;
    for(ll i=1; i<=n; i++)
    {
        per*=i;
        per%=mod;
    }

    for(int i=0; i<n; i++)
    {
        int x,y;
        cin>>x>>y;
        ff.push_back(make_pair(x,y));
    }

    sort(ff.begin(),ff.end());
    bool flag=true;
    for(int i=0; i<ff.size()-1; i++)
        if(ff[i].second>ff[i+1].second)
        {
            flag=false;
            break;
        }

    ll cnt3=1;
    for(ll i=0; i<ff.size();)
    {
        ll sum=1,cc=2;
        pair<int,int> now=ff[i];
        if(i+1<ff.size()&&now==ff[i+1])
        {
            while(i<ff.size()&&ff[++i]==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt3*=sum;
        cnt3%=mod;
    }

    ll cnt1=1;
    for(ll i=0; i<ff.size();)
    {
        ll sum=1,cc=2;
        ll now=ff[i].first;
        if(i+1<ff.size()&&now==ff[i+1].first)
        {
            while(i<ff.size()&&ff[++i].first==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt1*=sum;
        cnt1%=mod;
    }

    sort(ff.begin(),ff.end(),cmp);
    ll cnt2=1;
    for(ll i=0; i<ff.size();)
    {
        ll sum=1,cc=2;
        ll now=ff[i].second;
        if(i+1<ff.size()&&now==ff[i+1].second)
        {
            while(i<ff.size()&&ff[++i].second==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt2*=sum;
        cnt2%=mod;
    }

    //cout<<per<<" "<<cnt1<<" "<<cnt2<<" "<<cnt3<<endl;

    if(!flag)
        cnt3=0;
    cout<<(per-cnt1-cnt2+cnt3)%mod;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 1005
#define ll long long
#define inf 1000000009
#define IOS ios::sync_with_stdio(false)
#define mod  998244353

vector<pair<ll,ll> >ff;
bool cmp(pair<ll,ll>a,pair<ll,ll> b)
{
    if(a.second==b.second)
        return a.first<b.first;
    return a.second<b.second;
}

int main()
{
    IOS;
    ll n;
    cin>>n;
    ll per=1;
    for(ll i=1; i<=n; i++)
    {
        per*=i;
        per%=mod;
    }

    for(ll i=0; i<n; i++)
    {
        ll x,y;
        cin>>x>>y;
        ff.push_back(make_pair(x,y));
    }

    sort(ff.begin(),ff.end());
    bool flag=true;
    for(ll i=0; i<ff.size()-1; i++)
        if(ff[i].second>ff[i+1].second)
        {
            flag=false;
            break;
        }

    ll cnt3=1;
    for(ll i=0; i<ff.size();)
    {
        ll sum=1,cc=2;
        pair<ll,ll> now=ff[i];
        if(i+1<ff.size()&&now==ff[i+1])
        {
            while(i+1<ff.size()&&ff[++i]==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt3*=sum;
        cnt3%=mod;
    }

    ll cnt1=1;
    for(ll i=0; i<ff.size();)
    {
        ll sum=1,cc=2;
        ll now=ff[i].first;
        if(i+1<ff.size()&&now==ff[i+1].first)
        {
            while(i+1<ff.size()&&ff[++i].first==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt1*=sum;
        cnt1%=mod;
    }

    sort(ff.begin(),ff.end(),cmp);
    ll cnt2=1;
    for(ll i=0; i<ff.size();)
    {
        ll sum=1,cc=2;
        ll now=ff[i].second;
        if(i+1<ff.size()&&now==ff[i+1].second)
        {
            while(i+1<ff.size()&&ff[++i].second==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt2*=sum;
        cnt2%=mod;
    }

    if(!flag)
        cnt3=0;
//cout<<per<<" "<<cnt1<<" "<<cnt2<<" "<<cnt3<<endl;
    cout<<(((per%mod-cnt1%mod)%mod-cnt2%mod)%mod+cnt3%mod)%mod;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 1005
#define ll long long
#define inf 1000000009
#define IOS ios::sync_with_stdio(false)
#define mod  998244353

vector<pair<int,int> >ff;

bool cmp(pair<int,int>a,pair<int,int> b)
{
    return a.second<b.second;
}

int main()
{
    IOS;
    int n;
    cin>>n;
    ll per=1;
    for(ll i=1; i<=n; i++)
    {
        per*=i;
        per%=mod;
    }

    for(int i=0; i<n; i++)
    {
        int x,y;
        cin>>x>>y;
        ff.push_back(make_pair(x,y));
    }

    sort(ff.begin(),ff.end());
    bool flag=true;
    for(int i=0; i<ff.size()-1; i++)
        if(ff[i].second>ff[i+1].second)
        {
            flag=false;
            break;
        }

    ll cnt3=1;
    for(ll i=0; i<ff.size();)
    {
        ll sum=1,cc=2;
        pair<int,int> now=ff[i];
        if(i+1<ff.size()&&now==ff[i+1])
        {
            while(i<ff.size()&&ff[++i]==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt3*=sum;
        cnt3%=mod;
    }

    ll cnt1=1;
    for(ll i=0; i<ff.size();)
    {
        ll sum=1,cc=2;
        ll now=ff[i].first;
        if(i+1<ff.size()&&now==ff[i+1].first)
        {
            while(i<ff.size()&&ff[++i].first==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt1*=sum;
        cnt1%=mod;
    }

    sort(ff.begin(),ff.end(),cmp);
    ll cnt2=1;
    for(ll i=0; i<ff.size();)
    {
        ll sum=1,cc=2;
        ll now=ff[i].second;
        if(i+1<ff.size()&&now==ff[i+1].second)
        {
            while(i<ff.size()&&ff[++i].second==now)
            {
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt2*=sum;
        cnt2%=mod;
    }

    //cout<<per<<" "<<cnt1<<" "<<cnt2<<" "<<cnt3<<endl;

    if(!flag)
        cnt3=0;

    cout<<(((per-cnt1)%mod-cnt2)%mod+cnt3)%mod;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 1005
#define ll long long
#define inf 1000000009
#define IOS ios::sync_with_stdio(false)
#define mod  998244353

vector<pair<ll,ll> >ff;
bool cmp(pair<ll,ll>a,pair<ll,ll> b)
{
    if(a.second==b.second)
        return a.first<b.first;
    return a.second<b.second;
}

int main()
{
    IOS;
    ll n;
    cin>>n;
    ll per=1;
    for(ll i=1; i<=n; i++)
    {
        per*=i;
        per%=mod;
    }

    for(ll i=0; i<n; i++)
    {
        ll x,y;
        cin>>x>>y;
        ff.push_back(make_pair(x,y));
    }

    sort(ff.begin(),ff.end());
    bool flag=true;
    for(ll i=0; i<ff.size()-1; i++)
        if(ff[i].second>ff[i+1].second)
        {
            flag=false;
            break;
        }

    ll cnt3=1;
    for(ll i=0; i<ff.size()-1;)
    {
        ll sum=1,cc=1;
        pair<ll,ll> now=ff[i];
        if(now==ff[i+1])
        {
            while(ff[i]==now&&i<ff.size())
            {
                i++;
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt3*=sum;
        cnt3%=mod;
    }

    ll cnt1=1;
    for(ll i=0; i<ff.size()-1;)
    {
        ll sum=1,cc=1;
        ll now=ff[i].first;
        if(now==ff[i+1].first)
        {
            while(i<ff.size()&&ff[i].first==now)
            {
                i++;
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt1*=sum;
        cnt1%=mod;
    }

    sort(ff.begin(),ff.end(),cmp);
    ll cnt2=1;
    for(ll i=0; i<ff.size()-1;)
    {
        ll sum=1,cc=1;
        ll now=ff[i].second;
        if(now==ff[i+1].second)
        {
            while(i<ff.size()&&ff[i].second==now)
            {
                i++;
                sum*=cc;
                cc++;
                sum%=mod;
            }
        }
        else
            i++;
        cnt2*=sum;
        cnt2%=mod;
    }

    if(!flag)
        cnt3=0;
    //cout<<per<<" "<<cnt1<<" "<<cnt2<<" "<<cnt3<<endl;
    cout<<(((per-cnt1)%mod-cnt2)%mod+cnt3)%mod;
    return 0;
}


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

const int m = 3e5 + 5, MOD = 998244353;
int n;
int fact[m];
vector<pair<int, int> > vec;
map<int, int> mpf, mps;
map<pair<int, int>, int> mpfs;

int32_t main()
{
	fact[0] = 0LL;
	fact[1] = 1LL;
	for (int i = 2LL; i <= m; ++i)
		fact[i] = (fact[i - 1] * i) % MOD;

	cin >> n;
	int x, y;
	for (int i = 0; i < n; ++i)
	{
		cin >> x >> y;
		mpf[x]++;
		mps[y]++;
		vec.push_back({x, y});
		mpfs[{x, y}]++;
	}

	int cnt1 = 1, cnt2 = 1, cnt12 = 1;

	for (auto i : mpf)
		cnt1 = (cnt1 % MOD * fact[i.second] % MOD) % MOD;

	for (auto i : mps)
		cnt2 = (cnt2 % MOD * fact[i.second] % MOD) % MOD;

	sort(vec.begin(), vec.end());
	bool flag = true;

	for (int i = 0; i < n - 1; ++i)
		if (vec[i].second > vec[i + 1].second)
		{
			cnt12 = 0;
			flag = false;
			break;
		}

	if (flag)
		for (auto i : mpfs)
			cnt12 = (cnt12 % MOD * fact[i.second] % MOD) % MOD;

	cout << fact[n] - cnt1 - cnt2 + cnt12 << "\n";
}


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

const int m = 3e5 + 5, MOD = 998244353;
int n;
int fact[m];
vector<pair<int, int> > vec;
map<int, int> mpf, mps;
map<pair<int, int>, int> mpfs;

int32_t main()
{
	fact[0] = 0;
	fact[1] = 1;
	for (int i = 2; i < m; ++i)
		fact[i] = (fact[i - 1] * i) % MOD;

	cin >> n;
	int x, y;
	for (int i = 0; i < n; ++i)
	{
		cin >> x >> y;
		mpf[x]++;
		mps[y]++;
		vec.push_back({x, y});
		mpfs[{x, y}]++;
	}

	int cnt1 = 1, cnt2 = 1, cnt12 = 1;

	for (auto i : mpf)
		cnt1 = (cnt1 * fact[i.second]) % MOD;

	for (auto i : mps)
		cnt2 = (cnt2 * fact[i.second]) % MOD;

	sort(vec.begin(), vec.end());
	bool flag = true;

	for (int i = 0; i < n - 1; ++i)
		if (vec[i].second > vec[i + 1].second)
			cnt12 = 0;

	for (auto i : mpfs)
		cnt12 = (cnt12 * fact[i.second]) % MOD;
	cout << fact[n] - cnt1 - cnt2 + cnt12 << "\n";
	if (n == 300000)
		cout << fact[n] << " " << cnt1 << " " << cnt2 << " " << cnt12 << "\n";
}


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

const int m = 3e5 + 5, MOD = 998244353;
int n;
int fact[m];
vector<pair<int, int> > vec;
map<int, int> mpf, mps;
map<pair<int, int>, int> mpfs;

int32_t main()
{
	fact[0] = 0;
	fact[1] = 1;
	for (int i = 2; i < m; ++i)
		fact[i] = (fact[i - 1] % MOD * i % MOD) % MOD;

	cin >> n;
	int x, y;
	for (int i = 0; i < n; ++i)
	{
		cin >> x >> y;
		mpf[x]++;
		mps[y]++;
		vec.push_back({x, y});
		mpfs[{x, y}]++;
	}

	int cnt1 = 1, cnt2 = 1, cnt12 = 1;

	for (auto i : mpf)
		cnt1 = (cnt1 % MOD * fact[i.second] % MOD) % MOD;

	for (auto i : mps)
		cnt2 = (cnt2 % MOD * fact[i.second] % MOD) % MOD;

	sort(vec.begin(), vec.end());
	bool flag = true;

	for (int i = 0; i < n - 1; ++i)
		if (vec[i].second > vec[i + 1].second)
		{
			cnt12 = 0;
			flag = false;
			break;
		}

	if (flag)
		for (auto i : mpfs)
			cnt12 = (cnt12 % MOD * fact[i.second] % MOD) % MOD;

	cout << fact[n] - cnt1 - cnt2 + cnt12 << "\n";
}


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

const int m = 3e5 + 5, MOD = 998244353;
int n;
int fact[m];
vector<pair<int, int> > vec;
map<int, int> mpf, mps;
map<pair<int, int>, int> mpfs;

int32_t main()
{
	fact[0] = 0;
	fact[1] = 1;
	for (int i = 2; i <= m; ++i)
		fact[i] = (fact[i - 1] * i) % MOD;

	cin >> n;
	int x, y;
	for (int i = 0; i < n; ++i)
	{
		cin >> x >> y;
		mpf[x]++;
		mps[y]++;
		vec.push_back({x, y});
		mpfs[{x, y}]++;
	}

	int cnt1 = 1, cnt2 = 1, cnt12 = 1;

	for (auto i : mpf)
		cnt1 = (cnt1 % MOD * fact[i.second] % MOD) % MOD;

	for (auto i : mps)
		cnt2 = (cnt2 % MOD * fact[i.second] % MOD) % MOD;

	sort(vec.begin(), vec.end());
	bool flag = true;

	for (int i = 0; i < n - 1; ++i)
		if (vec[i].second > vec[i + 1].second)
		{
			cnt12 = 0;
			flag = false;
			break;
		}

	if (flag)
		for (auto i : mpfs)
			cnt12 = (cnt12 % MOD * fact[i.second] % MOD) % MOD;

	cout << fact[n] - cnt1 - cnt2 + cnt12 << "\n";
}


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

const int m = 3e5 + 5, MOD = 998244353;
int n;
int fact[m];
vector<pair<int, int> > vec;
map<int, int> mpf, mps;
map<pair<int, int>, int> mpfs;

int32_t main()
{
	fact[0] = 0;
	fact[1] = 1;
	for (int i = 2; i < m; ++i)
		fact[i] = (fact[i - 1] * i) % MOD;

	cin >> n;
	int x, y;
	for (int i = 0; i < n; ++i)
	{
		cin >> x >> y;
		mpf[x]++;
		mps[y]++;
		vec.push_back({x, y});
		mpfs[{x, y}]++;
	}

	int cnt1 = 1, cnt2 = 1, cnt12 = 1;

	for (auto i : mpf)
		cnt1 = (cnt1 * fact[i.second]) % MOD;

	for (auto i : mps)
		cnt2 = (cnt2 * fact[i.second]) % MOD;

	sort(vec.begin(), vec.end());
	bool flag = true;

	for (int i = 0; i < n - 1; ++i)
		if (vec[i].second > vec[i + 1].second)
			cnt12 = 0;

	for (auto i : mpfs)
		cnt12 = (cnt12 * fact[i.second]) % MOD;

	int ans = fact[n] - cnt1 - cnt2 + cnt12;

	cout << (MOD + (ans % MOD)) % MOD << "\n";
}


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

const int m = 3e5 + 5, MOD = 998244353LL;
int n;
int fact[m];
vector<pair<int, int> > vec;
map<int, int> mpf, mps;
map<pair<int, int>, int> mpfs;

int32_t main()
{
	fact[0] = 0LL;
	fact[1] = 1LL;
	for (int i = 2LL; i <= m; ++i)
		fact[i] = (fact[i - 1] * i) % MOD;

	cin >> n;
	int x, y;
	for (int i = 0; i < n; ++i)
	{
		cin >> x >> y;
		mpf[x]++;
		mps[y]++;
		vec.push_back({x, y});
		mpfs[{x, y}]++;
	}

	int cnt1 = 1, cnt2 = 1, cnt12 = 1;

	for (auto i : mpf)
		cnt1 = (cnt1 % MOD * fact[i.second] % MOD) % MOD;

	for (auto i : mps)
		cnt2 = (cnt2 % MOD * fact[i.second] % MOD) % MOD;

	sort(vec.begin(), vec.end());
	bool flag = true;

	for (int i = 0; i < n - 1; ++i)
		if (vec[i].second > vec[i + 1].second)
		{
			cnt12 = 0;
			flag = false;
			break;
		}

	if (flag)
		for (auto i : mpfs)
			cnt12 = (cnt12 % MOD * fact[i.second] % MOD) % MOD;

	cout << fact[n] - cnt1 - cnt2 + cnt12 << "\n";
}


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

const int m = 3e5 + 5, MOD = 998244353;
int n;
int fact[m];
vector<pair<int, int> > vec;
map<int, int> mpf, mps;
map<pair<int, int>, int> mpfs;

int main()
{
	fact[0] = 0;
	fact[1] = 1;
	for (int i = 2; i <= m; ++i)
		fact[i] = (fact[i - 1] * i) % MOD;

	cin >> n;
	int x, y;
	for (int i = 0; i < n; ++i)
	{
		cin >> x >> y;
		mpf[x]++;
		mps[y]++;
		vec.push_back({x, y});
		mpfs[{x, y}]++;
	}

	int cnt1 = 1, cnt2 = 1, cnt12 = 1;

	for (auto i : mpf)
		cnt1 = (cnt1 % MOD * fact[i.second] % MOD) % MOD;

	for (auto i : mps)
		cnt2 = (cnt2 % MOD * fact[i.second] % MOD) % MOD;

	sort(vec.begin(), vec.end());
	bool flag = true;

	for (int i = 0; i < n - 1; ++i)
		if (vec[i].second > vec[i + 1].second)
		{
			cnt12 = 0;
			flag = false;
			break;
		}

	if (flag)
		for (auto i : mpfs)
			cnt12 = (cnt12 % MOD * fact[i.second] % MOD) % MOD;

	cout << fact[n] - cnt1 - cnt2 + cnt12 << "\n";
}


#include <iostream>
using namespace std;

int main()
{
	long long n, k, m;
	while(cin >> n >> k >> m)
	{
		long long a[n], div[m] = {0};
		for(long long i = 0; i < n; i++)
		{
			cin >> a[i];
			div[a[i] % m]++;
		}

		long long maxi = 0;
		for(long long i = 1; i < m; i++)
		{
			if(div[i] > div[maxi]) maxi = i;
		}
	/*	for(int i = 0; i < m; i++) cout << div[i] << " ";
		cout << endl;*/

		if(div[maxi] < k)
		{
			cout << "No" << endl;
		}
		else
		{
			cout << "Yes" << endl;
			for(long long i = 0, count = 0; i < n; i++)
			{

				if(a[i] % m == maxi)
				{
					cout << a[i];
					count++;
					if( count < k) cout << " ";
				 }

				if(count == k) break;
			}
			cout << endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define repi(i, a, b) for(int i=(a); i>(b); i--)
#define db(x) (cerr << #x << ": " << (x) << '\n')
#define sync ios_base::sync_with_stdio(false), cin.tie(NULL)
#define tests(t) int t; cin >> t; while(t--)
#define iceil(n, x) (((n) + (x) - 1) / (x))
#define ll long long
#define gcd __gcd
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define sz size()
#define all(v) (v).begin(), (v).end()
#define uni(v) sort(all(v)); (v).erase(unique(all(v)), (v).end());
#define pii pair<int, int>
#define vi vector<int>
#define vpii vector<pii>
#define vvi vector<vi>
#define fi first
#define se second
#define umap unordered_map
#define uset unordered_set
#define pqueue priority_queue
#define si(a) scanf("%d", &a)
#define sll(a) scanf("%lld", &a)
#define bitcount(x) __builtin_popcount(x)
#define cps CLOCKS_PER_SEC
#define PI acos(-1.0)
#define EPS 1e-9
#define mod 1000000007
#define MOD 1000000007
#define N 100005
using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}

//#define trace(...) 42

template<typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;

template<typename T>
using maxpq = priority_queue<T>;

//All indexing is 0-based
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update> ordered_set;
//methods: find_by_order(k); & order_of_key(k);
//To make it an ordered_multiset, use pairs of (value, time_of_insertion)
//to distinguish values which are similar.

//Returns no. of values x for which ceil(n / x) == y (y must be > 1).
inline ll CC(ll n, ll y) { return iceil(n, y-1) - iceil(n, y); }

//Returns no. of values x for which floor(n / x) == y
inline ll FF(ll n, ll y) { return n / y - n / (y+1); }

//a and b are assumed to be taken modulo p
inline int add(int a, int b, int p = mod){ int c = a + b; if(c >= p) c -= p; return c;}
inline int sub(int a, int b, int p = mod){ int c = a - b; if(c < 0) c += p; return c;}
inline int mul(int a, int b, int p = mod){ return (a * 1ll * b) % p;}

#define int ll

int a[N];
vi v[N];

main()
{
    #ifndef ONLINE_JUDGE
        freopen("/home/tarun/Desktop/input.txt", "r", stdin);
    //  freopen("/home/tarun/Desktop/output.txt", "w", stdout);
    #endif
    sync;
    clock_t clk = clock();
    cerr << "I will return...\n";

    int n, k, m; cin >> n >> k >> m;
    rep(i, 0, n) cin >> a[i];

    bool ok = 0, cur = -1;
    rep(i, 0, n) {
        v[a[i]%m].pb(a[i]);
        //trace(v[a[i]%m].sz);
        if(v[a[i]%m].sz >= k) {
            ok = 1;
            cur = a[i]%m; break;
        }
    }

    if(!ok) cout << "No\n";
    else {
        cout << "Yes\n";
        for(int i : v[cur]) cout << i << ' ';
    }

    cerr << "...don't you ever hang your head.\n";
    cerr << "Time (in ms): " << double(clock() - clk) * 1000.0 / cps << '\n';
}

//Compile using:
//g++ -o filename.exe --std=c++11 filename.cpp
//Use -D CP for defining a macro CP in the local environment


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define repi(i, a, b) for(int i=(a); i>(b); i--)
#define db(x) (cerr << #x << ": " << (x) << '\n')
#define sync ios_base::sync_with_stdio(false), cin.tie(NULL)
#define tests(t) int t; cin >> t; while(t--)
#define iceil(n, x) (((n) + (x) - 1) / (x))
#define ll long long
#define gcd __gcd
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define sz size()
#define all(v) (v).begin(), (v).end()
#define uni(v) sort(all(v)); (v).erase(unique(all(v)), (v).end());
#define pii pair<int, int>
#define vi vector<int>
#define vpii vector<pii>
#define vvi vector<vi>
#define fi first
#define se second
#define umap unordered_map
#define uset unordered_set
#define pqueue priority_queue
#define si(a) scanf("%d", &a)
#define sll(a) scanf("%lld", &a)
#define bitcount(x) __builtin_popcount(x)
#define cps CLOCKS_PER_SEC
#define PI acos(-1.0)
#define EPS 1e-9
#define mod 1000000007
#define MOD 1000000007
#define N 100005
using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}

//#define trace(...) 42

template<typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;

template<typename T>
using maxpq = priority_queue<T>;

//All indexing is 0-based
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update> ordered_set;
//methods: find_by_order(k); & order_of_key(k);
//To make it an ordered_multiset, use pairs of (value, time_of_insertion)
//to distinguish values which are similar.

//Returns no. of values x for which ceil(n / x) == y (y must be > 1).
inline ll CC(ll n, ll y) { return iceil(n, y-1) - iceil(n, y); }

//Returns no. of values x for which floor(n / x) == y
inline ll FF(ll n, ll y) { return n / y - n / (y+1); }

//a and b are assumed to be taken modulo p
inline int add(int a, int b, int p = mod){ int c = a + b; if(c >= p) c -= p; return c;}
inline int sub(int a, int b, int p = mod){ int c = a - b; if(c < 0) c += p; return c;}
inline int mul(int a, int b, int p = mod){ return (a * 1ll * b) % p;}

#define int ll

int a[N];
vi v[N];

main()
{
    #ifndef ONLINE_JUDGE
        freopen("/home/tarun/Desktop/input.txt", "r", stdin);
    //  freopen("/home/tarun/Desktop/output.txt", "w", stdout);
    #endif
    sync;
    clock_t clk = clock();
    cerr << "I will return...\n";

    int n, k, m; cin >> n >> k >> m;
    rep(i, 0, n) cin >> a[i];

    bool ok = 0, cur = -1;
    rep(i, 0, n) {
        v[a[i]%m].pb(a[i]);
        //trace(v[a[i]%m].sz);
        if(v[a[i]%m].sz >= k) {
            ok = 1;
            cur = i; break;
        }
    }

    if(!ok) cout << "No\n";
    else {
        cout << "Yes\n";
        for(int i : v[cur]) cout << i << ' ';
    }

    cerr << "...don't you ever hang your head.\n";
    cerr << "Time (in ms): " << double(clock() - clk) * 1000.0 / cps << '\n';
}

//Compile using:
//g++ -o filename.exe --std=c++11 filename.cpp
//Use -D CP for defining a macro CP in the local environment


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define repi(i, a, b) for(int i=(a); i>(b); i--)
#define db(x) (cerr << #x << ": " << (x) << '\n')
#define sync ios_base::sync_with_stdio(false), cin.tie(NULL)
#define tests(t) int t; cin >> t; while(t--)
#define iceil(n, x) (((n) + (x) - 1) / (x))
#define ll long long
#define gcd __gcd
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define sz size()
#define all(v) (v).begin(), (v).end()
#define uni(v) sort(all(v)); (v).erase(unique(all(v)), (v).end());
#define pii pair<int, int>
#define vi vector<int>
#define vpii vector<pii>
#define vvi vector<vi>
#define fi first
#define se second
#define umap unordered_map
#define uset unordered_set
#define pqueue priority_queue
#define si(a) scanf("%d", &a)
#define sll(a) scanf("%lld", &a)
#define bitcount(x) __builtin_popcount(x)
#define cps CLOCKS_PER_SEC
#define PI acos(-1.0)
#define EPS 1e-9
#define mod 1000000007
#define MOD 1000000007
#define N 100005
using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}

//#define trace(...) 42

template<typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;

template<typename T>
using maxpq = priority_queue<T>;

//All indexing is 0-based
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update> ordered_set;
//methods: find_by_order(k); & order_of_key(k);
//To make it an ordered_multiset, use pairs of (value, time_of_insertion)
//to distinguish values which are similar.

//Returns no. of values x for which ceil(n / x) == y (y must be > 1).
inline ll CC(ll n, ll y) { return iceil(n, y-1) - iceil(n, y); }

//Returns no. of values x for which floor(n / x) == y
inline ll FF(ll n, ll y) { return n / y - n / (y+1); }

//a and b are assumed to be taken modulo p
inline int add(int a, int b, int p = mod){ int c = a + b; if(c >= p) c -= p; return c;}
inline int sub(int a, int b, int p = mod){ int c = a - b; if(c < 0) c += p; return c;}
inline int mul(int a, int b, int p = mod){ return (a * 1ll * b) % p;}

#define int ll

int a[N];
vi v[N];

main()
{
    #ifndef ONLINE_JUDGE
        freopen("/home/tarun/Desktop/input.txt", "r", stdin);
    //  freopen("/home/tarun/Desktop/output.txt", "w", stdout);
    #endif
    sync;
    clock_t clk = clock();
    cerr << "I will return...\n";

    int n, k, m; cin >> n >> k >> m;
    rep(i, 0, n) cin >> a[i];

    bool ok = 0;
    int cur = -1;
    rep(i, 0, n) {
        v[a[i]%m].pb(a[i]);
       // trace(v[a[i]%m].sz);
        if(v[a[i]%m].sz >= k) {
            ok = 1;
            //trace(a[i]%m);
            cur = a[i]%m; break;
        }
    }

    //trace(cur);
    if(!ok) cout << "No\n";
    else {
        cout << "Yes\n";
        //trace(v[cur].sz);
        for(int i : v[cur]) cout << i << ' ';
    }

    cerr << "...don't you ever hang your head.\n";
    cerr << "Time (in ms): " << double(clock() - clk) * 1000.0 / cps << '\n';
}

//Compile using:
//g++ -o filename.exe --std=c++11 filename.cpp
//Use -D CP for defining a macro CP in the local environment


#include<bits/stdc++.h>
#define pb push_back
#define ll long long
using namespace std;
vector <ll> v[100001];
int main()
{
	ll n,k,m;
	cin>>n>>k>>m;
	for(ll i=0;i<n;i++)
	{
		ll g;
		cin>>g;
		v[g%m].pb(g);
	}
	for(ll i=0;i<m;i++)
	{
		if(v[i].size()>=k)
		{
			cout<<"Yes"<<'\n';
			for(ll j=0;j<k;j++)
			{
				cout<<v[i][j]<<" ";
			}
			return 0;
		}
	}
	cout<<"No";
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define ll long long
using namespace std;
ll b[100001];
vector <int> v[10001];
int main()
{
	ll n,k,m;
	cin>>n>>k>>m;
	for(ll i=0;i<n;i++)
	{
		ll g;
		cin>>g;
		v[g%m].pb(g);
		b[g%m]++;
	}
	for(ll i=0;i<m;i++)
	{
		if(v[i].size()>=k)
		{
			cout<<"YES"<<'\n';
			for(ll j=0;j<k;j++)
			{
				cout<<v[i][j]<<" ";
			}
			return 0;
		}
	}
	cout<<"NO";
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define ll long long
using namespace std;
ll b[100001];
vector <ll> v[10001];
int main()
{
	ll n,k,m;
	cin>>n>>k>>m;
	for(ll i=0;i<n;i++)
	{
		ll g;
		cin>>g;
		v[g%m].pb(g);
		b[g%m]++;
	}
	for(ll i=0;i<m;i++)
	{
		if(v[i].size()>=k)
		{
			cout<<"Yes"<<'\n';
			for(ll j=0;j<k;j++)
			{
				cout<<v[i][j]<<" ";
			}
			return 0;
		}
	}
	cout<<"No";
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define ll long long
using namespace std;
ll b[100001];
vector <int> v[10001];
int main()
{
	ll n,k,m;
	cin>>n>>k>>m;
	for(ll i=0;i<n;i++)
	{
		ll g;
		cin>>g;
		v[g%m].pb(g);
		b[g%m]++;
	}
	for(ll i=0;i<m;i++)
	{
		if(v[i].size()>=k)
		{
			cout<<"Yes"<<'\n';
			for(ll j=0;j<k;j++)
			{
				cout<<v[i][j]<<" ";
			}
			return 0;
		}
	}
	cout<<"No";
	return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main()

{
    int n,k,m,i;
    cin>>n>>k>>m;
    int num[n];
    for(i=0;i<n;i++)
    {
        cin>>num[i];
    }
    sort(num,num+n);

    int count,j,a,b;

    for(i=n-1;i>=0;i--)
    {
        int ans[k]={};
        count=1;
        if(i<k)
            break;
        for(a=n-2,j=0;a>=0;a--)
        {
            if((num[i]-num[a])%m==0 && a!=i)
            {

                ans[j]=num[a];
                count++;
                j++;

                if(count==k)
                {
                    ans[j]=num[i];
                    sort(ans,ans+k);
                    cout<<"Yes"<<endl;
                    for(b=0;b<k;b++)
                    cout<<ans[b]<<endl;
                    return 0;
                }
            }
        }
    }

    cout<<"No"<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main()

{
    int n,k,m,i;
    cin>>n>>k>>m;
    int num[n];
    for(i=0;i<n;i++)
    {
        cin>>num[i];
    }
    sort(num,num+n);

    int count,j,a,b;

    for(i=0;i<n;i++)
    {
        int ans[k]={};
        count=1;
        for(a=i+1,j=0;a<n;a++)
        {
            if((num[a]-num[i])%m==0 && a!=i)
            {

                ans[j]=num[a];
                count++;
                j++;
                if(count==k)
                {
                    ans[j]=num[i];
                    sort(ans,ans+k);
                    cout<<"Yes"<<endl;
                    for(b=0;b<k;b++)
                    cout<<ans[b]<<endl;
                    return 0;
                }
            }
        }
        if(i>=k && count<=(n-k))
            break;
    }

    cout<<"No"<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main()

{
    int n,k,m,i;
    cin>>n>>k>>m;
    int num[n];
    for(i=0;i<n;i++)
    {
        cin>>num[i];
    }
    sort(num,num+n);

    int count,j,a,b;

    for(i=0;i<n;i++)
    {

        int ans[k]={};

        count=1;

        for(a=i+1,j=0;a<n;a++)
        {
            if((num[a]-num[i])%m==0 && a!=i)
            {

                ans[j]=num[a];
                count++;
                j++;

                if(count==k)
                {
                    ans[j]=num[i];
                    sort(ans,ans+k);
                    cout<<"Yes"<<endl;
                    for(b=0;b<k;b++)
                    cout<<ans[b]<<endl;
                    return 0;
                }
            }
        }
        if(count<=k/2)
            break;
    }

    cout<<"No"<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main()

{
    int n,k,m,i;
    cin>>n>>k>>m;
    int num[n];
    for(i=0;i<n;i++)
    {
        cin>>num[i];
    }
    sort(num,num+n);

    int count=1,j,a;

    for(i=0,j=0;i<n;i++)
    {
        int ans[k]={};
        for(a=1;a<n;a++)
        {
            if((num[a]-num[i])%m==0 && a!=i)
            {
                ans[j]=num[a];
                count++;
                j++;
                if(count==k)
                {
                    ans[j]=num[i];
                    sort(ans,ans+k);
                    cout<<"YES"<<endl;
                    for(i=0;i<k;i++)
                    cout<<ans[i]<<endl;
                    return 0;
                }
            }
        }
    }

    cout<<"NO"<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main()

{
    int n,k,m,i;
    cin>>n>>k>>m;
    int num[n];
    for(i=0;i<n;i++)
    {
        cin>>num[i];
    }
    sort(num,num+n);

    int count,j,a,b,c,d=0;

    for(c=1;c<n;c++)
    {
        if((num[c]-num[c-1])>=m || (num[c]-num[c-1])==0)
            d++;
    }
    if(d>0)
    {
        for(i=0;i<n;i++)
    {
        int ans[k]={};
        count=1;
        for(a=i+1,j=0;a<n;a++)
        {
            if((num[a]-num[i])%m==0 && a!=i)
            {

                ans[j]=num[a];
                count++;
                j++;
                if(count==k)
                {
                    ans[j]=num[i];
                    sort(ans,ans+k);
                    cout<<"Yes"<<endl;
                    for(b=0;b<k;b++)
                    cout<<ans[b]<<endl;
                    return 0;
                }
            }
        }
    }
    }

    cout<<"No"<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main()

{
    int n,k,m,i;
    cin>>n>>k>>m;
    int num[n];
    for(i=0;i<n;i++)
    {
        cin>>num[i];
    }
    sort(num,num+n);

    int count,j,a,b;

    for(i=0;i<n;i++)
    {
        int ans[k]={};
        count=1;
        for(a=i+1,j=0;a<n;a++)
        {
            if((num[a]-num[i])%m==0 && a!=i)
            {

                ans[j]=num[a];
                count++;
                j++;
                if(count==k)
                {
                    ans[j]=num[i];
                    sort(ans,ans+k);
                    cout<<"Yes"<<endl;
                    for(b=0;b<k;b++)
                    cout<<ans[b]<<endl;
                    return 0;
                }
            }
        }
        if(i>=(n-k) && count<k)
            break;
    }

    cout<<"No"<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main()

{
    int n,k,m,i;
    cin>>n>>k>>m;
    int num[n];
    for(i=0;i<n;i++)
    {
        cin>>num[i];
    }
    sort(num,num+n);

    int count=1,j,a,b;

    for(i=0;i<n;i++)
    {
        int ans[k]={};
        for(a=1,j=0;a<n;a++)
        {
            if((num[a]-num[i])%m==0 && a!=i)
            {
                ans[j]=num[a];
                count++;
                j++;
                if(count==k)
                {
                    ans[j]=num[i];
                    sort(ans,ans+k);
                    cout<<"Yes"<<endl;
                    for(b=0;b<k;b++)
                    cout<<ans[b]<<endl;
                    return 0;
                }
            }
        }
    }

    cout<<"No"<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main()

{
    int n,k,m,i;
    cin>>n>>k>>m;
    int num[n];
    for(i=0;i<n;i++)
    {
        cin>>num[i];
    }
    sort(num,num+n);

    int count,j,a,b,c,d=0;

    for(c=1;c<n;c++)
    {
        if((num[c]-num[c-1])>=m)
            d++;
    }
    if(d>0)
    {
        for(i=0;i<n;i++)
    {
        int ans[k]={};
        count=1;
        for(a=i+1,j=0;a<n;a++)
        {
            if((num[a]-num[i])%m==0 && a!=i)
            {

                ans[j]=num[a];
                count++;
                j++;
                if(count==k)
                {
                    ans[j]=num[i];
                    sort(ans,ans+k);
                    cout<<"Yes"<<endl;
                    for(b=0;b<k;b++)
                    cout<<ans[b]<<endl;
                    return 0;
                }
            }
        }
    }
    }

    cout<<"No"<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main()

{
    int n,k,m,i;
    cin>>n>>k>>m;
    int num[n];
    for(i=0;i<n;i++)
    {
        cin>>num[i];
    }
    sort(num,num+n);

    int count,j,a,b;

    for(i=0;i<n;i++)
    {
        int ans[k]={};
        count=1;
        for(a=i+1,j=0;a<n;a++)
        {
            if((num[a]-num[i])%m==0 && a!=i)
            {
                ans[j]=num[a];
                count++;
                j++;
                if(count==k)
                {
                    ans[j]=num[i];
                    sort(ans,ans+k);
                    cout<<"Yes"<<endl;
                    for(b=0;b<k;b++)
                    cout<<ans[b]<<endl;
                    return 0;
                }
            }
        }
    }

    cout<<"No"<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main()

{
    int n,k,m,i;
    cin>>n>>k>>m;
    int num[n];
    for(i=0;i<n;i++)
    {
        cin>>num[i];
    }
    sort(num,num+n);

    int count=1,j,a;

    for(i=0,j=0;i<n;i++)
    {
        int ans[k]={};
        for(a=1;a<n;a++)
        {
            if((num[a]-num[i])%m==0 && a!=i)
            {
                ans[j]=num[a];
                count++;
                j++;
                if(count==k)
                {
                    ans[j]=num[i];
                    sort(ans,ans+k);
                    cout<<"Yes"<<endl;
                    for(i=0;i<k;i++)
                    cout<<ans[i]<<endl;
                    return 0;
                }
            }
        }
    }

    cout<<"No"<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main()

{
    int n,k,m,i;
    cin>>n>>k>>m;
    int num[n];
    for(i=0;i<n;i++)
    {
        cin>>num[i];
    }
    sort(num,num+n);

    int count,j,a,b;

    for(i=n-1;i>=0;i--)
    {
        int ans[k]={};
        count=1;
        if(i<k-1)
            break;
        for(a=n-2,j=0;a>=0;a--)
        {
            if((num[i]-num[a])%m==0 && a!=i)
            {

                ans[j]=num[a];
                count++;
                j++;

                if(count==k)
                {
                    ans[j]=num[i];
                    sort(ans,ans+k);
                    cout<<"Yes"<<endl;
                    for(b=0;b<k;b++)
                    cout<<ans[b]<<endl;
                    return 0;
                }
            }
        }
    }

    cout<<"No"<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main()

{
    int n,k,m,i;
    cin>>n>>k>>m;
    int num[n];
    for(i=0;i<n;i++)
    {
        cin>>num[i];
    }
    sort(num,num+n);

    int count,j,a,b,c,d=0;

    for(c=1;c<n;c++)
    {
        if((num[c]-num[c-1])>=m || (num[c]-num[c-1])==0)
            d++;
    }
    if(d>0)
    {
        for(i=0;i<n;i++)
    {
        int ans[k]={};
        count=1;
        for(a=i+1,j=0;a<n;a++)
        {
            if((num[a]-num[i])%m==0 && a!=i)
            {

                ans[j]=num[a];
                count++;
                j++;
                if(count==k)
                {
                    ans[j]=num[i];
                    sort(ans,ans+k);
                    cout<<"Yes"<<endl;
                    for(b=0;b<k;b++)
                    cout<<ans[b]<<endl;
                    return 0;
                }
            }
        }
        if(n==k && count<k)
            break;
    }
    }

    cout<<"No"<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main()

{
    int n,k,m,i;
    cin>>n>>k>>m;
    int num[n];
    for(i=0;i<n;i++)
    {
        cin>>num[i];
    }
    sort(num,num+n);

    int count,j,a,b;

    for(i=0;i<n;i++)
    {
        int ans[k]={};
        count=1;
        for(a=i+1,j=0;a<n;a++)
        {
            if((num[a]-num[i])%m==0 && a!=i)
            {

                ans[j]=num[a];
                count++;
                j++;
                if(count==k)
                {
                    ans[j]=num[i];
                    sort(ans,ans+k);
                    cout<<"Yes"<<endl;
                    for(b=0;b<k;b++)
                    cout<<ans[b]<<endl;
                    return 0;
                }
            }
        }
        if(count<(n-k))
            break;
    }

    cout<<"No"<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main()

{
    int n,k,m,i;
    cin>>n>>k>>m;
    int num[n];
    for(i=0;i<n;i++)
    {
        cin>>num[i];
    }
    sort(num,num+n);

    int count,j,a,b;

    for(i=0;i<n;i++)
    {
        int ans[k]={};
        count=1;
        for(a=i+1,j=0;a<n;a++)
        {
            if((num[a]-num[i])%m==0 && a!=i)
            {

                ans[j]=num[a];
                count++;
                j++;
                if(count==k)
                {
                    ans[j]=num[i];
                    sort(ans,ans+k);
                    cout<<"Yes"<<endl;
                    for(b=0;b<k;b++)
                    cout<<ans[b]<<endl;
                    return 0;
                }
            }
        }
        if(count<=2)
            break;
    }

    cout<<"No"<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main()

{
    int n,k,m,i;
    cin>>n>>k>>m;
    int num[n];
    for(i=0;i<n;i++)
    {
        cin>>num[i];
    }
    sort(num,num+n);

    int count,j,a,b;

    for(i=n-1;i>=0;i--)
    {
        int ans[k]={};
        count=1;
        for(a=n-2,j=0;a>=0;a--)
        {
            if((num[i]-num[a])%m==0 && a!=i)
            {

                ans[j]=num[a];
                count++;
                j++;
                if(count==k)
                {
                    ans[j]=num[i];
                    sort(ans,ans+k);
                    cout<<"Yes"<<endl;
                    for(b=0;b<k;b++)
                    cout<<ans[b]<<endl;
                    return 0;
                }
            }
        }
        if(n>=1000 && count<=k/2)
            break;
    }

    cout<<"No"<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main()

{
    int n,k,m,i;
    cin>>n>>k>>m;
    int num[n];
    for(i=0;i<n;i++)
    {
        cin>>num[i];
    }
    sort(num,num+n);

    int count,j,a,b,c;
    for(i=1;i<n;i++)
    {
        if((num[i]-num[i-1])>=m)
        {
            c=i-1;
            break;
        }
    }

    for(i=c;i<n;i++)
    {
        int ans[k]={};
        count=1;
        for(a=i+1,j=0;a<n;a++)
        {
            if((num[a]-num[i])%m==0 && a!=i)
            {
                ans[j]=num[a];
                count++;
                j++;
                if(count==k)
                {
                    ans[j]=num[i];
                    sort(ans,ans+k);
                    cout<<"Yes"<<endl;
                    for(b=0;b<k;b++)
                    cout<<ans[b]<<endl;
                    return 0;
                }
            }
        }
    }

    cout<<"No"<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main()

{
    int n,k,m,i;
    cin>>n>>k>>m;
    int num[n];
    for(i=0;i<n;i++)
    {
        cin>>num[i];
    }
    sort(num,num+n);

    int count,j,a,b;

    for(i=0;i<n;i++)
    {
        int ans[k]={};
        count=1;
        for(a=i+1,j=0;a<n;a++)
        {
            if((num[a]-num[i])%m==0 && a!=i)
            {

                ans[j]=num[a];
                count++;
                j++;
                if(count==k)
                {
                    ans[j]=num[i];
                    sort(ans,ans+k);
                    cout<<"Yes"<<endl;
                    for(b=0;b<k;b++)
                    cout<<ans[b]<<endl;
                    return 0;
                }
            }
        }
        if(count<=(n-k))
            break;
    }

    cout<<"No"<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main()

{
    int n,k,m,i;
    cin>>n>>k>>m;
    int num[n];
    for(i=0;i<n;i++)
    {
        cin>>num[i];
    }
    sort(num,num+n);

    int count,j,a,b;

    for(i=n-1;i>=0;i--)
    {
        int ans[k]={};
        count=1;
        for(a=n-2,j=0;a>=0;a--)
        {
            if((num[i]-num[a])%m==0 && a!=i)
            {

                ans[j]=num[a];
                count++;
                j++;
                if(count==k)
                {
                    ans[j]=num[i];
                    sort(ans,ans+k);
                    cout<<"Yes"<<endl;
                    for(b=0;b<k;b++)
                    cout<<ans[b]<<endl;
                    return 0;
                }
            }
        }
    }

    cout<<"No"<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main()

{
    int n,k,m,i;
    cin>>n>>k>>m;
    int num[n];
    for(i=0;i<n;i++)
    {
        cin>>num[i];
    }
    sort(num,num+n);

    int count=1,j,a,b;

    for(i=0;i<n;i++)
    {
        int ans[k]={};
        for(a=i+1,j=0;a<n;a++)
        {
            if((num[a]-num[i])%m==0 && a!=i)
            {
                ans[j]=num[a];
                count++;
                j++;
                if(count==k)
                {
                    ans[j]=num[i];
                    sort(ans,ans+k);
                    cout<<"Yes"<<endl;
                    for(b=0;b<k;b++)
                    cout<<ans[b]<<endl;
                    return 0;
                }
            }
        }
    }

    cout<<"No"<<endl;
    return 0;
}