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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		long mod = (long)1e9 + 7;
		long[] f = new long[N+1];
		f[0] = f[1] = 1;
		for(int i=2;i<=N;i++){
			f[i] = f[i-1] *i;
			if(f[i] >= mod) f[i] %= mod;
		}
		
		Pair[] P = new Pair[N];
		for(int i=0;i<N;i++){
			P[i] = new Pair(i,C[i],W[i]);
		}
		
		Array.Sort(P,(p,q) => p.W.CompareTo(q.W));
		
		Dictionary<int,List<int>> D = new Dictionary<int,List<int>>();
		Dictionary<int,int> Same = new Dictionary<int,int>();
		for(int i=0;i<N;i++){
			if(!D.ContainsKey(P[i].C)){
				D.Add(P[i].C,new List<int>());
				Same.Add(P[i].C,0);
			}
			D[P[i].C].Add(P[i].Idx);
			if(P[i].W + W[D[P[i].C][0]] <= X) Same[P[i].C]++;
		}
		
		
		int[] CC = D.Keys.ToArray();
		int NN = CC.Length;
		int[] WW = new int[NN];
		for(int i=0;i<NN;i++) WW[i] = W[D[CC[i]][0]];
		
		Array.Sort(WW,CC);
		long ret = 1;
		int t=0;
		for(;t<NN;t++){
			int nt = t;
//Console.WriteLine("{0} {1} {2}",t,WW[t],CC[t]);
			long way = 1;
			//Console.WriteLine(way);
			while(true){
				if(nt+1<NN && WW[nt+1]+WW[t]<=Y){
					if(nt == t) way = Same[CC[t]];
					nt++;
					int can = 0;
					while(true){
						if(can+1 < D[CC[nt]].Count && W[D[CC[nt]][can+1]] + WW[t] <= Y){
							can++;
							continue;
						}
						break;
					}
					way += Math.Max(Same[CC[nt]],can+1);
					continue;
				}
				break;
			}
			
			ret *= f[way];
			if(ret >= mod) ret %= mod;
			t = nt;
		}
		Console.WriteLine(ret);
	}
	
	class Pair{
		public int Idx,W,C;
		public Pair(int idx, int c, int w){
			Idx = idx; W = w; C = c;
		}
	}
	
	int N,X,Y;
	int[] C,W;
	public Sol(){
		var d = ria();
		N = d[0]; X = d[1]; Y = d[2];
		C = new int[N];
		W = new int[N];
		for(int i=0;i<N;i++){
			d = ria();
			C[i] = d[0]; W[i] = d[1];
		}
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}