Problem p02905 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define INF 1000000009
#define LINF 1000000000000000009
#define double long double
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll n, ll m) { return (m ? gcd(m, n % m) : n); }
ll lcm(ll n, ll m) { return n / gcd(n, m) * m; }

template <class T> class segtree {
  int n;
  vector<T> data;
  T def;
  function<T(T, T)> operation;
  function<T(T, T)> update;
  T _query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return def;
    if (a <= l && r <= b)
      return data[k];
    T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2);
    T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r);
    return operation(c1, c2);
  }

public:
  segtree(size_t _n, T _def, function<T(T, T)> _operation,
          function<T(T, T)> _update)
      : def(_def), operation(_operation), update(_update) {
    n = 1;
    while (n < _n) {
      n *= 2;
    }
    data = vector<T>(2 * n - 1, def);
  }
  void change(int i, T x) {
    i += n - 1;
    data[i] = update(data[i], x);
    while (i > 0) {
      i = (i - 1) / 2;
      data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]);
    }
  }
  T query(int a, int b) { return _query(a, b, 0, 0, n); }
  T operator[](int i) { return data[i + n - 1]; }
};

ll mod = 998244353;

ll modpow(ll a, ll b) {
  ll ans = 1;
  a %= mod;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}

ll modinv(ll n) { return modpow(n, mod - 2); }

ll sz = 1000000;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> cnt(sz + 3);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
  }
  vector<ll> c(sz + 3);
  for (ll i = 1; i <= sz; i++) {
    c[i] += modinv(i);
    c[i] %= mod;
    for (ll j = 2 * i; j <= sz; j += i) {
      c[j] -= c[i];
      if (c[j] < 0)
        c[j] += mod;
    }
  }
  ll ans = 0;
  for (ll i = 1; i <= sz; i++) {
    ll x = 0, y = 0;
    for (ll j = i; j <= sz; j += i) {
      x += cnt[j] * j % mod;
      x %= mod;
      y += cnt[j] * j % mod * j % mod;
      y %= mod;
    }
    x = x * x;
    x %= mod;
    x -= y;
    x += mod;
    x %= mod;
    x *= modinv(2);
    x %= mod;
    x *= c[i];
    x %= mod;
    ans += x;
    ans %= mod;
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define INF 1000000009
#define LINF 1000000000000000009
#define double long double
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll n, ll m) { return (m ? gcd(m, n % m) : n); }
ll lcm(ll n, ll m) { return n / gcd(n, m) * m; }

template <class T> class segtree {
  int n;
  vector<T> data;
  T def;
  function<T(T, T)> operation;
  function<T(T, T)> update;
  T _query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return def;
    if (a <= l && r <= b)
      return data[k];
    T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2);
    T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r);
    return operation(c1, c2);
  }

public:
  segtree(size_t _n, T _def, function<T(T, T)> _operation,
          function<T(T, T)> _update)
      : def(_def), operation(_operation), update(_update) {
    n = 1;
    while (n < _n) {
      n *= 2;
    }
    data = vector<T>(2 * n - 1, def);
  }
  void change(int i, T x) {
    i += n - 1;
    data[i] = update(data[i], x);
    while (i > 0) {
      i = (i - 1) / 2;
      data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]);
    }
  }
  T query(int a, int b) { return _query(a, b, 0, 0, n); }
  T operator[](int i) { return data[i + n - 1]; }
};

ll mod = 998244353;

ll modpow(ll a, ll b) {
  ll ans = 1;
  a %= mod;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}

ll modinv(ll n) { return modpow(n, mod - 2); }

ll sz = 1000000;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> cnt(sz + 3);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
  }
  vector<ll> c(sz + 3);
  for (ll i = 1; i <= sz; i++) {
    c[i] += modinv(i);
    c[i] %= mod;
    for (ll j = 2 * i; j <= sz; j += i) {
      c[j] -= c[i];
      if (c[j] < 0)
        c[j] += mod;
    }
  }
  ll ans = 0;
  for (ll i = 1; i <= sz; i++) {
    ll x = 0, y = 0;
    for (ll j = i; j <= sz; j += i) {
      x += cnt[j] * j % mod;
      x %= mod;
      y += cnt[j] * j % mod * j % mod;
      y %= mod;
    }
    x = x * x;
    x %= mod;
    x -= y;
    if (x < 0)
      x += mod;
    x *= (mod + 1) / 2;
    x %= mod;
    x *= c[i];
    x %= mod;
    ans += x;
    ans %= mod;
  }
  cout << ans << endl;
}