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;
typedef long long LL;

const long double PI = acos(-1);
const LL mod = 200003;
const int N = 800010;

inline LL power(LL a, LL n, LL mod)
{
	LL Ans = 1;
	while (n)
	{
		if (n & 1) Ans = Ans * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return Ans;
}

struct cmplx
{
	long double a, b;

	cmplx() { }
	cmplx(long double _1, long double _2) : a(_1), b(_2) { }

	cmplx operator + (cmplx & x) { return cmplx(a + x.a, b + x.b); }
	cmplx operator - (cmplx & x) { return cmplx(a - x.a, b - x.b); }
	cmplx operator * (cmplx & x) { return cmplx(a*x.a - b*x.b, b*x.a + a*x.b); }
};


struct Mul
{
	int Len, Bit;
	int rev[800010];

	void getReverse()
	{
		for (int i = 0; i < Len; i++)
			rev[i] = (rev[i>>1] >> 1) | ((i&1) * (Len >> 1));
	}

	void FFT(cmplx *a, int opt)
	{
		getReverse();
		for (int i = 0; i < Len; i++)
			if (i < rev[i]) swap(a[i], a[rev[i]]);
		
		for (int i = 2; i <= Len; i <<= 1)
		{
			cmplx wn(cos(2.0*PI/i), opt * sin(2.0*PI/i));
			for (int j = 0; j < Len; j += i)
			{
				cmplx w(1.0, 0);
				for (int k = 0; k < (i>>1); k++)
				{
					cmplx x = a[j + k];
					cmplx y = w * a[j + k + (i>>1)];
					a[j + k] = x + y;
					a[j + k + (i>>1)] = x - y;
					w = w * wn;
				}
			}
		}
		
		if (opt == -1)
			for (int i = 0; i < Len; i++) a[i].a /= Len;
	}

	int getLen(int l)
	{
		Len = 1;
		for (; Len <= l; Len <<= 1);
		return Len;
	}
} Calc;

int n;
int a[N];
int pw[400010], id[400010];
cmplx tmp[N];

int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	
	pw[0] = 1;
	for (int i = 1; i < mod-1; i++)
	{
		pw[i] = 1LL * pw[i-1] * 2 % mod;
		id[pw[i]] = i;
	}
	
	int len = Calc.getLen((mod-1) * 2);
	for (int i = 1; i <= n; i++)
		if (a[i]) tmp[id[a[i]]].a += 1.0;
	Calc.FFT(tmp, 1);
	for (int i = 0; i < len; i++)
		tmp[i] = tmp[i] * tmp[i];
	Calc.FFT(tmp, -1);
	
	LL Ans = 0, x = 1;
	for (int i = 0; i < (mod-1) * 2; i++)
	{
		LL res = ((LL) (tmp[i].a + 0.5)) * x;
		x = x * 2LL % mod;
		Ans += res;
	}
	for (int i = 1; i <= n; i++) Ans -= 1LL * a[i] * a[i] % mod;
	Ans /= 2;
	cout << Ans << endl;
	
	return 0;
}