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)

// SPDX-License-Identifier: X11
// 2020-08-09 (Live)

#include <stdio.h>
#include <stdlib.h>

#define P 200003

int rcomp (const void *a, const void *b);

int main() {
  long N;
  scanf("%ld", &N);

  long x;
  long *A = calloc(N, sizeof(long));
  for (long i = 0; i < N; ++i) {
    scanf("%ld", &x);
    if ((x %= P) == 0)
      continue;
    else
      A[i] = x;
  }
  qsort(A, N, sizeof(long), rcomp);
  for (long i = N - 1; A[i] == 0; --N, --i);

  long sum = 0;
  for (long i = N - 1; i >= 1; --i) {
    x = 0;
    for (long j = i - 1; j >= 0; --j) {
      sum += A[i] * A[j] % P;
    }
  }

  printf("%ld\n", sum);
  free(A);
}

int rcomp (const void *a, const void *b) {
  const long *la = (const long *) a;
  const long *lb = (const long *) b;

  return (*la < *lb) - (*la > *lb);
}