C - Product Modulo 解説 /

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 800

問題文

P を素数 200\,003 とします。N 個の整数 A_1, A_2, \ldots, A_N が与えられるので、N \cdot (N-1) / 2 個すべての非順序対 (A_i, A_j) (i < j) に対する ((A_i \cdot A_j) \bmod P) の和を求めてください。

和を P で割った余りを求めるのではないことに注意してください。

制約

  • 2 \leq N \leq 200\,000
  • 0 \leq A_i < P = 200\,003
  • 入力中のすべての値は整数である。

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 A_2 \cdots A_N

出力

一つの整数、すなわち ((A_i \cdot A_j) \bmod P) の和を出力せよ。


入力例 1

4
2019 0 2020 200002

出力例 1

474287

0 でない積は以下の通りです。

  • 2019 \cdot 2020 \bmod P = 78320
  • 2019 \cdot 200002 \bmod P = 197984
  • 2020 \cdot 200002 \bmod P = 197983

よって、答えは 0 + 78320 + 197984 + 0 + 0 + 197983 = 474287 となります。


入力例 2

5
1 1 2 2 100000

出力例 2

600013

Score : 800 points

Problem Statement

Let’s take a prime P = 200\,003. You are given N integers A_1, A_2, \ldots, A_N. Find the sum of ((A_i \cdot A_j) \bmod P) over all N \cdot (N-1) / 2 unordered pairs of elements (i < j).

Please note that the sum isn't computed modulo P.

Constraints

  • 2 \leq N \leq 200\,000
  • 0 \leq A_i < P = 200\,003
  • All values in input are integers.

Input

Input is given from Standard Input in the following format.

N
A_1 A_2 \cdots A_N

Output

Print one integer — the sum over ((A_i \cdot A_j) \bmod P).


Sample Input 1

4
2019 0 2020 200002

Sample Output 1

474287

The non-zero products are:

  • 2019 \cdot 2020 \bmod P = 78320
  • 2019 \cdot 200002 \bmod P = 197984
  • 2020 \cdot 200002 \bmod P = 197983

So the answer is 0 + 78320 + 197984 + 0 + 0 + 197983 = 474287.


Sample Input 2

5
1 1 2 2 100000

Sample Output 2

600013