#P6435. 「EZEC-1」数列

「EZEC-1」数列

Description

You are given a positive integer nn, and the sequence 1,2,3,,n1,2,3,\ldots,n.

For each pair of adjacent terms, take aa times the left term plus bb times the right term, then add cc. This produces a new sequence with n1n-1 terms:

$1\times a+2\times b+c,2\times a+3\times b +c,\ldots,(n-1)\times a+n\times b+c$.

Repeat the above operation on this new sequence to obtain more sequences. In the end, the final sequence has only one term. Find the value of this term modulo pp.

Input Format

One line with five non-negative integers n,a,b,c,pn,a,b,c,p.

Output Format

One integer, the answer modulo pp.

1 1 1 1 1000000007
1
4 2 3 1 1000000007
381
23 19 17 0 1000000007
323147645
233 233 233 233 1000000000
770969703

Hint

[Sample Explanation]

Sample 2:

The sequences are:

1 2 3 4
9 14 19
61 86
381

[Constraints]

Test Point ID nn\le pp\le a,ba,b\le cc\le
141\sim 4 10310^3 109+710^9+7 1010
585\sim 8 10610^6 101410^{14} 10310^3
9,109,10 10910^9 109+710^9+7 11 00
11,1211,12 10910^9
13,1413,14 101810^{18}
15,1615,16 10910^9
172017 \sim 20 101410^{14}
  • For 80%80\% of the testdata, pp is a prime.

  • For 100%100\% of the testdata, 1n10181\le n\le 10^{18}, 1p10141\le p \le 10^{14}, 1a,b1091 \le a,b\le 10^9, and 0c1090\le c \le 10^9.

Translated by ChatGPT 5