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)

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <bits/stdc++.h>

using namespace std;

const int MOD = 200003;

struct Complex {
    double x, y;
    Complex(double _x = 0.0, double _y = 0.0) {
        x = _x; y = _y;
    }
    Complex operator -(const Complex& b)const {
        return Complex(x - b.x, y - b.y);
    }
    Complex operator +(const Complex& b)const {
        return Complex(x + b.x, y + b.y);
    }
    Complex operator *(const Complex& b)const {
        return Complex(x * b.x - y * b.y, x * b.y + y * b.x);
    }
};

void change(Complex y[], int len) {
    int i, j, k;
    for (i = 1, j = len / 2; i < len - 1; i++) {
        if (i < j) swap(y[i], y[j]);
        k = len / 2;
        while (j >= k) {
            j -= k;
            k /= 2;
        }
        if (j < k) j += k;
    }
}

void fft(Complex y[], int len, int on) {
    change(y, len);
    const double pi = acos(-1.0);
    for (int h = 2; h <= len; h <<= 1) {
        Complex wn(cos(-on * 2 * pi / h), sin(-on * 2 * pi / h));
        for (int j = 0; j < len; j += h) {
            Complex w(1, 0);
            for (int k = j; k < j + h / 2; k++) {
                Complex u = y[k];
                Complex t = w * y[k + h / 2];
                y[k] = u + t;
                y[k + h / 2] = u - t;
                w = w * wn;
            }
        }
    }
    if (on == -1) {
        for (int i = 0; i < len; i++) y[i].x /= len;
    }
}

int vals[MOD];
int idx[MOD];

void find_radix() {
    for (int radix = 2; radix < MOD; ++radix) {
        int ok = 1;
        vals[0] = 1;
        for (int i = 1; i < MOD - 1; i++) {
            vals[i] = (long long)vals[i - 1] * radix % MOD;
            if (vals[i] == 1) {
                ok = 0;
                break;
            }
        }
        if (!ok) continue;
        for (int i = 0; i < MOD - 1; i++) {
            idx[vals[i]] = i;
        }
        break;
    }
}

const int L = (1 << 20);
Complex x[L];

int main()
{
#ifdef _MSC_VER
    freopen("c.in", "r", stdin);
#endif
    std::ios::sync_with_stdio(false);
    find_radix();
    int n;
    cin >> n;
    long long self = 0;
    for (int i = 0; i < n; i++) {
        int val;
        cin >> val;
        if (val != 0) {
            x[idx[val]].x += 1;
            self += (long long)val * val % MOD;
        }
    }
    fft(x, L, 1);
    for (int i = 0; i < L; i++) {
        x[i] = x[i] * x[i];
    }
    fft(x, L, -1);
    long long ret = 0;
    for (int i = 0; i < L; i++) {
        if (x[i].x > 0.5) {
            long long c = (long long)(x[i].x + 0.5);
            ret += c * vals[i % (MOD - 1)];
        }
    }
    cout << (ret - self) / 2 << endl;
    return 0;
}