Problem p02905 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

#define scan(x)                                                                \
  do {                                                                         \
    while ((x = getchar()) < '0')                                              \
      ;                                                                        \
    for (x -= '0'; '0' <= (_ = getchar()); x = (x << 3) + (x << 1) + _ - '0')  \
      ;                                                                        \
  } while (0)
char _;
#define complete_unique(a) a.erase(unique(a.begin(), a.end()), a.end())
#define all(a) a.begin(), a.end()
#define println printf("\n");
#define readln(x) getline(cin, x);
#define pb push_back
#define endl "\n"
#define INT_INF 0x3f3f3f3f
#define LL_INF 0x3f3f3f3f3f3f3f3f
#define MOD 998244353
#define MOD2 1494318097
#define SEED 131
#define mp make_pair
#define fastio                                                                 \
  cin.tie(0);                                                                  \
  cin.sync_with_stdio(0);

#define MAXN 200005
#define MAXV 1000005

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef unordered_map<int, int> umii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef pair<int, pii> triple;
typedef int8_t byte;

mt19937 g1(time(0));

int randint(int a, int b) { return uniform_int_distribution<int>(a, b)(g1); }
ll randlong(ll a, ll b) {
  return uniform_int_distribution<long long>(a, b)(g1);
}

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll fpow(ll b, ll exp, ll mod) {
  if (exp == 0)
    return 1;
  ll t = fpow(b, exp / 2, mod);
  if (exp & 1)
    return t * t % mod * b % mod;
  return t * t % mod;
}
ll divmod(ll i, ll j, ll mod) {
  i %= mod, j %= mod;
  return i * fpow(j, mod - 2, mod) % mod;
}

int len, arr[MAXN];
vector<int> nums[MAXV];
ll dp[MAXV], res;

int main() {
  scanf("%d", &len);
  for (int i = 1; i <= len; i++) {
    scanf(" %d", &arr[i]);
    for (int k = 1; k <= sqrt(arr[i]); k++) {
      if (arr[i] % k == 0) {
        nums[k].pb(arr[i]);
        if (k * k != arr[i])
          nums[arr[i] / k].pb(arr[i]);
      }
    }
  }
  for (int n = 1000000; n >= 1; n--) {
    ll sum = 0, tot = 0;
    for (int check : nums[n])
      sum = (sum + check) % MOD;
    for (int i = 0; i < nums[n].size(); i++) {
      sum = (sum - nums[n][i] + MOD) % MOD;
      tot = (tot + nums[n][i] * sum) % MOD;
    }
    for (int v = n + n; v <= 1000000; v += n)
      tot = (tot - dp[v] + MOD) % MOD;
    res = (res + divmod(tot, n, MOD)) % MOD;
    dp[n] = tot;
  }
  printf("%lld\n", res);
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

#define scan(x)                                                                \
  do {                                                                         \
    while ((x = getchar()) < '0')                                              \
      ;                                                                        \
    for (x -= '0'; '0' <= (_ = getchar()); x = (x << 3) + (x << 1) + _ - '0')  \
      ;                                                                        \
  } while (0)
char _;
#define complete_unique(a) a.erase(unique(a.begin(), a.end()), a.end())
#define all(a) a.begin(), a.end()
#define println printf("\n");
#define readln(x) getline(cin, x);
#define pb push_back
#define endl "\n"
#define INT_INF 0x3f3f3f3f
#define LL_INF 0x3f3f3f3f3f3f3f3f
#define MOD 998244353
#define MOD2 1494318097
#define SEED 131
#define mp make_pair
#define fastio                                                                 \
  cin.tie(0);                                                                  \
  cin.sync_with_stdio(0);

#define MAXN 200005
#define MAXV 1000005

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef unordered_map<int, int> umii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef pair<int, pii> triple;
typedef int8_t byte;

mt19937 g1(time(0));

int randint(int a, int b) { return uniform_int_distribution<int>(a, b)(g1); }
ll randlong(ll a, ll b) {
  return uniform_int_distribution<long long>(a, b)(g1);
}

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll fpow(ll b, ll exp, ll mod) {
  if (exp == 0)
    return 1;
  ll t = fpow(b, exp / 2, mod);
  if (exp & 1)
    return t * t % mod * b % mod;
  return t * t % mod;
}
ll divmod(ll i, ll j, ll mod) {
  i %= mod, j %= mod;
  return i * fpow(j, mod - 2, mod) % mod;
}

int len, arr[MAXN];
vector<int> nums[MAXV];
ll dp[MAXV], res;

int main() {
  scanf("%d", &len);
  for (int i = 1; i <= len; i++) {
    scanf(" %d", &arr[i]);
    for (int k = 1; k <= sqrt(arr[i]); k++) {
      if (arr[i] % k == 0) {
        nums[k].pb(arr[i]);
        if (k * k != arr[i])
          nums[arr[i] / k].pb(arr[i]);
      }
    }
  }
  for (int n = 1000000; n >= 1; n--) {
    if (!nums[n].size())
      continue;
    ll sum = 0, tot = 0;
    for (int check : nums[n])
      sum = (sum + check) % MOD;
    for (int i = 0; i < nums[n].size(); i++) {
      sum = (sum - nums[n][i] + MOD) % MOD;
      tot = (tot + nums[n][i] * sum) % MOD;
    }
    for (int v = n + n; v <= 1000000; v += n)
      tot = (tot - dp[v] + MOD) % MOD;
    res = (res + divmod(tot, n, MOD)) % MOD;
    dp[n] = tot;
  }
  printf("%lld\n", res);
}