Problem p02793 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define watch(x) cerr << (#x) << ": " << (x) << endl
#define int long long

const int N = 1000009, MOD = 1000000007;
int n, a[N];

struct mint {
  int val;

  mint(int v = 0) {
    val = v % MOD;
    if (val < 0)
      val += MOD;
  }

  static int mod_inv(int a, int m = MOD) {
    int g = m, r = a, x = 0, y = 1;

    while (r != 0) {
      int q = g / r;
      g %= r;
      swap(g, r);
      x -= q * y;
      swap(x, y);
    }

    return x < 0 ? x + m : x;
  }

  explicit operator int() const { return val; }

  mint &operator+=(const mint &other) {
    val += other.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }

  mint &operator-=(const mint &other) {
    val -= other.val;
    if (val < 0)
      val += MOD;
    return *this;
  }

  static unsigned fast_mod(uint64_t x, unsigned m = MOD) { return x % m; }

  mint &operator*=(const mint &other) {
    val = fast_mod((uint64_t)val * other.val);
    return *this;
  }

  mint &operator/=(const mint &other) { return *this *= other.inv(); }

  friend mint operator+(const mint &a, const mint &b) { return mint(a) += b; }
  friend mint operator-(const mint &a, const mint &b) { return mint(a) -= b; }
  friend mint operator*(const mint &a, const mint &b) { return mint(a) *= b; }
  friend mint operator/(const mint &a, const mint &b) { return mint(a) /= b; }

  mint &operator++() {
    val = val == MOD - 1 ? 0 : val + 1;
    return *this;
  }

  mint &operator--() {
    val = val == 0 ? MOD - 1 : val - 1;
    return *this;
  }

  mint operator++(int32_t) {
    mint before = *this;
    ++*this;
    return before;
  }
  mint operator--(int32_t) {
    mint before = *this;
    --*this;
    return before;
  }

  mint operator-() const { return val == 0 ? 0 : MOD - val; }

  bool operator==(const mint &other) const { return val == other.val; }
  bool operator!=(const mint &other) const { return val != other.val; }

  mint inv() const { return mod_inv(val); }

  mint pow(int p) const {
    assert(p >= 0);
    mint a = *this, result = 1;

    while (p > 0) {
      if (p & 1)
        result *= a;

      a *= a;
      p >>= 1;
    }

    return result;
  }

  friend ostream &operator<<(ostream &stream, const mint &m) {
    return stream << m.val;
  }
};

int fac[N];
void init() {
  fac[1] = 1;
  for (int i = 2; i * i <= N; ++i) {
    if (fac[i] == 0) {
      fac[i] = i;

      for (int j = i; j < N; j += i) {
        fac[j] = j / i;
      }
    }
  }
}

map<int, int> get(int p) {
  map<int, int> ret;
  while (p != 1) {
    int nxt = fac[p];
    ret[p / nxt]++;
    p = nxt;
  }
  return ret;
}

int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  init();

  int g = a[0];
  for (int i = 1; i < n; ++i) {
    g = __gcd(g, a[i]);
  }

  for (int i = 0; i < n; ++i) {
    a[i] /= g;
  }

  map<int, int> f;

  for (int i = 0; i < n; ++i) {
    map<int, int> facts = get(a[i]);

    for (auto p : facts) {
      f[p.first] = max(f[p.first], p.second);
    }
  }

  mint mn(1);
  for (auto p : f) {
    mint base(p.first);
    mn *= base.pow(p.second);
  }

  mint ans(0);
  for (int i = 0; i < n; ++i) {
    ans += mn / a[i];
  }

  cout << ans << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define watch(x) cerr << (#x) << ": " << (x) << endl
#define int long long

const int N = 1000009, MOD = 1000000007;
int n, a[N];

struct mint {
  int val;

  mint(int v = 0) {
    val = v % MOD;
    if (val < 0)
      val += MOD;
  }

  static int mod_inv(int a, int m = MOD) {
    int g = m, r = a, x = 0, y = 1;

    while (r != 0) {
      int q = g / r;
      g %= r;
      swap(g, r);
      x -= q * y;
      swap(x, y);
    }

    return x < 0 ? x + m : x;
  }

  explicit operator int() const { return val; }

  mint &operator+=(const mint &other) {
    val += other.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }

  mint &operator-=(const mint &other) {
    val -= other.val;
    if (val < 0)
      val += MOD;
    return *this;
  }

  static unsigned fast_mod(uint64_t x, unsigned m = MOD) { return x % m; }

  mint &operator*=(const mint &other) {
    val = fast_mod((uint64_t)val * other.val);
    return *this;
  }

  mint &operator/=(const mint &other) { return *this *= other.inv(); }

  friend mint operator+(const mint &a, const mint &b) { return mint(a) += b; }
  friend mint operator-(const mint &a, const mint &b) { return mint(a) -= b; }
  friend mint operator*(const mint &a, const mint &b) { return mint(a) *= b; }
  friend mint operator/(const mint &a, const mint &b) { return mint(a) /= b; }

  mint &operator++() {
    val = val == MOD - 1 ? 0 : val + 1;
    return *this;
  }

  mint &operator--() {
    val = val == 0 ? MOD - 1 : val - 1;
    return *this;
  }

  mint operator++(int32_t) {
    mint before = *this;
    ++*this;
    return before;
  }
  mint operator--(int32_t) {
    mint before = *this;
    --*this;
    return before;
  }

  mint operator-() const { return val == 0 ? 0 : MOD - val; }

  bool operator==(const mint &other) const { return val == other.val; }
  bool operator!=(const mint &other) const { return val != other.val; }

  mint inv() const { return mod_inv(val); }

  mint pow(int p) const {
    assert(p >= 0);
    mint a = *this, result = 1;

    while (p > 0) {
      if (p & 1)
        result *= a;

      a *= a;
      p >>= 1;
    }

    return result;
  }

  friend ostream &operator<<(ostream &stream, const mint &m) {
    return stream << m.val;
  }
};

int fac[N];
void init() {
  fac[1] = 1;
  for (int i = 2; i <= N; ++i) {
    if (fac[i] == 0) {
      fac[i] = i;

      for (int j = i; j < N; j += i) {
        fac[j] = j / i;
      }
    }
  }
}

map<int, int> get(int p) {
  map<int, int> ret;
  while (p != 1) {
    int nxt = fac[p];
    ret[p / nxt]++;
    p = nxt;
  }
  return ret;
}

int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  init();

  int g = a[0];
  for (int i = 1; i < n; ++i) {
    g = __gcd(g, a[i]);
  }

  for (int i = 0; i < n; ++i) {
    a[i] /= g;
  }

  map<int, int> f;

  for (int i = 0; i < n; ++i) {
    map<int, int> facts = get(a[i]);

    for (auto p : facts) {
      f[p.first] = max(f[p.first], p.second);
    }
  }

  mint mn(1);
  for (auto p : f) {
    mint base(p.first);
    mn *= base.pow(p.second);
  }

  mint ans(0);
  for (int i = 0; i < n; ++i) {
    ans += mn / a[i];
  }

  cout << ans << '\n';

  return 0;
}