P18121 [ICPC 2026 Seoul R] Chemistry Experiment
Description
The International Chemical Protection Council (ICPC) enforces strict safety regulations in chemical laboratories: direct contact between different chemicals may trigger hazardous reactions, so a neutralizing agent must always be placed between them.
A laboratory has a row of $n$ containment compartments, indexed from $1$ to $n$. Each compartment must be filled with one of the following:
- One of $k$ distinct chemical substances, labeled with integers from $1$ to $k$.
- A neutralizing agent, labeled with the integer $0$.
In this experiment, all $k$ chemical substances must be utilized. Therefore, for every integer $x$ from $1$ to $k$, the chemical substance $x$ must be placed in at least one compartment.
Let $A_i$ denote the label of the chemical substance in compartment $i$, or $0$ if the compartment contains the neutralizing agent. The resulting sequence is $A = (A_1, A_2, \ldots, A_n)$.
According to ICPC safety regulations, two adjacent compartments cannot contain different nonzero chemical substances. Formally, for every $1 \le i < n$, if $A_i \ne 0$ and $A_{i+1} \ne 0$, then $A_i = A_{i+1}$.
Given the integers $n$, $k$, and $m$, find the number of valid sequences $A$ that satisfy all the conditions above, modulo $m$.
Input Format
Your program is to read from standard input. The first and only line of the input contains three space-separated integers $n$, $k$, and $m$ ($1 \le k \le 5 \times 10^5$; $k \le n \le 10^9$; $10^8 < m < 10^9$; $m$ is a prime number).
Output Format
Your program is to write to standard output. Print exactly one line. The line should contain the number of valid sequences modulo $m$.