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 <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
const int MOD=1000000007;
int c[200001],w[200001],inv[200001],cnt[200001],siz,curans;
bool vst[200001];
vector<int> sc[200001],mp[200001],vv;
void dfs(int k)
{
    vv.push_back(k);
    vst[k]=true;
    curans=(long long)curans*(++siz)%MOD*inv[++cnt[c[k]]]%MOD;
    for (size_t i=0;i<mp[k].size();++i)
        if (!vst[mp[k][i]])
            dfs(mp[k][i]);
}
int main()
{
    inv[0]=inv[1]=1;
    for (int i=2;i<=200000;++i)
        inv[i]=MOD-(long long)MOD/i*inv[MOD%i]%MOD;
    int n,x,y;
    scanf("%d%d%d",&n,&x,&y);
    for (int i=1;i<=n;++i)
    {
        scanf("%d%d",c+i,w+i);
        sc[c[i]].push_back(i);
    }
    vector<int> least;
    for (int i=1;i<=n;++i)
        if (sc[i].size())
        {
            int best=sc[i][0];
            for (size_t j=1;j<sc[i].size();++j)
                if (w[sc[i][j]]<w[best])
                    best=sc[i][j];
            for (size_t j=0;j<sc[i].size();++j)
                if (sc[i][j]!=best&&w[best]+w[sc[i][j]]<=x)
                {
                    mp[best].push_back(sc[i][j]);
                    mp[sc[i][j]].push_back(best);
                }
            least.push_back(best);
        }
    int a1=least[0],a2=0;
    if (least.size()>1u)
    {
        for (size_t i=1;i<least.size();++i)
        {
            if (w[least[i]]<w[a1])
            {
                a2=a1;
                a1=least[i];
            }
            else if (!a2||w[least[i]]<w[a2])
                a2=least[i];
        }
    }
    for (int i=1;i<=n;++i)
        if (i!=a1&&c[i]!=c[a1]&&w[i]+w[a1]<=y)
        {
            mp[i].push_back(a1);
            mp[a1].push_back(i);
        }
    if (a2)
        for (int i=1;i<=n;++i)
            if (i!=a1&&i!=a2&&c[i]!=c[a2]&&w[i]+w[a2]<=y)
            {
                mp[i].push_back(a2);
                mp[a2].push_back(i);
            }
    int ans=1;
    for (int i=1;i<=n;++i)
        if (!vst[i])
        {
            vv.clear();
            curans=1;
            siz=0;
            dfs(i);
            ans=(long long)ans*curans%MOD;
            for (size_t j=0;j<vv.size();++j)
                --cnt[c[vv[j]]];
        }
    printf("%d\n",ans);
    return 0;
}