409183: GYM103451 C Krosh and paths

Memory Limit:256 MB Time Limit:4 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

C. Krosh and pathstime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Krosh has a graph of $$$n$$$ vertexes. For any vertex $$$1 \le i \le n - 2$$$ there are oriented edges $$$(i, i + 1)$$$ and $$$(i, i + 2)$$$, for vertex $$$n - 1$$$ there is oriented edge $$$(n - 1, n)$$$. Every vertex has it's value - $$$a_i$$$. Krosh considers paths from vertex $$$1$$$ to vertex $$$n$$$. Cost of the path is the maximum among all the values of the vertexes Krosh has visited. Help Krosh to calculate sum of costs over all the paths from $$$1$$$ to $$$n$$$. Output answer modulo $$$m$$$ where $$$m$$$ is given(can be non-prime).

Input

In first line you are given two numbers $$$1 \le n \le 2 * 10^5$$$ and $$$10 \le m \le 10^9$$$ - number of vertexes and modulo.In next line you are given $$$n$$$ positive integres $$$1 \le a_i \le 10^9$$$ values of vertexes.

Output

Output answer modulo m.

ExamplesInput
4 1000
2 3 5 3
Output
13
Input
1 1000000000
1000000000
Output
0
Input
5 583723
1000000000 1000000000 1000000000 1000000000 1000000000
Output
412505

加入题单

算法标签: