Binomial Queries
You are given q queries. Each query gives two integers n and k, and asks for the number of
ways to choose k items out of n distinct items, taken modulo 1 000 000 007.
Formally, for each query output C(n, k) mod (10^9 + 7), where C(n, k) = n! / (k! · (n-k)!) is
the binomial coefficient. If k > n, there is no way to choose, so the answer is 0. If k = 0 or
k = n, the answer is 1.
Input
The first line contains a single integer q (1 ≤ q ≤ 10^5), the number of queries.
Each of the next q lines contains two integers n and k (0 ≤ k ≤ 10^6, 0 ≤ n ≤ 10^6)
describing one query. k may exceed n.
Output
Print q lines. The i-th line must contain C(n_i, k_i) mod (10^9 + 7) for the i-th query.
Notes
In the first example, C(5, 2) = 10, C(5, 0) = 1, and C(3, 5) = 0 since you cannot choose 5
items out of only 3.
With n reaching 10^6 and q reaching 10^5, building a fresh Pascal's triangle per query, or
recomputing factorials from scratch each time, is too slow — the intended solution answers every
query in O(1) after a single O(n_max) precomputation.