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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<iostream>
#include<cassert>
#include<stdexcept>
#include<utility>
#include<functional>
#include<numeric>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<array>
#include<stack>
#include<queue>
#include<deque>
#include<vector>
#include<complex>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<string>
#include<bitset>
#include<memory>
using namespace std;
using ll=long long;

#include<iostream>

namespace ProconLib{
    using ll=long long;
    using Int=int;
    template<Int MOD,bool IsPrime=true>
    class ModInt{
        Int n;        
        static Int regularize(int n){Int tmp=n%MOD; return tmp>=0 ? tmp : tmp+MOD;}
        static Int regularize(ll n){Int tmp=n%MOD; return tmp>=0 ? tmp : tmp+MOD;}
        public:
        ModInt():n(0){};
        ModInt(int n):n(regularize(n)){}
        ModInt(long long n):n(regularize(n)){}
        ModInt(const ModInt<MOD,IsPrime> &mn):n(mn.n){}
        explicit operator int() const{return n;}
        explicit operator long long() const{return n;}

        ModInt<MOD,IsPrime>& operator+=(ModInt<MOD,IsPrime> rhs);
        ModInt<MOD,IsPrime>& operator-=(ModInt<MOD,IsPrime> rhs);
        ModInt<MOD,IsPrime>& operator*=(ModInt<MOD,IsPrime> rhs);

        bool operator==(ModInt<MOD,IsPrime> rhs){return n==rhs.n;}
        const Int& get() const {return n;}
        void set(int x){n=regularize(x);}
        void set(ll x){n=regularize(x);}        
    };

    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime> operator+(ModInt<MOD,IsPrime> lhs,ModInt<MOD,IsPrime> rhs){return lhs+=rhs;};
    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime> operator+(ModInt<MOD,IsPrime> lhs,int rhs){return lhs+ModInt<MOD,IsPrime>(rhs);}
    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime> operator+(ModInt<MOD,IsPrime> lhs,ll rhs){return lhs+ModInt<MOD,IsPrime>(rhs);}
    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime> operator+(int lhs,ModInt<MOD,IsPrime> rhs){return ModInt<MOD,IsPrime>(lhs)+rhs;}
    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime> operator+(ll lhs,ModInt<MOD,IsPrime> rhs){return ModInt<MOD,IsPrime>(lhs)+rhs;}

    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime> operator+(ModInt<MOD,IsPrime> mn){return mn;};
    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime> operator-(ModInt<MOD,IsPrime> mn){return ModInt<MOD,IsPrime>(-mn.get());};

    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime> operator-(ModInt<MOD,IsPrime> lhs,ModInt<MOD,IsPrime> rhs){return lhs-=rhs;};
    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime> operator-(ModInt<MOD,IsPrime> lhs,int rhs){return lhs-ModInt<MOD,IsPrime>(rhs);};
    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime> operator-(ModInt<MOD,IsPrime> lhs,ll rhs){return lhs-ModInt<MOD,IsPrime>(rhs);};
    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime> operator-(int lhs,ModInt<MOD,IsPrime> rhs){return ModInt<MOD,IsPrime>(lhs)-rhs;};
    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime> operator-(ll lhs,ModInt<MOD,IsPrime> rhs){return ModInt<MOD,IsPrime>(lhs)-rhs;};

    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime> operator*(ModInt<MOD,IsPrime> lhs,ModInt<MOD,IsPrime> rhs){return lhs*=rhs;};
    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime> operator*(ModInt<MOD,IsPrime> lhs,int rhs){return lhs*ModInt<MOD,IsPrime>(rhs);}
    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime> operator*(ModInt<MOD,IsPrime> lhs,ll rhs){return lhs*ModInt<MOD,IsPrime>(rhs);}
    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime> operator*(ll lhs,ModInt<MOD,IsPrime> rhs){return ModInt<MOD,IsPrime>(lhs)*rhs;}
    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime> operator*(int lhs,ModInt<MOD,IsPrime> rhs){return ModInt<MOD,IsPrime>(lhs)*rhs;}

    template<Int MOD>
    ModInt<MOD,true> operator/(ModInt<MOD,true> lhs,ModInt<MOD,true> rhs);
    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime> powm(ModInt<MOD,IsPrime> x,Int k);
    template<Int MOD>
    ModInt<MOD,true> inv(ModInt<MOD,true> x){return powm(x,MOD-2);}

    template<Int MOD,bool IsPrime>
    std::ostream& operator<<(std::ostream& os,const ModInt<MOD,IsPrime> &mn);
    

    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime>& ModInt<MOD,IsPrime>::operator+=(ModInt<MOD,IsPrime> rhs){
        n+=rhs.n;
        n= n>=MOD ? n-MOD : n;
        return *this;
    }
    
    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime>& ModInt<MOD,IsPrime>::operator-=(ModInt rhs){
        n=n-rhs.n<0 ? n-rhs.n+MOD : n-rhs.n;
        return *this;
    }
    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime>& ModInt<MOD,IsPrime>::operator*=(ModInt<MOD,IsPrime> rhs){
        n=ll(n)*rhs.n%MOD;
        return *this;
    }
    
    template<Int MOD>
    ModInt<MOD,true> operator/(ModInt<MOD,true> lhs,ModInt<MOD,true> rhs){
        return lhs*inv(rhs);
    }
    template<Int MOD,bool IsPrime>
    ModInt<MOD,IsPrime> powm(ModInt<MOD,IsPrime> x,Int k){
        ModInt<MOD,IsPrime> res(1);
        while(k){
            if(k&1) res*=x;
            k>>=1;
            x*=x;
        }
        return res;
    }

    template<Int MOD,bool IsPrime>
    std::ostream& operator<<(std::ostream& os,const ModInt<MOD,IsPrime> &mn){
        os<<mn.get();
        return os;
    }

    template<Int MOD,bool IsPrime>
    std::istream& operator>>(std::istream& is,ModInt<MOD,IsPrime> &mn){
        Int tmp;
        is>>tmp;
        mn.set(tmp);
        return is;
    }
}
const int MOD=998244353;
using namespace ProconLib;
using mint=ModInt<MOD>;

const int SIZE=1000000;
int main(){
    int N;
    cin>>N;
    vector<int> A(N);
    for(int i=0;i<N;i++) cin>>A[i];

    vector<mint> cnt(SIZE+1);
    for(int i=0;i<N;i++) cnt[A[i]]+=1;

    vector<vector<int>> gs(SIZE+1);
    vector<mint> X(SIZE+1);
    vector<mint> Y(SIZE+1);
    vector<mint> Z(SIZE+1);
    for(int i=1;i<=SIZE;i++){
        for(int j=i;j<=SIZE;j+=i){
            X[i]+=cnt[j]*j;
            Y[i]+=cnt[j]*j*j;
            if(i!=j) gs[j].push_back(i);
        }
    }

    mint res=0;
    for(int i=SIZE;i>=1;i--){
        res+=(X[i]*X[i]-Y[i]-Z[i])*inv(mint(i));
        for(auto g:gs[i]){
            Z[g]+=X[i]*X[i]-Y[i]-Z[i];
        }
    }
    cout<<res*inv(mint(2))<<endl;
    return 0;
}