luogu#P8386. [PA 2021] Od deski do deski

    ID: 7737 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP2021PA(波兰)DP 套 DP

[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