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# (.NET Core 3.1.201) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using CompLib.Mathematics;
using CompLib.Util;

public class Program
{
    private int N;
    private int[] A;

    // 原始根
    private const long G = 2;

    // T[G^i] = i
    private long[] T;

    public void Solve()
    {
        {
            T = new long[ModInt.Mod];
            long tmp = 1;
            for (int i = 0; i < ModInt.Mod - 1; i++)
            {
                T[tmp] = i;
                tmp *= G;
                tmp %= ModInt.Mod;
            }
        }

        var sc = new Scanner();
        N = sc.NextInt();
        A = sc.IntArray();

        FFT.ModInt2[] x = new FFT.ModInt2[FFT.Len];
        foreach (int i in A)
        {
            if (i != 0) x[T[i]] += 1;
        }

        FFT.Convolution2(x);
        long ans = 0;

        foreach (int i in A)
        {
            ans -= ((long) i * i) % ModInt.Mod;
        }

        for (int i = 0; i < 2 * ModInt.Mod; i++)
        {
            long n = x[i].Num;
            if (n == 0)
            {
                continue;
            }

            // Console.WriteLine($"{i} {n} {ModInt.Pow(G,n)}");
            ans += ModInt.Pow(G, i).num * n;
        }


        Console.WriteLine(ans / 2);
    }

    public static void Main(string[] args) => new Program().Solve();
}

class FFT
{
    // private const int Lg = 23;
    // private const int Lg = 18;
    private const int Lg = 19;
    public const int Len = 1 << Lg;

    // private static readonly ModInt2 O = new ModInt2(441, 31);
    // private static readonly ModInt2 O = new ModInt2(84488, 2192);
    private static readonly ModInt2 O = new ModInt2(1266, 1847);
    private static ModInt2[] Z, InvZ;
    private static readonly ModInt2 InvLen;

    static FFT()
    {
        Z = new ModInt2[Lg + 1];
        InvZ = new ModInt2[Lg + 1];
        Z[Lg] = O;
        InvZ[Lg] = ModInt2.Inverse(O);
        for (int i = Lg - 1; i >= 0; i--)
        {
            Z[i] = Z[i + 1] * Z[i + 1];
            InvZ[i] = InvZ[i + 1] * InvZ[i + 1];
        }

        InvLen = ModInt2.Inverse(Len);
    }

    private static int BitReverse(int n)
    {
        int result = 0;
        for (int i = 0; i < Lg; i++)
        {
            if ((n & (1 << i)) > 0)
            {
                result |= 1 << (Lg - i - 1);
            }
        }

        return result;
    }

    private static void Transform(ModInt2[] a, bool inverse)
    {
        for (int i = 0; i < Len; i++)
        {
            int rev = BitReverse(i);
            if (i < rev)
            {
                var tmp = a[i];
                a[i] = a[rev];
                a[rev] = tmp;
            }
        }

        for (int i = 1; i <= Lg; i++)
        {
            int b = 1 << (i - 1);
            ModInt2 z = 1;
            for (int j = 0; j < b; j++)
            {
                for (int k = j; k < Len; k += (b << 1))
                {
                    var t = z * a[k + b];
                    var u = a[k];
                    a[k] = u + t;
                    a[k + b] = u - t;
                }

                z *= inverse ? InvZ[i] : Z[i];
            }
        }
    }

    public static void Convolution(ModInt2[] a, ModInt2[] b)
    {
        Transform(a, false);
        Transform(b, false);
        for (int i = 0; i < Len; i++)
        {
            a[i] *= b[i];
        }

        Transform(a, true);
        for (int i = 0; i < Len; i++)
        {
            a[i] *= InvLen;
        }
    }

    public static void Convolution2(ModInt2[] a)
    {
        Transform(a, false);
        for (int i = 0; i < Len; i++)
        {
            a[i] *= a[i];
        }

        Transform(a, true);
        for (int i = 0; i < Len; i++)
        {
            a[i] *= InvLen;
        }
    }

    public struct ModInt2
    {
        private const long Mod1 = 15 * (1L << 27) + 1;
        private const long Mod2 = 119 * (1 << 23) + 1;

        private long Num1, Num2;

        public long Num
        {
            get
            {
                Num1 %= Mod1;
                Num2 %= Mod2;
                if (Num1 == Num2) return Num1;
                long r;
                EMath.ChaineseRemainderTheorem(Num1, Mod1, Num2, Mod2, out r);
                return r;
            }
        }

        public ModInt2(long n)
        {
            Num1 = n;
            Num2 = n;
        }

        public ModInt2(long n, long m)
        {
            Num1 = n;
            Num2 = m;
        }

        public static implicit operator ModInt2(long n)
        {
            return new ModInt2(n % Mod1, n % Mod2);
        }

        public static ModInt2 operator +(ModInt2 l, ModInt2 r)
        {
            return new ModInt2((l.Num1 + r.Num1) % Mod1, (l.Num2 + r.Num2) % Mod2);
        }

        public static ModInt2 operator *(ModInt2 l, ModInt2 r)
        {
            return new ModInt2((l.Num1 * r.Num1) % Mod1, (l.Num2 * r.Num2) % Mod2);
        }

        public static ModInt2 operator -(ModInt2 l, ModInt2 r)
        {
            long n = (l.Num1 - r.Num1) % Mod1;
            if (n < 0) n += Mod1;

            long m = (l.Num2 - r.Num2) % Mod2;
            if (m < 0) m += Mod2;
            return new ModInt2(n, m);
        }

        public static ModInt2 Pow(ModInt2 v, long k)
        {
            return new ModInt2(Pow(v.Num1, k, Mod1), Pow(v.Num2, k, Mod2));
        }

        public static ModInt2 Inverse(ModInt2 v)
        {
            return new ModInt2(Pow(v.Num1, Mod1 - 2, Mod1), Pow(v.Num2, Mod2 - 2, Mod2));
        }

        private static long Pow(long v, long k, long mod)
        {
            long result = 1;
            while (k > 0)
            {
                if (k % 2 == 1)
                {
                    result *= v;
                    result %= mod;
                }

                v *= v;
                v %= mod;
                k /= 2;
            }

            return result;
        }
    }
}


namespace CompLib.Mathematics
{
    public static partial class EMath
    {
        private static long Mod(long a, long m)
        {
            return (a % m + m) % m;
        }

        /// <summary>
        /// n%p=a, n%q=b, n%lcm(p*q)を求める ただしa=b (mod gcd(p,q))
        /// </summary>
        /// <param name="a"></param>
        /// <param name="p"></param>
        /// <param name="b"></param>
        /// <param name="q"></param>
        /// <returns></returns>
        public static long ChaineseRemainderTheorem(long a, long p, long b, long q, out long pq)
        {
            long c, d;
            long e = EMath.ExtGCD(p, q, out c, out d);
            pq = p / e * q;
            if (a % e != b % e)
            {
                return -1;
            }

            // pc + qd = e 

            // n = a + p(b-a)c/e

            // p(b-a)c/e (mod lcm(pq))

            long tmp = (b - a) / e * c % (q / e);
            return (a + Mod(p * tmp, pq)) % pq;
        }
    }

    public partial class EMath
    {
        /// <summary>
        /// ap+bq=gcd(a,b)を求め,gcd(a,b)を返す
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="p"></param>
        /// <param name="q"></param>
        /// <returns></returns>
        public static long ExtGCD(long a, long b, out long p, out long q)
        {
            if (b == 0)
            {
                p = 1;
                q = 0;
                return a;
            }

            long d = ExtGCD(b, a % b, out q, out p);
            q -= a / b * p;
            return d;
        }
    }
}

// https://bitbucket.org/camypaper/complib
namespace CompLib.Mathematics
{
    #region ModInt

    /// <summary>
    /// [0,<see cref="Mod"/>) までの値を取るような数
    /// </summary>
    public struct ModInt
    {
        /// <summary>
        /// 剰余を取る値．
        /// </summary>
        public const long Mod = 200003;
        // public const long Mod = 998244353;

        /// <summary>
        /// 実際の数値．
        /// </summary>
        public long num;

        /// <summary>
        /// 値が <paramref name="n"/> であるようなインスタンスを構築します．
        /// </summary>
        /// <param name="n">インスタンスが持つ値</param>
        /// <remarks>パフォーマンスの問題上，コンストラクタ内では剰余を取りません．そのため，<paramref name="n"/> ∈ [0,<see cref="Mod"/>) を満たすような <paramref name="n"/> を渡してください．このコンストラクタは O(1) で実行されます．</remarks>
        public ModInt(long n)
        {
            num = n;
        }

        /// <summary>
        /// このインスタンスの数値を文字列に変換します．
        /// </summary>
        /// <returns>[0,<see cref="Mod"/>) の範囲内の整数を 10 進表記したもの．</returns>
        public override string ToString()
        {
            return num.ToString();
        }

        public static ModInt operator +(ModInt l, ModInt r)
        {
            l.num += r.num;
            if (l.num >= Mod) l.num -= Mod;
            return l;
        }

        public static ModInt operator -(ModInt l, ModInt r)
        {
            l.num -= r.num;
            if (l.num < 0) l.num += Mod;
            return l;
        }

        public static ModInt operator *(ModInt l, ModInt r)
        {
            return new ModInt(l.num * r.num % Mod);
        }

        public static implicit operator ModInt(long n)
        {
            n %= Mod;
            if (n < 0) n += Mod;
            return new ModInt(n);
        }

        /// <summary>
        /// 与えられた 2 つの数値からべき剰余を計算します．
        /// </summary>
        /// <param name="v">べき乗の底</param>
        /// <param name="k">べき指数</param>
        /// <returns>繰り返し二乗法により O(N log N) で実行されます．</returns>
        public static ModInt Pow(ModInt v, long k)
        {
            return Pow(v.num, k);
        }

        /// <summary>
        /// 与えられた 2 つの数値からべき剰余を計算します．
        /// </summary>
        /// <param name="v">べき乗の底</param>
        /// <param name="k">べき指数</param>
        /// <returns>繰り返し二乗法により O(N log N) で実行されます．</returns>
        public static ModInt Pow(long v, long k)
        {
            long ret = 1;
            for (k %= Mod - 1; k > 0; k >>= 1, v = v * v % Mod)
                if ((k & 1) == 1)
                    ret = ret * v % Mod;
            return new ModInt(ret);
        }

        /// <summary>
        /// 与えられた数の逆元を計算します．
        /// </summary>
        /// <param name="v">逆元を取る対象となる数</param>
        /// <returns>逆元となるような値</returns>
        /// <remarks>法が素数であることを仮定して，フェルマーの小定理に従って逆元を O(log N) で計算します．</remarks>
        public static ModInt Inverse(ModInt v)
        {
            return Pow(v, Mod - 2);
        }
    }

    #endregion

    #region Binomial Coefficient

    public class BinomialCoefficient
    {
        public ModInt[] fact, ifact;

        public BinomialCoefficient(int n)
        {
            fact = new ModInt[n + 1];
            ifact = new ModInt[n + 1];
            fact[0] = 1;
            for (int i = 1; i <= n; i++)
                fact[i] = fact[i - 1] * i;
            ifact[n] = ModInt.Inverse(fact[n]);
            for (int i = n - 1; i >= 0; i--)
                ifact[i] = ifact[i + 1] * (i + 1);
            ifact[0] = ifact[1];
        }

        public ModInt this[int n, int r]
        {
            get
            {
                if (n < 0 || n >= fact.Length || r < 0 || r > n) return 0;
                return fact[n] * ifact[n - r] * ifact[r];
            }
        }

        public ModInt RepeatedCombination(int n, int k)
        {
            if (k == 0) return 1;
            return this[n + k - 1, k];
        }
    }

    #endregion
}


namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            if (_index >= _line.Length)
            {
                string s;
                do
                {
                    s = Console.ReadLine();
                } while (s.Length == 0);

                _line = s.Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public string ReadLine()
        {
            _index = _line.Length;
            return Console.ReadLine();
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            string s = Console.ReadLine();
            _line = s.Length == 0 ? new string[0] : s.Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}