QOJ.ac

QOJ

実行時間制限: 1 s メモリ制限: 512 MB 満点: 100

#14457. Defect Decoder

統計

There is a string $S$, which is initially empty. A faulty decoder is continuously outputting characters and appending them to the end of $S$. In this problem, the size of the character set is $m$, and each time the decoder outputs a new character, it is chosen uniformly at random from the $m$ characters, each with a probability of $\frac{1}{m}$.

Because the decoder is faulty, after each character is output, it checks whether the current string $S$ is stable. The decoder considers the string $S$ to be unstable if and only if $S$ is a palindrome of length at least 2, or if it can be written in the form $S = U + U$ or $S = U + v + U$, where $U$ is any non-empty string and $v$ is any single character. For example, $abcabc$, $ababa$, and $abcab$ are all unstable, but any string of length 1 is stable. If the current string $S$ is unstable, the decoder resets $S$ to an empty string.

Write a program to calculate the expected number of characters the decoder must output to obtain a stable string $S$ of length exactly $n$, or determine if it is impossible. Note that even if the string is repeatedly reset during the process, the historical count of output characters is not cleared. For example, $a \to ab \to aba \to (\text{empty}) \to a \to ab \to abc$ results in a total of 6 characters output.

Input

The input contains one line with two positive integers $n, m$ ($1 \le n \le 12, 1 \le m \le 10^9$).

Output

Output a single integer representing the expected number of characters. If it is impossible to achieve, output -1. Otherwise, assuming the answer is $\frac{p}{q}$, you need to output the smallest non-negative integer $r$ such that $q \cdot r \equiv p \pmod{10^9 + 7}$. You may assume such an $r$ always exists.

Examples

Input 1

1 26

Output 1

1

Input 2

2 1

Output 2

-1

Input 3

2 2

Output 3

4

Input 4

6 26

Output 4

420983172

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.