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: Accepted)

using System;
using CompLib.Util;
using System.Diagnostics;
using CompLib.Algorithm;
using CompLib.Mathematics;

public class Program
{
    private const int P = 200003;
    private const int G = 2;
    private int N;
    private int[] A;

    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        A = sc.IntArray();

        int[] t = new int[P];
        int tmp = 1;
        for (int i = 0; i < P - 1; i++)
        {
            t[tmp] = i;
            tmp *= G;
            tmp %= P;
        }

        ModInt2[] x = new ModInt2[P - 1];
        foreach (int i in A)
        {
            if (i != 0) x[t[i]] += 1;
        }

        FastFourierTransform2.Convolution2(ref x);

        long ans = 0;

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

        tmp = 1;
        for (int i = 0; i <= 2 * (P - 2); i++)
        {
            ans += tmp * x[i].Num;
            tmp *= G;
            tmp %= P;
        }

        Console.WriteLine(ans / 2);
    }

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


namespace CompLib.Algorithm
{
    public class FastFourierTransform2
    {
        private const int E = 26;
        public static readonly ModInt2 O = new ModInt2(18769, 136);

        public static readonly ModInt2[] Z;
        public static readonly ModInt2[] InvZ;
        private static readonly ModInt2[] InvLen;

        static FastFourierTransform2()
        {
            Z = new ModInt2[E + 1];
            InvZ = new ModInt2[E + 1];
            Z[E] = O;
            InvZ[E] = ModInt2.Inverse(O);

            for (int i = E - 1; i >= 0; i--)
            {
                Z[i] = Z[i + 1] * Z[i + 1];
                InvZ[i] = InvZ[i + 1] * InvZ[i + 1];
            }

            InvLen = new ModInt2[E + 1];
            for (int lg = 0; lg <= E; lg++)
            {
                InvLen[lg] = ModInt2.Inverse(1 << lg);
            }
        }

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

            return result;
        }

        private static void Transform(ModInt2[] a, int lg, bool inverse)
        {
            Debug.Assert(a.Length == (1 << lg));
            for (int i = 0; i < a.Length; i++)
            {
                int rev = BitReverse(i, lg);
                if (i < rev)
                {
                    var t = a[i];
                    a[i] = a[rev];
                    a[rev] = t;
                }
            }

            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 < a.Length; 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(ref ModInt2[] a, ModInt2[] b)
        {
            int sz = a.Length + b.Length - 1;
            int lg = 0;
            int len = 1;
            for (; len < sz; lg++, len <<= 1) ;

            Array.Resize(ref a, len);
            Array.Resize(ref b, len);
            Transform(a, lg, false);
            Transform(b, lg, false);
            for (int i = 0; i < len; i++)
            {
                a[i] *= b[i];
            }

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

        public static void Convolution2(ref ModInt2[] a)
        {
            int sz = a.Length + a.Length - 1;
            int lg = 0;
            int len = 1;
            for (; len < sz; lg++, len <<= 1) ;

            Array.Resize(ref a, len);
            Transform(a, lg, false);
            for (int i = 0; i < len; i++)
            {
                a[i] *= a[i];
            }

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

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

        public long Num1 { private set; get; }
        public long Num2 { private set; get; }

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

        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)
        {
            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 operator *(ModInt2 l, ModInt2 r)
        {
            return new ModInt2((l.Num1 * r.Num1) % Mod1, (l.Num2 * r.Num2) % 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 & 1) > 0)
                {
                    result *= v;
                    result %= mod;
                }

                v *= v;
                v %= mod;
                k >>= 1;
            }

            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;
        }
    }
}

namespace CompLib.Mathematics
{
    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;
        }
    }
}

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();
    }
}