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;

struct Complex {
	double a,b;
	Complex(){
		a = b = 0;
	}
	Complex(double a,double b):a(a),b(b) {}
	Complex operator +(const Complex &r) {
		return Complex(a+r.a,b+r.b);
	}
	Complex operator -(const Complex &r) {
		return Complex(a-r.a,b-r.b);
	}
	Complex operator *(const Complex &r) {
		return Complex(a*r.a-b*r.b,a*r.b+b*r.a);
	}
	Complex operator /(const double &r) {
		return Complex(a/r,b/r);
	}
	Complex conj() {
		return Complex(a,-b);
	}
}a[804040],b[804040],w1[804040],w2[804040];
int nn = 1,rev[804040],n,m;
const int mod = 200003;
const double pi = acos(-1);
inline void FFT(Complex *x,Complex *w) {
	register int i,j,k;
	for (i = 1;i < nn; i++)
		if (rev[i] > i)
			swap(x[rev[i]],x[i]);
	Complex t0,t1;
	for (i = 1;i < nn; i <<= 1)
		for (j = 0;j < nn;j += (i<<1))
			for (k = 0;k < i; k++) {
				t0 = x[j|k],t1 = x[i|j|k]*w[i|k];
				x[j|k] = t0+t1,x[i|j|k] = t0-t1;
			}
}
int rg[202020],rgcnt = 0;

inline int quick_pow(int x,int p,int mul=1) {
	for (;p;p>>=1,x=1LL*x*x%mod) if (p&1) mul=1LL*mul*x%mod; return mul;
}
inline int getRoot() {
	for (int i = 2;i < mod-1; i++)
		if ((mod-1)%i == 0)
			rg[++rgcnt] = i;
	for (int i = 2;i < mod; i++) {
		int j;
		for (j = 1;j <= rgcnt; j++)
			if (quick_pow(i,rg[j]) == 1)
				break;
	//	if (j > rgcnt)
	//		cout << i << endl;
	}
	return 0;
}
int pows[202020] = {0},t[202020];
	int aa[202020];
int main() {
	int rt = getRoot();
	rt = 5;
	pows[0] = 1;
	t[1] = 0;
	for (int i = 1;i <= mod-1; i++) {
		pows[i] = pows[i-1]*rt%mod;
		t[pows[i]] = i;
	}
	scanf("%d",&n);
	for (int i = 1;i <= n; i++) {
		scanf("%d",&aa[i]);
		if (aa[i])
		a[t[aa[i]]].a++;
	}
	int N = n;
	n = m = mod-1;
	while (nn <= n+m)
		nn <<= 1;
	int half = nn/2;
	for (int i = 1;i < nn; i++)
		rev[i] = ((rev[i>>1]>>1)|((i&1)?half:0));
	for (int i = 0;i < half; i++)
		w1[i|half] = Complex(cos(pi*i/half),sin(pi*i/half));
	for (int i = half-1;i; i--)
		w1[i] = w1[i<<1];
	for (int i = 0;i < nn; i++)
		w2[i] = w1[i].conj();
	FFT(a,w1);
	for (int i = 0;i < nn; i++)
		a[i] = a[i]*a[i];
	FFT(a,w2);
	long long ans = 0,now = 1;
	for (int i = 0;i <= n+m; i++) {
		ans += now*(long long)(a[i].a/nn+.5);
		now *= rt;
		now %= mod;
	}
	for (int i = 1;i <= n; i++)
		ans -= (long long)aa[i]*aa[i]%mod;
	ans /= 2;
	printf("%lld\n",ans);
	return 0;
}