#P5431. 【模板】模意义下的乘法逆元 2

【模板】模意义下的乘法逆元 2

Description

Given nn positive integers aia_i, find their multiplicative inverses modulo pp.

Since outputting all inverses would be too much, a constant kk is given. The answer you need to output is:

i=1nkiai\sum\limits_{i=1}^n\frac{k^i}{a_i}

Output the answer modulo pp.

Input Format

The first line contains three positive integers n,p,kn, p, k, with the meaning as described in the statement.
The second line contains nn positive integers aia_i, the numbers whose inverses you need to compute.

Output Format

Output one integer in one line, representing the answer.

6 233 42
1 4 2 8 5 7
91

Hint

For 30%30\% of the testdata, 1n1051 \le n \le 10^5.

For 100%100\% of the testdata, 1n5×1061 \le n \le 5\times 10^6, 2k<p1092 \le k < p \le 10^9, 1ai<p1 \le a_i < p, and pp is guaranteed to be prime.

Note: The time limit for this problem is relatively strict. Please use faster I/O methods.

Translated by ChatGPT 5