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: Time Limit Exceeded)

#ifdef _MSC_VER
#include <__msvc_all_public_headers.hpp>
#undef min
#undef max
#else
#define NDEBUG
#include <bits/stdc++.h>
#endif

using namespace std;
using large = long long;
constexpr int P = 200'003;

int main()
{
    int n;
    cin >> n;
    static large a[200'005];
    int count = 0;
    for (int i = 0; i < n; ++i) {
        int a_i;
        cin >> a_i;
        if (a_i != 0) a[count++] = a_i;
    }
    n = count;
    sort(a, a + n);
    static large asum[200'005];
    asum[0] = 0;
    for (int i = 0; i < n; ++i) {
        asum[i + 1] = asum[i] + a[i];
    }


#if 0
//    cerr << "A {";
//    for (auto i = 0; i < n; ++i) cerr << " " << a[i];
//    cerr << " }\n";

    auto total = 0LL;
    for (int i = 0; i + 1 < n; ++i) {
        if (a[i] == 1) {
            total += asum[n] - asum[i + 1];
            continue;
        }

//        cerr << "A" << i << " = " << a[i] << "\n";

        auto x = 0LL;
        auto L = i + 1;
        while (L < n) {
            x = max(x + 1, a[i] * a[L] / P);
            auto thrs = P * x / a[i];
            auto itr = upper_bound(a + L, a + n, thrs);
            auto R = itr - a;
            total += (asum[R] - asum[L]) * a[i];
            total -= (R - L) * (x - 1) * P;

/*            if (L < R) {
                cerr << "  x is " << x << "\n";
                cerr << "  L, R: " << L << ", " << R << "\n";
                cerr << "   {";
                for (int y = L; y < R; ++y) cerr << " " << a[y];
                cerr << " }\n";
                cerr << "  plus  " << (asum[R] - asum[L]) * a[i] << "\n";
                cerr << "  minus " << (R - L) * (x - 1) * P << "\n";
            }
*/
            thrs = P * x / a[i];
            L = R;
        }
    }
#else

    static large B[200'005];
    int paired = 0;
    {
        int pos = n - 1;
        int i = 0;
        int j = 1;
        while (i < j) {
            if (a[i] + a[j] == P) {
                B[pos--] = a[j];
                B[pos--] = a[i];
                a[i] = -1;
                a[j] = -1;
                i++;
                j--;
                paired++;
                continue;
            }

            if (a[i] + a[j] > P) {
                j--;
            } else {
                i++;
            }
        }
        i = 0;
        j = 0;
        for (i = 0; i < n; ++i) {
            if (a[i] < 0) continue;
            B[j++] = a[i];
        }
    }

    auto total = 0LL;
    for (int i = 0; i + 1 < n; ++i) {
        int L = i + 1;
        int R = n - 2 * paired;
        if (R < L) {
            int x = (R - L + 1) & ~1;
            R += x;
        }
        for (int j = i + 1; j < n; ++j) {
            total += a[i] * a[j] % P;
        }
    }



#endif

    cout << total << endl;
    return 0;
}