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)

// https://atcoder.jp/contests/agc047/tasks/agc047_c
//
#![allow(unused_imports)]
use std::io::*;
use std::io::Write;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

// Input macros.
// Original by tanakh: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, switch) => {
        {
            let ty = read_value!($iter, i32);
            if ty == 1 {
                vec![ty, read_value!($iter, i32), read_value!($iter, i32)]
            } else if ty == 2 {
                vec![ty, read_value!($iter, i32)]
            } else {
                vec![ty, read_value!($iter, i32)]
            }
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! read_line {
    ($t:tt) => {
        {
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).unwrap();
            s.trim_right().parse::<$t>().unwrap()
        }
    }
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        eprintln!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

//===

#[derive(Default, Debug, Clone, Copy, PartialEq)]
struct Complex {
    re: f64,
    im: f64
}

impl Complex {
    fn new(re: f64, im: f64) -> Self {
        Self { re, im }
    }

    fn zero() -> Self {
        Self::new(0f64, 0f64)
    }

    fn one() -> Self {
        Self::new(1f64, 0f64)
    }

    fn zeta(i: i64, n: i64) -> Self {
        let rad = (2f64 * std::f64::consts::PI / n as f64) * (i as f64);
        Self::new(rad.cos(), rad.sin())
    }
}

impl std::ops::Add for Complex {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        Complex::new(self.re + rhs.re, self.im + rhs.im)
    }
}

impl std::ops::Sub for Complex {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self::Output {
        Complex::new(self.re - rhs.re, self.im - rhs.im)
    }
}
impl std::ops::Mul for Complex {
    type Output = Self;
    fn mul(self, rhs: Self) -> Self::Output {
        Complex::new(self.re * rhs.re - self.im * rhs.im, self.re * rhs.im + self.im * rhs.re)
    }
}

/// Cooley–Tukey FFT algorithm.
/// O(NlogN)
fn fft(f: &mut Vec<Complex>, sign: i64) {
    let n = f.len();
    assert!(n.is_power_of_two());
    let wlen = n.trailing_zeros() as usize;
    for i in 0..n {
        let mut j = i;
        let mut k = 0;
        for _ in 0..wlen {
            k = (k << 1) | (j & 1);
            j >>= 1;
        }
        if i < k {
            f.swap(i, k);
        }
    }
    for k in 1..=wlen {
        let block_size = 1usize<<k;
        let w = Complex::zeta(sign, block_size as i64);
        for chunk in f.chunks_exact_mut(block_size) {
            let (l, r) = chunk.split_at_mut(block_size/2);
            let mut q = Complex::one();
            for (l, r) in l.iter_mut().zip(r.iter_mut()) {
                let s = *l;
                let t = q * *r;
                *l = s + t;
                *r = s - t;
                q = q * w;
            }
        }
    }
}

fn convolution(g: Vec<i64>, h: Vec<i64>) -> Vec<i64> {
    let n = (g.len() + h.len() + 1).next_power_of_two();
    let mut a = vec![Complex::zero(); n];
    let mut b = vec![Complex::zero(); n];
    for i in 0..g.len() {
        a[i].re = g[i] as f64;
    }
    for i in 0..h.len() {
        b[i].re = h[i] as f64;
    }

    fft(&mut a, 1);
    fft(&mut b, 1);
    for i in 0..n {
        a[i] = a[i] * b[i];
    }
    fft(&mut a, -1);

    let mut c = vec![0; n];
    for i in 0..n {
        c[i] = (a[i].re / (n as f64)).round() as i64;
    }
    c
}

const MOD: usize = 200003;

fn main() {
    input! {
        n: usize,
        a: [usize; n]
    };


    let mut pow2idx = vec![MOD; MOD];
    let mut p2 = 1;
    for i in 0..MOD {
        pow2idx[p2] = i;
        p2 = (p2 * 2) % MOD;
    }

    let mut count = vec![0; MOD];
    for i in 0..n {
        if a[i] >= 1 {
            count[pow2idx[a[i]]] += 1;
        }
    }
    let ret = convolution(count.clone(), count);

    let mut ans = 0;
    let n = ret.len();
    let mut p2 = 1;
    let modi64 = MOD as i64;
    for i in 0..n {
        ans += ret[i] * p2;
        p2 = (p2 * 2) % modi64;
    }
    for i in 0..a.len() {
        ans -= ((a[i] * a[i]) as i64) % modi64;
    }
    println!("{}", ans / 2);
}