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 Rust (1.42.0) (judge verdict: Accepted)

// ---------- begin karatsuba multiplication ----------
fn karatsuba<T>(a: &[T], b: &[T], c: &mut [T], zero: T, buf: &mut [T]) 
where T: std::marker::Copy +
         std::ops::Add<Output = T> +
         std::ops::Sub<Output = T> +
         std::ops::Mul<Output = T> +
{
    assert!(a.len() == b.len());
    let n = a.len();
    if n <= 16 {
        for (i, a) in a.iter().enumerate() {
            for (c, b) in c[i..].iter_mut().zip(b.iter()) {
                *c = *c + *a * *b;
            }
        }
        return;
    }
    if n & 1 == 1 {
        karatsuba(&a[1..], &b[1..], &mut c[2..], zero, buf);
        let x = a[0];
        let y = b[0];
        c[0] = c[0] + x * y;
        for (c, (a, b)) in c[1..].iter_mut().zip(a[1..].iter().zip(b[1..].iter())) {
            *c = *c + x * *b + *a * y;
        }
        return;
    }
    let m = n / 2;
    let (fa, ta) = a.split_at(m);
    let (fb, tb) = b.split_at(m);
    karatsuba(fa, fb, &mut c[..n], zero, buf);
    karatsuba(ta, tb, &mut c[n..], zero, buf);
    let (x, buf) = buf.split_at_mut(m);
    let (y, buf) = buf.split_at_mut(m);
    let (z, buf) = buf.split_at_mut(n);
    for z in z.iter_mut() {
        *z = zero;
    }
    for (x, (p, q)) in x.iter_mut().zip(fa.iter().zip(ta.iter())) {
        *x = *p + *q;
    }
    for (y, (p, q)) in y.iter_mut().zip(fb.iter().zip(tb.iter())) {
        *y = *p + *q;
    }
    karatsuba(x, y, z, zero, buf);
    for (z, (p, q)) in z.iter_mut().zip(c[..n].iter().zip(c[n..].iter())) {
        *z = *z - (*p + *q);
    }
    for (c, z) in c[m..].iter_mut().zip(z.iter()) {
        *c = *c + *z;
    }
}

fn multiply<T>(a: &[T], b: &[T], zero: T) -> Vec<T>
where T: std::marker::Copy +
         std::ops::Add<Output = T> +
         std::ops::Sub<Output = T> +
         std::ops::Mul<Output = T> +
{
    let mut i = 0;
    let mut j = 0;
    let mut ans = vec![zero; a.len() + b.len()];
    let mut buf = vec![zero; 4 * std::cmp::min(a.len(), b.len())];
    let mut c = Vec::with_capacity(a.len() + b.len());
    while i < a.len() && j < b.len() {
        let x = a.len() - i;
        let y = b.len() - j;
        let z = std::cmp::min(x, y);
        c.clear();
        c.resize(2 * z, zero);
        karatsuba(&a[i..(i + z)], &b[j..(j + z)], &mut c, zero, &mut buf);
        for (ans, c) in ans[(i + j)..].iter_mut().zip(c.iter()) {
            *ans = *ans + *c;
        }
        if x <= y {
            j += x;
        } else {
            i += y;
        }
    }
    ans.truncate(a.len() + b.len() - 1);
    ans
}
// ---------- end karatsuba multiplication ----------

use proconio::*;
use proconio::marker::*;
use std::collections::*;
use std::cmp::*;
use std::ops::Bound::*;

// Pが小さいことを利用しそう
// 積の総和から商を引く？
// 積の総和は容易
// 商の総和？
// A*B = Qp + r 
// のQが求まればいい
// 求められる？
// sum A * B = sum (qp + r)
// sum r = sum A * b - sum QP
// 
// 総和の積は簡単だと思ったが溢れる
//

#[fastout]
fn run() {
    input! {
        n: usize,
        a: [usize; n],
    }
    let p = 200_003usize;
    let mut rad = 0;
    for r in 2.. {
        let mut used = vec![false; p];
        let mut g = r;
        while !used[g] {
            used[g] = true;
            g = g * r % p;
        }
        if used.into_iter().filter(|p| *p).count() == p - 1 {
            rad = r;
            break;
        }
    }
    let mut index = vec![0; p];
    let mut g = rad;
    for i in 1..p {
        if index[g] == 0 {
            index[g] = i;
            g = rad * g % p;
        } else {
            assert!(g == rad);
        }
    }
    let mut cnt = vec![0; p];
    for a in a {
        if a != 0 {
            cnt[index[a]] += 1;
        }
    }
    let b = multiply(&cnt, &cnt, 0usize);
    let mut ans = 0usize;
    let mut pow = 1;
    for b in b.iter() {
        ans += *b * pow;
        pow = pow * rad % p;
    }
    let mut pow = 1;
    for i in 1..p {
        pow = rad * rad * pow % p;
        ans -= pow * cnt[i];
    }
    ans /= 2;
    println!("{}", ans);
}

fn main() {
    run();
}