#P10964. [2015杭电多校]Just A String

[2015杭电多校]Just A String

Just A String

Problem Description

soda has a random string of length nn which is generated by the following algorithm: each of nn characters of the string is equiprobably chosen from the alphabet of size mm. For a string ss, if we can reorder the letters in string ss so as to get a palindrome, then we call ss a good string. soda wants to know the expected number of good substrings in the random string.

Input

There are multiple test cases. The first line of input contains an integer TT, indicating the number of test cases. For each test case: The first line contains two integers nn and mm (1n,m2000)(1 \le n, m \le 2000).

Output

For each case, if the expected number is EE, a single integer denotes Emn mod 1000000007E \cdot m^n \text{ mod } 1000000007.

Sample Input

3
2 2
3 2
10 3

Sample Output

10
40
1908021

Author

zimpha@zju

Source

2015 Multi-University Training Contest 6