Problem:
Score : 1000 points

Problem Statement

Snuke arranged N colorful balls in a row.
The i-th ball from the left has color c_i and weight w_i.

He can rearrange the balls by performing the following two operations any number of times, in any order:

Operation 1: Select two balls with the same color. If the total weight of these balls is at most X, swap the positions of these balls.

Operation 2: Select two balls with different colors. If the total weight of these balls is at most Y, swap the positions of these balls.

How many different sequences of colors of balls can be obtained? Find the count modulo 10^9 + 7.

Constraints

1 ≤ N ≤ 2 × 10^5

1 ≤ X, Y ≤ 10^9

1 ≤ c_i ≤ N

1 ≤ w_i ≤ 10^9

X, Y, c_i, w_i are all integers.

Input

Input is given from Standard Input in the following format:

N X Y
c_1 w_1
:
c_N w_N

Output

Print the answer.

Sample Input 1

4 7 3
3 2
4 3
2 1
4 4

Sample Output 1

2

The sequence of colors (2,4,3,4) can be obtained by swapping the positions of the first and third balls by operation 2.

It is also possible to swap the positions of the second and fourth balls by operation 1, but it does not affect the sequence of colors.

Sample Input 2

1 1 1
1 1

Sample Output 2

1

Sample Input 3

21 77 68
16 73
16 99
19 66
2 87
2 16
7 17
10 36
10 68
2 38
10 74
13 55
21 21
3 7
12 41
13 88
18 6
2 12
13 87
1 9
2 27
13 15

Sample Output 3

129729600

Solution in C++ (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
#define N 2000010
#define mod 1000000007
#define int long long
using namespace std;
struct data
{
	int c,w;
}a[N];
int fac[N],imfac[N],n,X,Y,num,sum[N],minn1,minn2,minn[N];
long long ans=1;
int fastpow(int x,int y)
{
	int sum=1;
	while(y)
	{
		if(y&1)
		{
			sum=(sum*x)%mod;
		}
		x=(x*x)%mod;
		y>>=1;
	}
	return sum;
}
int C(int n,int m)
{
	if(!m)
	{
		return 1;
	}
	if(m>n)
	{
		return 0;
	}
	return ((fac[n]*imfac[n-m])%mod*imfac[m])%mod;
}
signed main()
{
	memset(minn,127,sizeof(minn));
	scanf("%lld%lld%lld",&n,&X,&Y);
	for(int i=1;i<=n;i++)
	{
		scanf("%lld%lld",&a[i].c,&a[i].w);
		minn[a[i].c]=min(minn[a[i].c],a[i].w);
	}
	for(int i=1;i<=n;i++)
	{
		if(minn[i]<minn[minn1])
		{
			minn2=minn1;
			minn1=i;
		}else{
			if(minn[i]<minn[minn2])
			{
				minn2=i;
			}
		}
    }
	for(int i=1;i<=n;i++)
	{
		if(a[i].w+minn[a[i].c]<=X)
		{
			a[i].w=minn[a[i].c];
		}
		if(a[i].c==minn1&&minn[minn2]+a[i].w<=Y)
		{
			sum[a[i].c]++;
			num++;
		}
		if(a[i].c!=minn1&&minn[minn1]+a[i].w<=Y)
		{
			sum[a[i].c]++;
			num++;
		}
    }
	fac[0]=1;
	for(int i=1;i<=n;i++)
	{
		fac[i]=(1ll*fac[i-1]*i)%mod;
	}
	imfac[n]=fastpow(fac[n],mod-2);
	for(int i=n-1;i>=0;i--)
	{
		imfac[i]=(1ll*imfac[i+1]*(i+1))%mod;
	}
	for(int i=1;i<=n;i++)
	{
		if(sum[i])
		{
			ans=1ll*ans*C(num,sum[i])%mod;
			num-=sum[i];
		}
	}
	printf("%lld\n",ans);
	return 0;
}