Equivalent Strings

4.4

8 votes
Mathematics, Dynamic Programming, Open, Approved, Hard
Problem

Nam have an alphabet of size M (i.e alphabet consists of M different characters). He will construct all string of length N by using his alphabet. Hence, he have to construct MN string in total. After construct all strings, Nam will categorize them into classes. Two strings must be in the same class if they are equivalent (see the definition below). But MN is a large number, so he can't construct all strings in 1 or 2 days. So he need your help to calculate the number of classes.

In this problem, 2 strings of the same length A and B are consider equivalent if 2 following conditions are true for all 1 <= i, j <= length of strings:

  1. (Ai != Aj) <=> (Bi != Bj)
  2. (Ai = Aj) <=> (Bi = Bj)

where Si denote the i-th (1 based-indexing) character of string S. It is easy to see that, if string A and B are equivalent, string B and C are equivalent, then string A and C are equivalent.

Input

The first line is T - the number of test cases. Then T test cases follow.

Each test is described in a single line contains 2 integers M and N.

Output

For each test cases, print the number of classes. Since this number can be large, you must print it modulo 109 + 7.

Constraints

  • 1 ≤ T ≤ 100000
  • 1 ≤ M, N ≤ 2000
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In the third test (M = 2, N = 3), there are 2^3 = 8 strings: "aaa", "aab", "aba", "abb", "baa", "bab", "bba", "bbb" (assuming that 'a' and 'b' is characters of Nam's alphabet). There are 4 classes, they are {"aaa", "bbb"}, {"aab", "bba"}, {"aba", "bab"} and {"abb", "baa"}.

Editor Image

?