#P5598. 【XR-4】混乱度

【XR-4】混乱度

Description

Xiao X has balls of nn different colors. For the ii-th color, there are aia_i balls. Balls of the same color are indistinguishable. Define the disorder degree f(l,r)f(l, r) for colors lrl \sim r as follows: line up all balls of colors lrl \sim r in a row, and let f(l,r)f(l, r) be the total number of distinct arrangements modulo pp. Xiao X wants you to help compute the value of:

l=1nr=lnf(l,r)\sum_{l=1}^n \sum_{r=l}^n f(l, r)

Input Format

The first line contains two integers n,pn, p.

The second line contains nn integers aia_i.

Output Format

Output one integer in one line, representing the answer.

2 2
1 2

3

4 7
1 2 8 9

28

15 5
1 5 26 1 0 5 0 6 7 51 1 5 26 1 0

124

Hint

[Sample 1 Explanation]

f(1,1)=1mod2=1f(1,1) = 1 \bmod 2 = 1 f(1,2)=3mod2=1f(1,2) = 3 \bmod 2 = 1 f(2,2)=1mod2=1f(2,2) = 1 \bmod 2 = 1 l=1nr=lnf(l,r)=3\sum_{l=1}^n \sum_{r=l}^n f(l, r) = 3

This problem uses bundled testdata.

  • Subtask 1 (31 points): 1n5×1051 \le n \le 5 \times 10^5, aia_i is uniformly random in [0,105][0, 10^5], time limit 1.5 s1.5 \text{ s}.
  • Subtask 2 (32 points): 1n5×1041 \le n \le 5 \times 10^4, time limit 5 s5 \text{ s}.
  • Subtask 3 (37 points): no special constraints, time limit 2.5 s2.5 \text{ s}.

For 100%100\% of the data, 1n5×1051 \le n \le 5 \times 10^5, 0ai10180 \le a_i \le 10^{18}, p{2,3,5,7}p \in \{2,3,5,7\}.

Translated by ChatGPT 5