problems / 151

Distanced Round Table

medium combinatorics math number-theory 1000 ms · 256 MB

Distanced Round Table

Alice runs a small tea house whose round tables have their chairs bolted to the floor. A table with n chairs has them numbered 1, 2, …, n clockwise. Chair i is next to chairs i − 1 and i + 1, and because the table is round, chair n is also next to chair 1. No other pairs of chairs are next to each other.

Guests have recently insisted on keeping their distance: a group of k guests will only sit down if no two of them end up in chairs that are next to each other. Each guest takes exactly one chair, and the remaining chairs stay empty.

Alice does not care who sits where, only which chairs are taken. Two seatings are therefore different exactly when the sets of occupied chairs are different. Since the chairs are numbered, two seatings that are rotations of each other still count as different (on a table with 4 chairs, taking chairs {1, 3} and taking chairs {2, 4} are two different seatings).

Alice asks you about several tables. For each one, count the seatings of k guests at a table of n chairs in which no two guests are next to each other. The numbers can be huge, so print them modulo 1 000 000 007.

Input

The first line contains q (1 ≤ q ≤ 10^5), the number of questions.

Each of the next q lines contains two integers n and k (2 ≤ n ≤ 10^6, 0 ≤ k ≤ n): the number of chairs at the table and the number of guests.

Output

For each question, print on its own line the number of valid seatings modulo 1 000 000 007. If the guests cannot be seated at all, print 0. Seating zero guests counts as exactly one seating (every chair empty).

Notes

In the first example, a table of 5 chairs can host 2 guests in 5 ways: {1, 3}, {1, 4}, {2, 4}, {2, 5}, {3, 5}. The pair {1, 5} is not allowed because chairs 5 and 1 are next to each other. With 6 chairs and 3 guests only {1, 3, 5} and {2, 4, 6} work, and 4 guests never fit around 7 chairs.

With n = 2, the two chairs are next to each other, so one guest has 2 seatings and two guests have none.

Example 1

Input

6
5 2
6 3
6 2
4 1
7 4
5 0

Output

5
2
9
4
0
1

Example 2

Input

7
10 3
2 1
3 1
3 2
1000000 1
8 4
8 3

Output

50
2
3
0
1000000
2
16

Hints · reveal one at a time if you're stuck

Hint 1

Start with a straight row of chairs and ask how many ways k non-adjacent guests can sit in it. Then consider chair 1 on the circle: is it taken or empty, and what does each case leave behind?

Hint 2

Count the two cases, or find a single expression for the total, and handle the trivial cases (no guests, or guests who cannot fit) before you divide anything. With up to 10^5 questions, precompute factorials and inverse factorials once so each question takes constant time.

report an issue Prefer your own editor? sooks prep 151 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.