Combinations

Given up to 1000 pairs (n, k), compute the binomial coefficient C(n, k) modulo 10^9+7 for each pair.

Medium4CombinatoricsMathNumber theoryDynamic programmingInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

Taking kk elements out of a set of nn elements gives a kk-combination.

For the set of the numbers from 1 to 5, the combinations are these:

  • 1-combinations (1 element at a time): (1), (2), (3), (4), (5)
  • 2-combinations (2 elements at a time): (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)
  • 3-combinations (3 elements at a time): (1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5), (1, 4, 5), (2, 3, 4), (2, 3, 5), (2, 4, 5), (3, 4, 5)
  • 4-combinations (4 elements at a time): (1, 2, 3, 4), (1, 2, 3, 5), (1, 2, 4, 5), (1, 3, 4, 5), (2, 3, 4, 5)
  • 5-combination (all elements at once): (1, 2, 3, 4, 5)
  • 0-combination (no element): ()

The number of kk-combinations of a set of nn elements comes from this formula:

(nk)=n(n1)(nk+1)k(k1)1\binom{n}{k} = \frac{n(n-1)\cdots(n-k+1)}{k(k-1)\cdots 1}

The list above gives (50)=1\binom{5}{0} = 1, (51)=5\binom{5}{1} = 5, (52)=10\binom{5}{2} = 10, (53)=10\binom{5}{3} = 10, (54)=5\binom{5}{4} = 5, (55)=1\binom{5}{5} = 1.

Given several (n,k)(n, k) pairs, compute (nk)\binom{n}{k} for each one.

Input

The first line has an integer tt. Each of the next tt lines has two integers nn and kk separated by a space.

  • 1t10001 \le t \le 1000
  • 1n10001 \le n \le 1000
  • 0kn0 \le k \le n

Output

For each (n,k)(n, k) pair, print the number of kk-combinations of a set of nn elements modulo 10000000071000000007 (109+710^9 + 7), one per line, in the order the pairs are given.