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: Runtime Error)

#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;
#pragma warning (disable: 4996)

long long fact[200009], mod = 1000000007, n, c[200009], w[200009], A, B; vector<pair<long long, long long>>X;
vector<int>x[200009]; long long col[200009], cnt, num[200009]; vector<int>Z, u[200009];

long long modpow(long long a, long long b, long long m) {
	long long p = 1, q = a;
	for (int i = 0; i < 35; i++) {
		if ((b&(1LL << i)) != 0) { p *= q; p %= m; }
		q *= q; q %= m;
	}
	return p;
}
long long Div(long long a, long long b, long long m) {
	return (a*modpow(b, m - 2, m)) % m;
}
void dfs(int pos) {
	if (col[pos] >= 1) return;
	col[pos] = cnt; Z.push_back(c[pos]); num[c[pos]]++;
	for (int i = 0; i < x[pos].size(); i++) dfs(x[pos][i]);
}

void edges() {
	long long P = c[0], PP = 1000000;
	for (int i = 1; i < n; i++) {
		if (c[i] != P && w[0] + w[i] <= B) {
			if (PP == 1000000)PP = i; x[0].push_back(i); x[i].push_back(0);
		}
	}
	for (int i = 0; i < n; i++) {
		if (c[i] != c[PP] && w[PP] + w[i] <= B) { x[PP].push_back(i); x[i].push_back(PP); }
	}
	for (int i = 0; i < n; i++) u[c[i]].push_back(i);
	for (int i = 0; i <= n; i++) {
		for (int j = 1; j < (int)u[i].size(); j++) {
			if (w[u[i][j]] + w[u[i][0]] > A)continue;
			x[u[i][j]].push_back(u[i][0]);
			x[u[i][0]].push_back(u[i][j]);
		}
	}
}

int main() {
	fact[0] = 1; for (int i = 1; i <= 200008; i++) fact[i] = (1LL * fact[i - 1] * i) % mod;
	cin >> n >> A >> B;
	for (int i = 0; i < n; i++) {
		scanf("%lld%lld", &c[i], &w[i]);
		X.push_back(make_pair(w[i], c[i]));
	}
	sort(X.begin(), X.end());
	for (int i = 0; i < n; i++) { c[i] = X[i].second; w[i] = X[i].first; }

	edges();

	long long ans = 1;
	for (int i = 0; i < n; i++) {
		if (col[i] >= 1)continue;
		for (int j = 0; j < Z.size(); j++)num[Z[j]] = 0; Z.clear();
		cnt++; dfs(i);
		long long ret = fact[Z.size()];
		sort(Z.begin(), Z.end());
		for (int j = 0; j < Z.size(); j++) {
			if (j == 0 || Z[j - 1] != Z[j])ret = Div(ret, fact[num[Z[j]]], mod);
		}
		ans *= ret; ans %= mod;
	}
	cout << ans << endl;
	return 0;
}