Problem p02905 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define rev(i, s, e) for (i64(i) = (s); (i)-- > (e);)
#define all(x) x.begin(), x.end()

template <class T>
static inline std::vector<T> ndvec(size_t &&n, T val) noexcept {
  return std::vector<T>(n, std::forward<T>(val));
}

template <class... Tail>
static inline auto ndvec(size_t &&n, Tail &&...tail) noexcept {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(
      n, ndvec(std::forward<Tail>(tail)...));
}

#pragma GCC target("avx")

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

template <i64 M> struct modint {
  i64 a;
  constexpr modint(const i64 x = 0) noexcept : a((x % M + M) % M) {}
  constexpr i64 value() const noexcept { return a; }
  constexpr modint pow(i64 r) const noexcept {
    modint ans(1);
    modint aa = *this;
    while (r) {
      if (r & 1) {
        ans *= aa;
      }
      aa *= aa;
      r >>= 1;
    }
    return ans;
  }
  constexpr modint &operator+=(const modint r) noexcept {
    a += r.a;
    if (a >= M)
      a -= M;
    return *this;
  }
  constexpr modint &operator=(const i64 r) {
    a = (r % M + M) % M;
    return *this;
  }
  constexpr modint &operator-=(const modint r) noexcept {
    a -= r.a;
    if (a < 0)
      a += M;
    return *this;
  }
  constexpr modint &operator*=(const modint r) noexcept {
    a = a * r.a % M;
    return *this;
  }
  constexpr modint &operator/=(modint r) noexcept {
    i64 ex = M - 2;
    while (ex) {
      if (ex & 1) {
        *this *= r;
      }
      r *= r;
      ex >>= 1;
    }
    return *this;
  }

  constexpr modint operator+(const modint r) const {
    return modint(*this) += r;
  }
  constexpr modint operator-(const modint r) const {
    return modint(*this) -= r;
  }
  constexpr modint operator*(const modint r) const {
    return modint(*this) *= r;
  }
  constexpr modint operator/(const modint r) const {
    return modint(*this) /= r;
  }
};

template <const i64 M>
std::ostream &operator<<(std::ostream &os, const modint<M> &m) {
  os << m.value();
  return os;
}

const i64 MOD = 998244353;
using fp = modint<MOD>;

constexpr i64 A = 1e6 + 1;

i64 gcd(i64 a, i64 b) {
  while (b != 0) {
    a %= b;
    swap(a, b);
  }
  return a;
}

int main() {

  vector<vector<int>> divs(A);
  rep(i, 1, A) {
    for (i64 x = i; x <= A; x += i) {
      divs[x].emplace_back(i);
    }
  }

  std::vector<int> mu(A);
  mu[1] = 1;
  rep(x, 2, A) {
    for (auto d : divs[x]) {
      if (d < x)
        mu[x] -= mu[d];
    }
  }
  std::vector<fp> g(A);
  rep(i, 1, A) { g[i] = fp(i).pow(MOD - 2); }

  std::vector<fp> f(A);
  rep(x, 1, A) {
    for (auto d : divs[x]) {
      if (mu[d] == 1)
        f[x] += g[x / d];
      if (mu[d] == -1)
        f[x] -= g[x / d];
    }
  }

  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  i64 N;
  cin >> N;
  vector<fp> sum(A);
  fp ans = 0;
  rep(i, 0, N) {
    i64 A;
    cin >> A;
    fp res = 0;
    for (auto d : divs[A]) {
      res += f[d] * sum[d];
      sum[d] += fp(A);
    }
    ans += res * fp(A);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define rev(i, s, e) for (i64(i) = (s); (i)-- > (e);)
#define all(x) x.begin(), x.end()

template <class T>
static inline std::vector<T> ndvec(size_t &&n, T val) noexcept {
  return std::vector<T>(n, std::forward<T>(val));
}

template <class... Tail>
static inline auto ndvec(size_t &&n, Tail &&...tail) noexcept {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(
      n, ndvec(std::forward<Tail>(tail)...));
}

#pragma GCC target("avx")

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

template <i64 M> struct modint {
  i64 a;
  constexpr modint(const i64 x = 0) noexcept : a(x) {}
  constexpr i64 value() const noexcept { return a; }
  constexpr modint pow(i64 r) const noexcept {
    modint ans(1);
    modint aa = *this;
    while (r) {
      if (r & 1) {
        ans *= aa;
      }
      aa *= aa;
      r >>= 1;
    }
    return ans;
  }
  constexpr modint &operator+=(const modint r) noexcept {
    a += r.a;
    if (a >= M)
      a -= M;
    return *this;
  }
  constexpr modint &operator=(const i64 r) {
    a = (r % M + M) % M;
    return *this;
  }
  constexpr modint &operator-=(const modint r) noexcept {
    a -= r.a;
    if (a < 0)
      a += M;
    return *this;
  }
  constexpr modint &operator*=(const modint r) noexcept {
    a = a * r.a % M;
    return *this;
  }
  constexpr modint &operator/=(modint r) noexcept {
    i64 ex = M - 2;
    while (ex) {
      if (ex & 1) {
        *this *= r;
      }
      r *= r;
      ex >>= 1;
    }
    return *this;
  }

  constexpr modint operator+(const modint r) const {
    return modint(*this) += r;
  }
  constexpr modint operator-(const modint r) const {
    return modint(*this) -= r;
  }
  constexpr modint operator*(const modint r) const {
    return modint(*this) *= r;
  }
  constexpr modint operator/(const modint r) const {
    return modint(*this) /= r;
  }
};

template <const i64 M>
std::ostream &operator<<(std::ostream &os, const modint<M> &m) {
  os << m.value();
  return os;
}

const i64 MOD = 998244353;
using fp = modint<MOD>;

constexpr i64 A = 1e6 + 1;

i64 gcd(i64 a, i64 b) {
  while (b != 0) {
    a %= b;
    swap(a, b);
  }
  return a;
}

int main() {

  vector<vector<int>> divs(A);
  rep(i, 1, A) {
    for (i64 x = i; x <= A; x += i) {
      divs[x].emplace_back(i);
    }
  }

  std::vector<int> mu(A);
  mu[1] = 1;
  rep(x, 2, A) {
    for (auto d : divs[x]) {
      if (d < x)
        mu[x] -= mu[d];
    }
  }
  std::vector<fp> g(A);
  rep(i, 1, A) { g[i] = fp(i).pow(MOD - 2); }

  std::vector<fp> f(A);
  rep(x, 1, A) {
    for (auto d : divs[x]) {
      if (mu[d] == 1)
        f[x] += g[x / d];
      if (mu[d] == -1)
        f[x] -= g[x / d];
    }
  }

  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  i64 N;
  cin >> N;
  vector<fp> sum(A);
  fp ans = 0;
  rep(i, 0, N) {
    i64 A;
    cin >> A;
    fp res = 0;
    for (auto d : divs[A]) {
      res += f[d] * sum[d];
      sum[d] += fp(A);
    }
    ans += res * fp(A);
  }
  cout << ans << endl;
}