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: Accepted)

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
using Enu = System.Linq.Enumerable;

public class Program
{
    static readonly int Mod = (int)1e9 + 7;

    public void Solve()
    {
        int N = Reader.Int(), X = Reader.Int(), Y = Reader.Int();
        var dic = new Dictionary<int, List<int>>();
        int[] C = new int[N], W = new int[N];
        for (int i = 0; i < N; i++)
        {
            C[i] = Reader.Int(); W[i] = Reader.Int();
            AddDic(dic, C[i], i);
        }
        if (dic.Count == 1) { Console.WriteLine(1); return; }

        var mins = new List<int>();
        var uf = new UnionFind(N);
        foreach (var A in dic.Values)
        {
            int minW = int.MaxValue, who = 0;
            foreach (int i in A)
                if (CheckMin(ref minW, W[i])) who = i;
            mins.Add(who);
            foreach (int i in A)
                if (minW + W[i] <= X) uf.Unite(who, i);
        }
        mins = mins.OrderBy(i => W[i]).ToList();
        foreach (var A in dic.Values)
            for (int mi = 0; mi < 2; mi++)
                if (C[mins[mi]] != C[A[0]])
                    foreach (int i in A)
                        if (W[mins[mi]] + W[i] <= Y) uf.Unite(mins[mi], i);
        long ans = 1;
        modC = new ModC(N, Mod);
        foreach (var A in uf.Components().Values)
        {
            var count = new Dictionary<int, List<int>>();
            foreach (int i in A) AddDic(count, C[i], 1);
            ans *= Count(count.Values.Select(v => v.Count).ToArray());
            ans %= Mod;
        }

        Console.WriteLine(ans);
    }


    ModC modC;
    long Count(int[] count)
    {
        int sum = count.Sum();
        long res = 1;
        foreach (int num in count)
        {
            res = res * modC[sum, num] % Mod;
            sum -= num;
        }
        return res;
    }

    bool CheckMin(ref int a, int b) { if (b < a) { a = b; return true; } return false; }

    void AddDic(Dictionary<int, List<int>> dic, int key, int val)
    {
        if (!dic.ContainsKey(key)) dic[key] = new List<int>();
        dic[key].Add(val);
    }
}

class ModC
{
    readonly long P;
    readonly long[] Fact, InvFact;

    public int this[int n, int k]
    {
        get
        {
            if (n < 0 || k < 0 || k > n) return 0;
            return (int)(Fact[n] * InvFact[k] % P * InvFact[n - k] % P);
        }
    }
    public ModC(int maxN, int p) // maxN < p, p is prime
    {
        P = p;
        Fact = new long[maxN + 1];
        InvFact = new long[maxN + 1];
        Fact[0] = 1;
        for (long i = 1; i <= maxN; i++)
            Fact[i] = Fact[i - 1] * i % P;
        InvFact[maxN] = ModPower(Fact[maxN], p - 2, p);
        for (int i = maxN - 1; i >= 0; i--)
            InvFact[i] = InvFact[i + 1] * (i + 1) % p;
    }
    long ModPower(long x, long n, long mod) // x ^ n
    {
        long res = 1;
        if ((x %= mod) < 0) x += mod;
        for (; n > 0; n >>= 1, x = x * x % mod)
            if ((n & 1) == 1) res = res * x % mod;
        return res;
    }
}

class UnionFind
{
    private int[] parent;
    private byte[] rank;

    public UnionFind(int N)
    {
        parent = new int[N];
        rank = new byte[N];
        for (int i = 0; i < N; i++) parent[i] = -1;
    }
    public int Root(int x)
    {
        if (parent[x] < 0) return x;
        return parent[x] = Root(parent[x]);
    }
    public bool Same(int x, int y)
    {
        return Root(x) == Root(y);
    }
    public int Count(int x)
    {
        return -parent[Root(x)];
    }
    public bool Unite(int x, int y)
    {
        x = Root(x); y = Root(y);
        if (x == y) return false;
        if (rank[x] > rank[y]) { var t = x; x = y; y = t; }
        if (rank[x] == rank[y]) rank[y]++;
        parent[y] += parent[x];
        parent[x] = y;
        return true;
    }
    public Dictionary<int, List<int>> Components()
    {
        var dic = new Dictionary<int, List<int>>();
        for (int i = 0; i < parent.Length; i++)
        {
            int root = Root(i);
            if (!dic.ContainsKey(root)) dic[root] = new List<int>();
            dic[root].Add(i);
        }
        return dic;
    }
}



class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    static TextReader reader = Console.In;
    static readonly char[] separator = { ' ' };
    static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    static string[] A = new string[0];
    static int i;
    static void Init() { Dispose(); A = new string[0]; }
    public static void Set(TextReader r) { Init(); reader = r; }
    public static void Set(string file) { Init(); reader = new StreamReader(file); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Range(N, Int); }
    public static int[][] IntTable(int H) { return Range(H, IntLine); }
    public static string[] StringArray(int N) { return Range(N, Next); }
    public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); }
    public static string Line() { return reader.ReadLine().Trim(); }
    public static T[] Range<T>(int N, Func<T> f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; }
    public static void Dispose() { reader.Dispose(); }
    static string[] Split(string s) { return s.Split(separator, op); }
    static string Next() { CheckNext(); return A[i++]; }
    static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}