#P8386. [PA 2021] Od deski do deski

[PA 2021] Od deski do deski

Problem Description

Given nn and mm, find the number of sequences of length nn that satisfy the following conditions:

  1. Each element is between [1,m][1, m].
  2. One operation is defined as deleting an interval of length at least 22 whose two endpoints are equal. The sequence must be able to be completely deleted using several operations.

Output the answer modulo 109+710^9 + 7.

Input Format

The first line contains two positive integers nn and mm.

Output Format

Output one integer, which is the result modulo 109+710^9 + 7.

4 2
10

Hint

Sample Explanation

The valid sequences are:

[1,1,1,1][1,1,1,1]

[1,1,2,1][1,1,2,1]

[1,1,2,2][1,1,2,2]

[1,2,1,1][1,2,1,1]

[1,2,2,1][1,2,2,1]

[2,1,1,2][2,1,1,2]

[2,1,2,2][2,1,2,2]

[2,2,1,1][2,2,1,1]

[2,2,1,2][2,2,1,2]

[2,2,2,2][2,2,2,2]

Constraints

1n30001 \le n \le 30001m1091 \le m \le 10^9

Translated by ChatGPT 5