Equivalent Strings
Practice
4.4 (8 votes)
Mathematics
Dynamic programming
Open
Approved
Hard
Problem
69% Success 300 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code

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

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
Tags:
AlgorithmsOpenApprovedHard
Points:50
Tags:
Dynamic ProgrammingHardRecurrence relationrecurrence relationsAlgorithmsdpMatrix ExponentiationAdvanced
Points:50
2 votes
Tags:
HardAlgorithmsApproved