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

#include <stdio.h>

#define N 1000000
const int mod = 998244353;

int n;
int A[N+1];
int B[N+1];
int M[N+1];
int S[N+1];
int inv[N+1];

/*
\sum_{i<j} lcm(Ai,Aj)
\sum_{i<j} Ai Aj / gcd(Ai,Aj)
\sum_{d} 1/d \sum_{i<j: gcd(Ai, Aj) = d} Ai Aj

f(d) = \sum_{i,j: gcd(Ai, Aj) = d} Ai Aj
F(d) = \sum_{i,j: d | gcd(Ai, Aj)} Ai Aj
F(d) = \sum_{i,j: d | Ai, d | Aj)} Ai Aj
F(d) = (\sum_{i: d | Ai} Ai)^2
\sum_{d} 1/d \sum_{n: d|n} m(n/d) F(n)
\sum_{n} F(n) \sum_{d: d|n} 1/d m(n/d)
*/

int modpow(int x, int n){
  int r=1;
  for(;n;n>>=1){
    if(n&1) r = (long long) r * x % mod;
    x = (long long) x * x % mod;
  }
  return r;
}

int main(){
  int i, j, r;
  scanf("%d", &n);
  for(i=0;i<n;i++){
    int x;
    scanf("%d",&x);
    A[x]++;
  }
  inv[1] = 1;
  for(i=2;i<=N;i++) inv[i] = (long long) (mod - mod/i) * inv[mod%i] % mod;

  M[1] = 1;
  for(i=2;i<=N;i++){
    if(!S[i]){
      M[i] = -1;
      for(j=2*i;j<=N;j+=i){
        S[j] = 1;
        if(M[j]) M[j] = -M[j];
        else M[j] = -1;
      }
    }
  }
  for(i=2;i*i<=N;i++){
    if(S[i]) continue;
    int ii = i*i;
    for(j=ii;j<=N;j+=ii) M[j] = 0;
  }

  for(i=1;i<=N;i++){
    for(j=i;j<=N;j+=i){
      B[i] = (B[i] + (long long) j * A[j]) % mod;
    }
  }

  for(i=1;i<=N;i++){
    B[i] = ((long long) B[i] * B[i]) % mod;
    for(j=i;j<=N;j+=i){
      B[i] = B[i] - (long long) j * j * A[j] % mod;
      if(B[i] < 0) B[i] += mod;
    }
    B[i] = B[i]&1?(B[i]+mod)/2:B[i]/2;
  }


  r = 0;
  for(i=1;i<=N;i++){
    int z = 0;
    for(j=1;j*i<=N;j++){
      if(M[j] > 0) z += B[i * j];
      else if(M[j] < 0) z += mod - B[i * j];
      if(z >= mod) z -= mod;
    }
    r = (r + (long long) inv[i] * z) % mod;
  }
  printf("%d\n", r);

  return 0;
}