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 (GCC 5.4.1) (judge verdict: Runtime Error)

#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<inttypes.h>

typedef int32_t i32;
typedef int64_t i64;

typedef struct quick_find{
  i32 *id;
  i32 **elem;
  i32 *size;
  i32 *max_size;
  i32 num;
} quick_find;

void init_quick_find (quick_find * const q) {
  for (i32 i = 0; i < q->num; ++i) {
    q->id[i] = i;
    free (q->elem[i]);
    q->elem[i] = (i32 *) calloc (1, sizeof (i32));
    q->elem[i][0] = i;
    q->size[i] = 1;
    q->max_size[i] = 1;
  }
}

quick_find* new_quick_find (const i32 n) {
  quick_find *q = (quick_find *) calloc (1, sizeof (quick_find));
  q->id = (i32 *) calloc (3 * n, sizeof (i32));
  q->elem = (i32 **) calloc (n, sizeof (i32 *));
  q->size = q->id + n;
  q->max_size = q->size + n;
  q->num = n;
  init_quick_find (q);
  return q;
}

void free_quick_find (quick_find * const q) {
  free (q->id);
  for (i32 i = 0; i < q->num; ++i) {
    free (q->elem[i]);
  }
  free (q);
}

i32 get_id (const quick_find * const q, const i32 x) {
  return q->id[x];
}

i32 get_size (const quick_find * const q, const i32 x) {
  return q->size[get_id (q, x)];
}

int same (const quick_find * const q, const i32 x, const i32 y) {
  return get_id (q, x) == get_id (q, y);
}

void unite (quick_find * const q, i32 x, i32 y) {
  x = get_id (q, x);
  y = get_id (q, y);
  if (x == y) {
    return;
  }
  if (q->size[x] < q->size[y]) {
    const i32 s = x;
    x = y;
    y = s;
  }
  if (q->max_size[x] < q->size[x] + q->size[y]) {
    q->max_size[x] *= 2;
    q->elem[x] = realloc (q->elem[x], sizeof (i32) * q->max_size[x]);
  }
  for (i32 i = 0; i < q->size[y]; ++i) {
    const i32 v = q->elem[y][i];
    q->id[v] = x;
    q->elem[x][q->size[x]++] = v;
  }
  free (q->elem[y]);
  q->elem[y] = NULL;
  q->size[y] = 0;
}

const i32 mod = 1000000007;

i32 inv (i32 a) {
  i32 t = 1;
  while (a > 1) {
    t = (i64) t * (mod - mod / a) % mod;
    a = mod % a;
  }
  return t;
}

i32 *fact = NULL;
i32 *iFact = NULL;
void init_fact (const i32 n) {
  fact = (i32 *) calloc (n + 1, sizeof (i32));
  fact[0] = 1;
  for (i32 i = 1; i <= n; ++i) {
    fact[i] = (i64) i * fact[i - 1] % mod;
  }
  iFact = (i32 *) calloc (n + 1, sizeof (i32));
  iFact[n] = inv (fact[n]);
  for (i32 i = n - 1; i >= 0; --i) {
    iFact[i] = (i64) (i + 1) * iFact[i + 1] % mod;
  }
}

#define MIN(a,b) ((a) < (b) ? (a) : (b))

typedef struct node {
  i32 c, w, index;
} node;

int cmp_node_weight (const void *a, const void *b) {
  i32 d = ((node *)a)->w - ((node *)b)->w;
  return d == 0 ? 0 : d < 0 ? -1 : 1;
}

int cmp_node_color_weight (const void *a, const void *b) {
  i32 d = ((node *)a)->c - ((node *)b)->c;
  if (d != 0) return d < 0 ? -1 : 1;
  return cmp_node_weight (a, b);
}

int cmp_node_index (const void *a, const void *b) {
  i32 d = ((node *)a)->index - ((node *)b)->index;
  return d == 0 ? 0 : d < 0 ? -1 : 1;
}

void run (void) {
  i32 n, x, y;
  scanf ("%" SCNi32 "%" SCNi32 "%" SCNi32, &n, &x, &y);
  node *a = (node *) calloc (n, sizeof (node));
  for (i32 i = 0; i < n; ++i) {
    i32 c, w;
    scanf ("%" SCNi32 "%" SCNi32, &c, &w);
    a[i] = (node) {c, w, i};
  }
  quick_find *q = new_quick_find (n);
  qsort (a, n, sizeof (node), cmp_node_color_weight);
  for (i32 i = 0; i < n; ++i) {
    if (i > 0 && a[i].c == a[i - 1].c) continue;
    for (i32 j = i + 1; j < n && a[i].c == a[j].c && a[i].w + a[j].w <= x; ++j) {
      unite (q, a[i].index, a[j].index);
    }
  }
  qsort (a, n, sizeof (node), cmp_node_weight);
  for (i32 i = 1; i < n && a[0].w + a[i].w <= y; ++i) {
    if (a[0].c == a[i].c) continue;
    unite (q, a[0].index, a[i].index);
  }
  for (i32 k = 1; k < n; ++k) {
    if (a[0].c == a[k].c) continue;
    for (i32 j = 0; j < n && a[k].w + a[j].w <= y; ++j) {
      if (a[k].c == a[j].c) continue;
      unite (q, a[k].index, a[j].index);
    }
    k = n;
  }
  qsort (a, n, sizeof (node), cmp_node_index);
  init_fact (n);
  i32 ans = 1;
  i32 *cnt = (i32 *) calloc (n, sizeof (i32));
  for (i32 i = 0; i < n; ++i) {
    if (q->size[i] <= 1) continue;
    ans = (i64) ans * fact[q->size[i]] % mod;
    for (i32 j = 0; j < q->size[i]; ++j) {
      i32 k = q->elem[i][j];
      cnt[a[k].c]++;
    }
    for (i32 j = 0; j < q->size[i]; ++j) {
      i32 k = q->elem[i][j];
      ans = (i64) ans * iFact[cnt[a[k].c]] % mod;
      cnt[a[k].c] = 0;
    }
  }
  printf ("%" PRIi32 "\n", ans);
}

int main (void) {
  run();
  return 0;
}