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 Kotlin (1.3.71) (judge verdict: Wrong Answer)

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.io.PrintWriter
import java.util.*

class BIT(n: Int) {
    private val N =
            if (Integer.highestOneBit(n) == n) n
            else Integer.highestOneBit(n) shl 1

    private val bit = LongArray(N + 1)

    fun sum(i: Int): Long {
        var x = i
        var s = 0L
        while(x > 0) {
            s += bit[x]
            x -= x and -x
        }
        return s
    }

    fun add(i: Int, a: Long) {
        var x = i + 1
        while(x <= N) {
            bit[x] += a
            x += x and -x
        }
    }
}

fun PrintWriter.solve(sc: FastScanner) {
    val n = sc.nextInt()
    val a = Array(n) { sc.nextLong() }
    val p = 200003
    a.sortDescending()
    val bit = BIT(p + 1)
    var sum = 0L
    var ans = 0.toBigInteger()
    val cnt = mutableMapOf<Long, Long>()
    for (i in 0 until n) {
        ans += sum.toBigInteger() * a[i].toBigInteger()
        sum += a[i]
        cnt[a[i]] = (cnt[a[i]] ?: 0) + 1
    }
    var qsum = 0L
    for (v in cnt.keys.sortedDescending()) {
        if (v == 0L) break
        val c = cnt[v]!!
        val q = v * v / p
        qsum += q * c * (c - 1) / 2
        val s = bit.sum(p)
        qsum += c * q * (s - bit.sum((q * p / v).toInt()))
        for (i in q + 1 until v) {
            qsum += c * (s - bit.sum((i * p / v).toInt()))
        }
        bit.add(v.toInt(), c)
    }
    ans -= (qsum * p).toBigInteger()
    println(ans)
}

fun main() {
    val writer = PrintWriter(System.out, false)
    writer.solve(FastScanner(System.`in`))
    writer.flush()
}

class FastScanner(s: InputStream) {
    private var st = StringTokenizer("")
    private val br = BufferedReader(InputStreamReader(s))

    fun next(): String {
        while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())

        return st.nextToken()
    }

    fun nextInt() = next().toInt()
    fun nextLong() = next().toLong()
    fun nextLine() = br.readLine()
    fun nextDouble() = next().toDouble()
    fun ready() = br.ready()
}