#P10375. [AHOI2024 初中组 / 科大国创杯初中组 2024] 计数

[AHOI2024 初中组 / 科大国创杯初中组 2024] 计数

Background

The testdata for this problem is not official.

Official testdata (that can be uploaded here) is being collected.

Official testdata (that cannot be uploaded here): https://www.luogu.com.cn/training/499869

Unofficial testdata download: https://scg3.piaoztsdy.cn/training/69bce235a7cf509104565c83

Problem Description

Xiao Keke had a dream. In the dream, there are nn candies from left to right. Each candy has a color, represented by a positive integer in [1,m][1, m].

Each time, Xiao Keke chooses two candies with the same color, and eats them as well as all candies between them.

Xiao Keke remembers that for the candy sequence in the dream, there exists a way to eat all candies.

After waking up, Xiao Keke forgot what the candy sequence in the dream was. Can you help her count, among all mnm^n possible candy sequences, how many sequences could have appeared in her dream (that is, there exists a way to eat them all)?

Since the answer may be very large, you only need to output it modulo 109+710^9+7.

Input Format

One line with two positive integers n,mn, m.

Output Format

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

3 2
4
6 3
405
30 2
73741759
100 100
566607183

Hint

Explanation for Sample 1

There are 44 valid candy sequences in total: [1,1,1][1,1,1], [1,2,1][1,2,1], [2,1,2][2,1,2], [2,2,2][2,2,2].

Constraints

For 10%10\% of the testdata, n6n \le 6, m4m \le 4.

For 20%20\% of the testdata, n6n \le 6, m100m \le 100.

For another 30%30\% of the testdata, n50n \le 50, m2m \le 2.

For 70%70\% of the testdata, n,m100n, m \le 100.

For 80%80\% of the testdata, n,m1000n, m \le 1000.

For 100%100\% of the testdata, 1n30001 \le n \le 3000, 1m1091 \le m \le 10^9.

Translated by ChatGPT 5