Problem p02905 (C++)

Buggy submission (status: Runtime Error):
#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>;

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

#include <cstdio>

namespace niu {
char cur;
struct FIN {
  static inline bool is_blank(char c) { return c <= ' '; }
  inline char next() { return cur = getc_unlocked(stdin); }
  inline char peek() { return cur; }
  inline void skip() {
    while (is_blank(next())) {
    }
  }
#define intin(inttype)                                                         \
  FIN &operator>>(inttype &n) {                                                \
    bool sign = 0;                                                             \
    n = 0;                                                                     \
    skip();                                                                    \
    while (!is_blank(peek())) {                                                \
      if (peek() == '-')                                                       \
        sign = 1;                                                              \
      else                                                                     \
        n = (n << 1) + (n << 3) + (peek() & 0b1111);                           \
      next();                                                                  \
    }                                                                          \
    if (sign)                                                                  \
      n = -n;                                                                  \
    return *this;                                                              \
  }
  intin(int) intin(long long)
} fin;

char tmp[128];
struct FOUT {
  static inline bool is_blank(char c) { return c <= ' '; }
  inline void push(char c) { putc_unlocked(c, stdout); }
  FOUT &operator<<(char c) {
    push(c);
    return *this;
  }
  FOUT &operator<<(const char *s) {
    while (*s)
      push(*s++);
    return *this;
  }
#define intout(inttype)                                                        \
  FOUT &operator<<(inttype n) {                                                \
    if (n) {                                                                   \
      char *p = tmp + 127;                                                     \
      bool neg = 0;                                                            \
      if (n < 0)                                                               \
        neg = 1, n = -n;                                                       \
      while (n)                                                                \
        *--p = (n % 10) | 0b00110000, n /= 10;                                 \
      if (neg)                                                                 \
        *--p = '-';                                                            \
      return (*this) << p;                                                     \
    } else {                                                                   \
      push('0');                                                               \
      return *this;                                                            \
    }                                                                          \
  }
  intout(int) intout(long long)
} fout;
} // namespace niu

constexpr i64 A = 11;
int divs[A][240];
int dcnt[A];

int main() {
  rep(i, 1, A) {
    for (i64 x = i; x < A; x += i) {
      divs[x][dcnt[x]] = i;
      dcnt[x]++;
    }
  }

  std::vector<int> mu(A);
  mu[1] = 1;
  rep(x, 2, A) {
    for (int i = 0; i < dcnt[x]; i++) {
      int d = divs[x][i];
      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 (int i = 0; i < dcnt[x]; i++) {
      int d = divs[x][i];
      if (mu[d] == 1)
        f[x] += g[x / d];
      if (mu[d] == -1)
        f[x] -= g[x / d];
    }
  }

  i64 N;
  niu::fin >> N;
  vector<fp> sum(A);
  fp ans = 0;
  rep(i, 0, N) {
    int x;
    niu::fin >> x;
    fp res = 0;
    for (int j = 0; j < dcnt[x]; j++) {
      int d = divs[x][j];
      res += f[d] * sum[d];
      sum[d] += fp(x);
    }
    ans += res * fp(x);
  }
  niu::fout << ans.value() << "\n";
}


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>;

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

#include <cstdio>

namespace niu {
char cur;
struct FIN {
  static inline bool is_blank(char c) { return c <= ' '; }
  inline char next() { return cur = getc_unlocked(stdin); }
  inline char peek() { return cur; }
  inline void skip() {
    while (is_blank(next())) {
    }
  }
#define intin(inttype)                                                         \
  FIN &operator>>(inttype &n) {                                                \
    bool sign = 0;                                                             \
    n = 0;                                                                     \
    skip();                                                                    \
    while (!is_blank(peek())) {                                                \
      if (peek() == '-')                                                       \
        sign = 1;                                                              \
      else                                                                     \
        n = (n << 1) + (n << 3) + (peek() & 0b1111);                           \
      next();                                                                  \
    }                                                                          \
    if (sign)                                                                  \
      n = -n;                                                                  \
    return *this;                                                              \
  }
  intin(int) intin(long long)
} fin;

char tmp[128];
struct FOUT {
  static inline bool is_blank(char c) { return c <= ' '; }
  inline void push(char c) { putc_unlocked(c, stdout); }
  FOUT &operator<<(char c) {
    push(c);
    return *this;
  }
  FOUT &operator<<(const char *s) {
    while (*s)
      push(*s++);
    return *this;
  }
#define intout(inttype)                                                        \
  FOUT &operator<<(inttype n) {                                                \
    if (n) {                                                                   \
      char *p = tmp + 127;                                                     \
      bool neg = 0;                                                            \
      if (n < 0)                                                               \
        neg = 1, n = -n;                                                       \
      while (n)                                                                \
        *--p = (n % 10) | 0b00110000, n /= 10;                                 \
      if (neg)                                                                 \
        *--p = '-';                                                            \
      return (*this) << p;                                                     \
    } else {                                                                   \
      push('0');                                                               \
      return *this;                                                            \
    }                                                                          \
  }
  intout(int) intout(long long)
} fout;
} // namespace niu

constexpr i64 A = 1e6 + 1;
int divs[A][240];
int dcnt[A];

int main() {
  rep(i, 1, A) {
    for (i64 x = i; x < A; x += i) {
      divs[x][dcnt[x]] = i;
      dcnt[x]++;
    }
  }

  std::vector<int> mu(A);
  mu[1] = 1;
  rep(x, 2, A) {
    for (int i = 0; i < dcnt[x]; i++) {
      int d = divs[x][i];
      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 (int i = 0; i < dcnt[x]; i++) {
      int d = divs[x][i];
      if (mu[d] == 1)
        f[x] += g[x / d];
      if (mu[d] == -1)
        f[x] -= g[x / d];
    }
  }

  i64 N;
  niu::fin >> N;
  vector<fp> sum(A);
  fp ans = 0;
  rep(i, 0, N) {
    int x;
    niu::fin >> x;
    fp res = 0;
    for (int j = 0; j < dcnt[x]; j++) {
      int d = divs[x][j];
      res += f[d] * sum[d];
      sum[d] += fp(x);
    }
    ans += res * fp(x);
  }
  niu::fout << ans.value() << "\n";
}