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: Wrong Answer)

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#include <iomanip>
#include <complex>
#include <bitset>
#include <stack>
using namespace std;
 
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define repl(i,s,n) for(int i=s; i<=n; ++i)
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define ktya(x) sort(all(x))
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 1e18
#define INFLL 1000000000000000007LL
#define SIZE 200105
#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mind(a,b) (a>b?b:a)
#define maxd(a,b) (a>b?a:b)
#define PI (acos(-1))
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
typedef pair<int,ll> pill; typedef pair<ll,int> plli; 
typedef pair<double, int> pdi;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }
typedef complex<double> P;
ll MOD = 1000000007;
//ll MOD=998244353;
typedef ll Weight;
struct Edge {
	int src, dst;
	Weight weight;
};
bool operator < (const Edge &e, const Edge &f) {
	return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
	e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
int N;
ll X,Y;
vector<pair<int, ll> > data2;
struct UnionFind {
	vector<int> data;
	void init(int n) { data.assign(n, -1); }
	bool unionSet(int x, int y) {
		x = root(x); y = root(y);
		if(x != y) {
			if(data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool findSet(int x, int y) { return root(x) == root(y); }
	int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
	int size(int x) { return -data[root(x)]; }
}uf;
ll bik[214514];
ll bikinv[214514];
ll gcd(ll a, ll b) {
	return b != 0 ? gcd(b, a % b) : a;
}
ll lcm(ll a, ll b) {
	return a * b / gcd(a, b);
}
// a x + b y = gcd(a, b)
ll extgcd(ll a, ll b, ll &x, ll &y) {
	ll g = a; x = 1; y = 0;
	if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
	return g;
}
ll invMod(ll a, ll m) {
	ll x, y;
	if (extgcd(a, m, x, y) == 1) return (x + m) % m;
	else                         return 0; // unsolvable
}
void bikcal(int NN){
	ll hoge=(ll)1;
	bik[0]=(ll)1;
	bikinv[0]=(ll)1;
	for(int i=1;i<=NN;i++){
		hoge=(hoge*i)%MOD;
		bik[i]=hoge;
		bikinv[i]=invMod(bik[i],MOD);
	}
}


ll Comb(int a,int b){
	ll ret;
	ret=(((bik[a]*bikinv[b])%MOD)*bikinv[a-b])%MOD;
	return ret;
}


ll powMod(ll x, ll k, ll m) {//x^k (mod n)
	if (k == 0)     return 1;
	if (k % 2 == 0) return powMod(x*x % m, k/2, m);
	else            return x*powMod(x, k-1, m) % m;
}
bool used[214514];
int minmemo[214514];
int main(){
	cin>>N>>X>>Y;
	uf.init(N);
	bikcal(N+1);
	rep(i,N)used[i]=false;
	rep(i,N)minmemo[i]=300000;
	rep(i,N){
		ll c,w;
		cin>>c>>w;c--;
		data2.pb(mp(w,c));
	}
	sort(all(data2));
	ll cos0=data2[0].first;
	int col0=data2[0].second;
	int col2memo=N+100;
	rep(i,N){
		ll cos=data2[i].first;
		int col=data2[i].second;
		if(i==0){
			used[col]=true;
			minmemo[col]=0;
		}else if(!used[col]){
			used[col]=true;
			minmemo[col]=i;
			col2memo=min(col2memo,i);
			if(cos+cos0<=Y){
				uf.unionSet(0,i);
			}
		}else{
			int me=minmemo[col];
			ll cos2=data2[me].first;
			if(cos+cos2<=X){
				uf.unionSet(me,i);
			}
			if(col==col0){
				if(col2memo<N&&cos+data2[col2memo].first<=Y)uf.unionSet(col2memo,i);
				
			}else{
				if(cos+cos0<=Y)uf.unionSet(0,i);
			}
		}
	}
	
	
	vi roots[214514];
	rep(i,N){
		roots[uf.root(i)].pb(i);
	}
	/*cout<<endl;
	rep(i,N){
		cout<<data2[i].first<<" "<<data2[i].second<<endl;
	}*/
	/*rep(i,N){
		rep(j,roots[i].size()){
			cout<<roots[i][j]<<" ";
		}
		cout<<endl;
	}*/
	ll ans=1;
	rep(i,N){
		if(uf.root(i)==i){
			
			map<int,int> cnts;
			rep(j,roots[i].size()){
				int now=roots[i][j];
				cnts[data2[now].second]++;
				//cout<<data2[now].second<<" ";
			}
			ll temp=bik[roots[i].size()];
			for(auto i=cnts.begin();i!=cnts.end();++i){
				temp*=bikinv[i->second];
				temp%=MOD;
				//cout<<(i->first)<<" "<<(i->second)<<endl;
			}
			ans*=temp;
			ans%=MOD;
			//cout<<ans<<endl;
		}
	}
	cout<<ans<<endl;
}