Permutations and queries

2.6

5 votes
Combinatorics, Dynamic Programming, Easy, Math
Problem

Define the value of a permutation of n elements as the number of subintervals where the number of inversions do not exceed K. That is, value(P)=i=1nj=in[Ka=ij1b=i+1j[Pa>Pb]].

For example, if P=[2,3,1], and K=0, then value(P)=4 (the valid intervals are [1,1],[2,2],[3,3],[1,2]).

Now you are given Q queries. Each query you will receive n and K, and you need to calculate the sum of value of all permutations of 1n.

Input

First line contains an integer Q.

Each of the next Q lines contain two integers, n and K.

Output

Q lines, ith line contains the answer of ith query, modulo 109+7.

Constraints

1Q100000

1n500

0K250000

Sample Input
3
2 0
7 10
10 14
Sample Output
5
137228
184025758
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

In the first query, n=2 and K=0, so value([1,2])=3 and value([2,1])=2, thus the answer is 5.

Contributers:
Editor Image

?