PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000132. Tổng các phần dư đoạn con (Mod Sigma Problem)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng các phần dư đoạn con (Mod Sigma Problem)

Mod Sigma Problem

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given nn nonnegative integers A1,…,AnA_1,\ldots,A_n and a positive integer MM. For every nonempty contiguous subarray [l,r][l,r], take its sum modulo MM. Compute

$$S=\sum_{1\le l\le r\le n}\left(\left(\sum_{i=l}^{r}A_i\right)\bmod M\right).$$

The modulo is applied inside each subarray term. Do not reduce the outer sum modulo MM. The final answer may be greater than MM.

Input

  • First line: n,Mn,M.
  • Second line: nn nonnegative values AiA_i.

Output

Print the integer SS without taking modulo on the outer sum.

Subtasks

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40; 1≤M≤2000001\le M\le200000; 0≤Ai≤1090\le A_i\le10^9.
  • Subtask 2 (30 points): 1≤n≤40001\le n\le 4000; 1≤M≤2000001\le M\le200000; 0≤Ai≤1090\le A_i\le10^9.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; 1≤M≤2000001\le M\le200000; 0≤Ai≤1090\le A_i\le10^9.

Examples

Example 1

Input

3 4
2 5 0

Output

10

Explanation

The six subarrays give remainders 2,3,3,1,1,02,3,3,1,1,0, respectively. Their sum is 10, not 10 mod 410\bmod4.

Example 2

Input

10 100
320 578 244 604 145 839 156 857 556 400

Output

2736

Explanation

There are 10⋅11/2=5510\cdot11/2=55 nonempty subarrays. Sum each subarray modulo 100, then add these 55 remainders. The final answer is 2736, not 36.