Problem:
Score : 700 points

Problem Statement

We have an integer sequence of length N: A_0,A_1,\cdots,A_{N-1}.

Find the following sum (\mathrm{lcm}(a, b) denotes the least common multiple of a and b):

\sum_{i=0}^{N-2} \sum_{j=i+1}^{N-1} \mathrm{lcm}(A_i,A_j)

Since the answer may be enormous, compute it modulo 998244353.

Constraints

1 \leq N \leq 200000

1 \leq A_i \leq 1000000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_0\ A_1\ \cdots\ A_{N-1}

Output

Print the sum modulo 998244353.

Sample Input 1

3
2 4 6

Sample Output 1

22

\mathrm{lcm}(2,4)+\mathrm{lcm}(2,6)+\mathrm{lcm}(4,6)=4+6+12=22.

Sample Input 2

8
1 2 3 4 6 8 12 12

Sample Output 2

313

Sample Input 3

10
356822 296174 484500 710640 518322 888250 259161 609120 592348 713644

Sample Output 3

353891724

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;

public class Program
{
    private int N;
    private int[] A;
    private int MaxA = 1000000;


    // 素数表
    private List<int> Prime;
    private bool[] IsPrime;

    // L[i] 約数にiがある数のリスト
    //private List<int>[] L;

    // L[i] 約数にiがある数の2 l C 2パターン 積の総和
    private ModInt[] L;
    // 約数にiがある数の(今までの)総和
    private ModInt[] Sum;

    private ModInt?[] WMemo;
    private List<int>[] DivMemo;

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

        Prime = new List<int>();
        IsPrime = MathEx.Sieve(MaxA, Prime);

        HashSet<int> d = new HashSet<int>();

        DivMemo = new List<int>[MaxA + 1];
        L = new ModInt[MaxA+1];
        Sum = new ModInt[MaxA + 1];

        foreach (int i in A)
        {
            var divisor = Divisor(i, 0);
            foreach (int i1 in divisor)
            {
                L[i1] += Sum[i1] * i;
                Sum[i1] += i;
                d.Add(i1);
            }
        }

        WMemo = new ModInt?[MaxA + 1];
        ModInt ans = 0;
        foreach (int i in d)
        {
            ans += W(i) * L[i];
        }

        Console.WriteLine(ans);
    }

    private ModInt W(int i)
    {
        if (WMemo[i].HasValue)
            return WMemo[i].Value;
        ModInt res = ModInt.Inverse(i);

        var d = Divisor(i, 0);

        for (int j = 0; j < d.Count - 1; j++)
        {
            int div = d[j];
            res -= W(div);
        }

        WMemo[i] = res;
        return res;
    }

    private List<int> Divisor(int n, int index)
    {
        if (DivMemo[n] != null)
            return DivMemo[n];

        if (n == 1)
        {
            DivMemo[1] = new List<int>();
            DivMemo[1].Add(1);
            return DivMemo[1];
        }

        if (IsPrime[n])
        {
            DivMemo[n] = new List<int>();
            DivMemo[n].Add(1);
            DivMemo[n].Add(n);
            return DivMemo[n];
        }

        int tmpN = n;
        int p = -1;
        int cnt = 0;
        for (; Prime[index] * Prime[index] <= n; index++)
        {
            if (tmpN % Prime[index] == 0)
            {
                p = Prime[index];
                for (; tmpN % p == 0; cnt++, tmpN /= p) ;
                break;
            }
        }

        DivMemo[n] = new List<int>();
        var b = Divisor(tmpN, index + 1);

        int tmp = 1;
        for (int i = 0; i <= cnt; i++)
        {
            foreach (int i1 in b)
            {
                DivMemo[n].Add(tmp * i1);
            }

            tmp *= p;
        }

        return DivMemo[n];
    }

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

#region ModInt

/// <summary>
/// [0,<see cref="Mod"/>) までの値を取るような数
/// </summary>
public struct ModInt
{
    /// <summary>
    /// 剰余を取る値．
    /// </summary>
    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 GCD LCM

/// <summary>
/// 様々な数学的関数の静的メソッドを提供します．
/// </summary>
public static partial class MathEx
{
    /// <summary>
    /// 2 つの整数の最大公約数を求めます．
    /// </summary>
    /// <param name="n">最初の値</param>
    /// <param name="m">2 番目の値</param>
    /// <returns>2 つの整数の最大公約数</returns>
    /// <remarks>ユークリッドの互除法に基づき最悪計算量 O(log N) で実行されます．</remarks>
    public static int GCD(int n, int m)
    {
        return (int) GCD((long) n, m);
    }


    /// <summary>
    /// 2 つの整数の最大公約数を求めます．
    /// </summary>
    /// <param name="n">最初の値</param>
    /// <param name="m">2 番目の値</param>
    /// <returns>2 つの整数の最大公約数</returns>
    /// <remarks>ユークリッドの互除法に基づき最悪計算量 O(log N) で実行されます．</remarks>
    public static long GCD(long n, long m)
    {
        n = Math.Abs(n);
        m = Math.Abs(m);
        while (n != 0)
        {
            m %= n;
            if (m == 0) return n;
            n %= m;
        }

        return m;
    }


    /// <summary>
    /// 2 つの整数の最小公倍数を求めます．
    /// </summary>
    /// <param name="n">最初の値</param>
    /// <param name="m">2 番目の値</param>
    /// <returns>2 つの整数の最小公倍数</returns>
    /// <remarks>最悪計算量 O(log N) で実行されます．</remarks>
    public static long LCM(long n, long m)
    {
        return (n / GCD(n, m)) * m;
    }
}

#endregion

#region PrimeSieve

public static partial class MathEx
{
    /// <summary>
    /// ある値までに素数表を構築します．
    /// </summary>
    /// <param name="max">最大の値</param>
    /// <param name="primes">素数のみを入れた数列が返される</param>
    /// <returns>0 から max までの素数表</returns>
    /// <remarks>エラトステネスの篩に基づき，最悪計算量 O(N loglog N) で実行されます．</remarks>
    public static bool[] Sieve(int max, List<int> primes = null)
    {
        var isPrime = new bool[max + 1];
        for (int i = 2; i < isPrime.Length; i++) isPrime[i] = true;
        for (int i = 2; i * i <= max; i++)
            if (!isPrime[i]) continue;
            else
                for (int j = i * i; j <= max; j += i)
                    isPrime[j] = false;
        if (primes != null)
            for (int i = 0; i <= max; i++)
                if (isPrime[i])
                    primes.Add(i);

        return isPrime;
    }
}

#endregion

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

    const char Separator = ' ';
    private int _pos;
    private string[] _line;

    #region スペース区切りで取得

    public string Next()
    {
        if (_pos >= _line.Length)
        {
            _line = Console.ReadLine().Split(Separator);
            _pos = 0;
        }

        return _line[_pos++];
    }

    public int NextInt()
    {
        return int.Parse(Next());
    }

    public long NextLong()
    {
        return long.Parse(Next());
    }

    public double NextDouble()
    {
        return double.Parse(Next());
    }

    #endregion

    #region 型変換

    private int[] ToIntArray(string[] array)
    {
        var result = new int[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = int.Parse(array[i]);
        }

        return result;
    }

    private long[] ToLongArray(string[] array)
    {
        var result = new long[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = long.Parse(array[i]);
        }

        return result;
    }

    private double[] ToDoubleArray(string[] array)
    {
        var result = new double[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = double.Parse(array[i]);
        }

        return result;
    }

    #endregion

    #region 配列取得

    public string[] Array()
    {
        if (_pos >= _line.Length)
            _line = Console.ReadLine().Split(Separator);

        _pos = _line.Length;
        return _line;
    }

    public int[] IntArray()
    {
        return ToIntArray(Array());
    }

    public long[] LongArray()
    {
        return ToLongArray(Array());
    }

    public double[] DoubleArray()
    {
        return ToDoubleArray(Array());
    }

    #endregion
}