G-Avoiding Sequence

No attempts yetTime limit1sMemory limit128 MB

Problem

You are given a set SS of distinct integers and an integer GG. A sequence of integers is called a G-Avoiding Sequence if both of the following conditions hold:

  1. the sequence is a permutation of the elements of SS; and
  2. for any two consecutive elements AA and BB in the sequence, ABA - B is not divisible by GG.

Compute the number of G-Avoiding Sequences modulo the prime 1,234,567,891.

Input

The input consists of several test cases. The first line of each test case contains two integers NN (1N2001 \le N \le 200), the size of SS, and GG (1G10001 \le G \le 1000). The next line contains NN integers, the elements of SS, each between 00 and 10610^6.

The input ends with a single line containing N=G=0N = G = 0, which must not be processed.

Output

For each test case, output a single line containing the number of G-Avoiding Sequences modulo 1,234,567,891.