Problem:
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

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <bits/stdc++.h> 
using namespace std; 
int gi() { 
	int res = 0, w = 1; 
	char ch = getchar(); 
	while (ch != '-' && !isdigit(ch)) ch = getchar(); 
	if (ch == '-') w = -1, ch = getchar(); 
	while (isdigit(ch)) res = res * 10 + ch - '0', ch = getchar(); 
	return res * w; 
} 
const long double PI = acos(-1.0); 
const int Mod = 200003, G = 2; 
const int MAX_N = 2e5 + 5; 
struct comp { long double x, y; } f[MAX_N << 2]; 
comp operator + (const comp &l, const comp &r) { return (comp){l.x + r.x, l.y + r.y}; } 
comp operator - (const comp &l, const comp &r) { return (comp){l.x - r.x, l.y - r.y}; } 
comp operator * (const comp &l, const comp &r) { return (comp){l.x * r.x - l.y * r.y, l.x * r.y + l.y * r.x}; } 
int Limit, rev[MAX_N << 2]; 
void FFT_prepare(int len) { 
	int p = 0; 
	for (Limit = 1; Limit <= len; Limit <<= 1) ++p; 
	for (int i = 1; i < Limit; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (p - 1)); 
} 
void FFT(comp p[], int op) { 
	for (int i = 1; i < Limit; i++) if (i < rev[i]) swap(p[i], p[rev[i]]); 
	for (int i = 1; i < Limit; i <<= 1) { 
		comp rot = (comp){cos(PI / i), op * sin(PI / i)}; 
		for (int j = 0; j < Limit; j += i << 1) { 
			comp w = (comp){1, 0}; 
			for (int k = 0; k < i; k++, w = w * rot) { 
				comp x = p[j + k], y = w * p[i + j + k]; 
				p[j + k] = x + y, p[i + j + k] = x - y; 
			} 
		} 
	} 
	if (op == -1) { 
		for (int i = 0; i < Limit; i++) p[i].x /= Limit; 
	} 
} 
int N, mp[MAX_N]; 

int main () { 
	for (int j = 1, i = 0; i < Mod - 1; i++) mp[j] = i, j = G * j % Mod; 
	N = gi(); 
	long long ans = 0; 
	for (int i = 1; i <= N; i++) { 
		int x = gi(); 
		if (x) f[mp[x]].x += 1, ans -= 1ll * x * x % Mod; 
	} 
	FFT_prepare(Mod << 1); 
	FFT(f, 1); 
	for (int i = 0; i < Limit; i++) f[i] = f[i] * f[i]; 
	FFT(f, -1); 
	for (int i = 0, v = 1; i < Mod * 2; i++) { 
		ans += 1ll * v * (long long)(f[i].x + 0.5);
		v = v * G % Mod; 
	} 
	ans /= 2; 
	printf("%lld\n", ans); 
    return 0; 
}