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: Wrong Answer)

#include <bits/stdc++.h>
#define LL long long
#define pb push_back
#define st first
#define nd second
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
template <class T> T read(T &a) {
    a=0;char x=getchar();bool f=0;
    for(;x<'0'||x>'9';x=getchar())f|=x=='-';
	for(;x>='0'&&x<='9';x=getchar())a=(a<<3)+(a<<1)+x-'0';
    if(f)a=-a;
    return a;
}
using namespace std;

class FFT {
    private:
        struct C {
            double r, i;
            C() {}
            C(double a, double b) {r = a, i = b;}
            C operator + (C x) {return C(r + x.r, i + x.i);}
            C operator - (C x) {return C(r - x.r, i - x.i);}
            C operator * (C x) {return C(r * x.r - i * x.i, r * x.i + i * x.r);}
        };
        const double PI = acos(-1.0);
        vector<C> a, b, w;
        vector<int> R;
        int n, L, A, B;
    public:
        void fft(vector<C> &a){
            for (int i = 0; i < n; ++i) {
                if (R[i] > i) {
                    swap(a[R[i]], a[i]);
                }
            }
            for (int t = n >> 1, d = 1; d < n; d <<= 1, t >>= 1) {
                for (int i = 0; i < n; i += (d << 1)) {
                    for (int j = 0; j < d; ++j) {
                        C tmp = w[t * j] * a[i + j + d];
                        a[i + j + d] = a[i + j] - tmp;
                        a[i + j] = a[i + j] + tmp;
                    }
                }
            }
        }
        void init(vector<int> c, vector<int> d) {
            A = c.size(), B = d.size();
            for (n = 1, L = 0; n <= A + B; n <<= 1, ++L);
            a.resize(n), b.resize(n), w.resize(n), R.resize(n);
            for (int i = 0; i < A; ++i) {
                a[i].r = c[i];
                a[i].i = 0; 
            }
            for (int i = 0; i < B; ++i) {
                b[i].r = d[i];
                b[i].i = 0;
            }
            R[0] = 0;
            for (int i = 0; i < n; ++i) {
                R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
                w[i] = C(cos(2.0 * i * PI / n), sin(2.0 * i * PI / n));
            }
        }
        vector<int> run() {
            fft(a);
            fft(b);
            for (int i = 0; i < n; i++) {
                a[i] = a[i] * b[i];
                w[i].i = -w[i].i;
            }
            fft(a);
            A--, B--;
            vector<int> ans;
            for (int i = 0;i <= A + B; ++i) {
                ans.push_back(int(a[i].r / n + 0.5));
            }
            return ans;
        }
};

const int MOD = 200003;

int a[MOD], b[MOD], mp[MOD + 5], num[MOD + 5], n;
vector<int> cnt(MOD), ans;
FFT fun;

int main() {
    for (int i = 0, j = 1; i < MOD - 1; j = (j << 1) % MOD, i++) {
        mp[i] = j;
        num[mp[i]] = i;
    }
    read(n);
    for (int i = 1; i <= n; ++i) {
        read(a[i]);
        if (a[i]) cnt[num[a[i]]]++;
    }
    fun.init(cnt, cnt);
    ans = fun.run();
    for (int i = 0; i < ans.size(); ++i) {
        b[mp[i % (MOD - 1)]] += ans[i];
    }
    for (int i = 1; i <= n; ++i) {
        b[((LL)a[i] * a[i]) % MOD]--;
    }
    LL tot = 0;
    for (int i = 1; i < MOD; ++i) {
        tot += b[i] / 2 * i;
    }
    cout << tot << endl;
	return 0;
}