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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
#define N 400005
#define ll long long
#define P 1000000007
using namespace std;
int n,X,Y,col[N],inv[N],fac[N],ifac[N],ans,sum,vis[N],b[N];
int to[N*2],nxt[N*2],l,fst[N];
struct T
{
	int id,w;
};
bool operator<(T a,T b)
{
	return a.w<b.w;
}
vector<T>a[N];
void link(int x,int y)
{
	to[++l]=y;nxt[l]=fst[x];fst[x]=l;
	to[++l]=x;nxt[l]=fst[y];fst[y]=l;
}
void dfs(int x)
{
	sum++;
	vis[x]=1;b[col[x]]++;
	for (int i=fst[x];i;i=nxt[i])
		if (!vis[to[i]])
			dfs(to[i]);
}
void solve(int x)
{
	vis[x]=2;
	ans=(ll)ans*ifac[b[col[x]]]%P;b[col[x]]=0;
	for (int i=fst[x];i;i=nxt[i])
		if (vis[to[i]]==1)
			solve(to[i]);
}
int main()
{
	scanf("%d%d%d",&n,&X,&Y);
	inv[1]=fac[1]=ifac[1]=fac[0]=ifac[0]=1;
	for (int i=2;i<=n;i++)
	{
		inv[i]=(ll)(P-P/i)*inv[P%i]%P;
		fac[i]=(ll)fac[i-1]*i%P;
		ifac[i]=(ll)ifac[i-1]*inv[i]%P;
	}
	for (int i=1;i<=n;i++)
	{
		int w;
		scanf("%d%d",&col[i],&w);
		a[col[i]].push_back((T){i,w});
	}
	int Mn1=0,Mn2=0;
	for (int i=1;i<=n;i++)
	if (a[i].size())
	{
		sort(a[i].begin(),a[i].end());
		for (int j=1;j<a[i].size();j++)
			if (a[i][j].w+a[i][0].w<=X) link(a[i][j].id,a[i][0].id);
		if (!Mn1||a[Mn1][0].w>a[i][0].w)Mn2=Mn1,Mn1=i;
		else if (!Mn2||a[Mn2][0].w>a[i][0].w)Mn2=i;
	}
	for (int i=1;i<=n;i++)
	if (a[i].size())
	{
		for (int j=0;j<a[i].size();j++)
			if (i!=Mn1)
			{
				if (a[i][j].w+a[Mn1][0].w<=Y) link(a[i][j].id,a[Mn1][0].id);
			}
			else if (Mn2)
			{
				if (a[i][j].w+a[Mn2][0].w<=Y) link(a[i][j].id,a[Mn2][0].id);
			}
	}
	//puts("ok");
	ans=1;
	for (int i=1;i<=n;i++)
		if (!vis[i])
		{
			sum=0;
			dfs(i);
			ans=(ll)ans*fac[sum]%P;
			solve(i);
		}
	printf("%d\n",ans);
}