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 <iostream>
#include <vector>
#include <complex>

using namespace std;

typedef complex<long double> cmpl;

const int P = 2e5 + 3;
int ep[P];
int inv[P];

void FFT(vector<cmpl> &x, bool inv)
{
    int n = x.size();

    int j = 0;
    for(int i = 0; i < n; ++i)
    {
        int pnt = n >> 1;
        if(i < j) swap(x[i], x[j]);
        while(j & pnt) j ^= pnt, pnt >>= 1; j ^= pnt;
    }

    for(int len = 1; len < n; len <<= 1)
    {
        long double ang = acos(-1) / len * (inv ? -1 : 1);
        cmpl w(cos(ang), sin(ang));
        cmpl wn = 1;

        int i = 0;
        while(i < n)
        {
            if(i & len)
            {
                x[i] *= wn;
                x[i ^ len] += x[i];
                x[i] = x[i ^ len] - x[i] * 2.0l;
                wn *= w; ++i;
            }
            else i ^= len, wn = 1;
        }
    }

    if(inv) for(auto &i : x) i /= n;
}

void conv(const vector<int> &a, const vector<int> &b, vector<int> &ret)
{
    vector<cmpl> ca(a.begin(), a.end()), cb(b.begin(), b.end());

    int sz = ca.size() + cb.size() - 1;
    int N = 1; while(N < sz) N <<= 1;
    ca.resize(N); cb.resize(N);

    FFT(ca, false); FFT(cb, false);
    for(int i = 0; i < N; ++i) ca[i] *= cb[i];
    FFT(ca, true);

    ret.resize(sz);
    for(int i = 0; i < sz; ++i) ret[i] = floor(ca[i].real() + 0.5);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n; cin >> n;
    int arr[n];
    int t = 1; for(int i = 0; i < P - 1; ++i)
    {
        ep[i] = t;
        inv[t] = i;
        t *= 2; t %= P;
    }

    vector<int> A(P - 1), B, ret;
    for(int i = 0; i < n; ++i)
    {
        int x; cin >> x;
        arr[i] = x;
        if(x) ++A[inv[x]];
    }
//    for(int i = 0; i < A.size(); ++i) if(A[i]) cout << i << ' ' << A[i] << endl;
    B = A;
    conv(A, B, ret);
    ret.push_back(0);

//    for(int i = 0; i < ret.size(); ++i) if(ret[i]) cout << i << ' ' << ret[i] << endl;

    long long ans = 0;
    for(int i = 0; i < P - 1; ++i)
    {
        ans += 1ll * ep[i] * (ret[i] + ret[i + P - 1]);
    }
    for(int i = 0; i < n; ++i)
    {
        ans -= 1ll * arr[i] * arr[i] % P;
    }
    cout << ans / 2;
    return 0;
}