#P1316. Mivik 写书

    ID: 5911 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>字符串枚举,暴力容斥快速数论变换 NTT

Mivik 写书

Description

Mivik’s keyboard has mm different keys, corresponding to mm different characters. Since Mivik cannot write articles, he randomly presses nn keys with equal probability, producing an article.

Mivik defines the complexity of an article as the number of all non-empty essentially different substrings in the article. We consider two strings essentially different if and only if their lengths are different, or they differ at any character position. For example, the complexity of the article abaa is 8, because it has 8 non-empty essentially different substrings: a, b, ab, ba, aa, aba, baa, abaa.

Now Mivik wants to know the expected complexity of this article. Since Mivik does not like strange-looking decimals, you only need to output the expected complexity modulo 109+710^9+7.

Input Format

One line with two integers nn and mm, as described above.

Output Format

One line with one integer, representing the answer modulo 109+710^9+7.

2 2
500000006
3 3
5
3 4
250000007

Hint

Sample Explanation

Sample 1: Suppose the characters on the keyboard are a and b. Then there are four possible articles: aa, ab, ba, bb. Their complexities are 2, 3, 3, 2 respectively, so the answer is 2+3+3+24=52\frac{2+3+3+2}{4}=\frac{5}{2}, which modulo 109+710^9+7 equals 500000006.

Constraints

For all data, 1n201\le n\le 20, 1m51061\le m\le 5\cdot 10^6.

Subtask 1 (10 pts): 1n,m71\le n, m\le 7.

Subtask 2 (20 pts): 1n51\le n\le 5.

Subtask 3 (20 pts): 1n101\le n\le 10.

Subtask 4 (50 pts): No additional constraints.

Translated by ChatGPT 5