Problem p02793 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef pair<int, int> pi;

const int MOD = 1000000007;

// const int N = 50;
const int N = 1000005;
vi pt(N, 0);

void init_pt() {
  for (int i = 2; i < N; i++) {
    if (!pt[i]) {
      for (int j = i; j < N; j += i) {
        pt[j] = i;
      }
    }
  }
}

vi factList(int n) {
  vi f;
  while (n > 1) {
    f.push_back(pt[n]);
    n /= pt[n];
  }
  return f;
}

vector<pi> fact(int n) {
  vi fl = factList(n);
  vector<pi> f(1, pi(fl[0], 0));
  for (int i : fl) {
    if (f.back().first == i) {
      f.back().second++;
    } else {
      f.push_back(pi(i, 1));
    }
  }
  return f;
}

ll mpow(ll a, ll n) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n = n >> 1;
  }
  return res;
}

ll mod_inv(ll a, ll b) {
  ll inv = mpow(b, MOD - 2);
  return a * inv % MOD;
}

int main() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];
  init_pt();

  map<int, int> max_f;
  for (int i = 0; i < n; i++) {
    vector<pi> f = fact(a[i]);
    for (pi j : f) {
      max_f[j.first] = max(j.second, max_f[j.first]);
    }
  }

  ll lcm = 1;
  for (pi fac : max_f) {
    rep(_, fac.second) { lcm = lcm * fac.first % MOD; }
  }

  ll ans = 0;
  rep(i, n) { ans = (ans + mod_inv(lcm, a[i])) % MOD; }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef pair<int, int> pi;

const int MOD = 1000000007;

// const int N = 50;
const int N = 1000005;
vi pt(N, 0);

void init_pt() {
  for (int i = 2; i < N; i++) {
    if (!pt[i]) {
      for (int j = i; j < N; j += i) {
        pt[j] = i;
      }
    }
  }
}

vi factList(int n) {
  vi f;
  while (n > 1) {
    f.push_back(pt[n]);
    n /= pt[n];
  }
  return f;
}

vector<pi> fact(int n) {
  vi fl = factList(n);
  if (fl.size() == 0)
    return vector<pi>(0);
  vector<pi> f(1, pi(fl[0], 0));
  for (int i : fl) {
    if (f.back().first == i) {
      f.back().second++;
    } else {
      f.push_back(pi(i, 1));
    }
  }
  return f;
}

ll mpow(ll a, ll n) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n = n >> 1;
  }
  return res;
}

ll mod_inv(ll a, ll b) {
  ll inv = mpow(b, MOD - 2);
  return a * inv % MOD;
}

int main() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];
  init_pt();

  map<int, int> max_f;
  for (int i = 0; i < n; i++) {
    vector<pi> f = fact(a[i]);
    for (pi j : f) {
      max_f[j.first] = max(j.second, max_f[j.first]);
    }
  }

  ll lcm = 1;
  for (pi fac : max_f) {
    rep(_, fac.second) { lcm = lcm * fac.first % MOD; }
  }

  ll ans = 0;
  rep(i, n) { ans = (ans + mod_inv(lcm, a[i])) % MOD; }
  cout << ans << endl;
  return 0;
}