Problem p02793 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;
const ll MOD = 1e9 + 7;

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

int main() {
  vector<bool> prime(1000001, true);
  for (int i = 2; i < 1000; i++) {
    if (prime[i]) {
      for (int j = i * 2; j < 1000001; j += i)
        prime[j] = false;
    }
  }
  vector<ll> primes(0);
  for (int i = 2; i < 1000000; i++) {
    if (prime[i])
      primes.push_back(i);
  }
  int M = primes.size();
  // rep(i, 100) cout << primes[i] << " ";
  // cout << endl;
  int N;
  cin >> N;
  vector<ll> A(N), factor(1000000, 0);
  rep(i, N) {
    cin >> A[i];
    ll x = A[i];
    rep(j, M) {
      int p = primes[j];
      if (x < p) {
        if (factor[x] == 0)
          factor[x] = 1;
        // cout << x << " 1" << endl;
        break;
      }
      int c = 0;
      while (x % p == 0) {
        x /= p;
        c++;
        // cout << "x=" <<  x << ", p=" << p << ", c=" << c << endl;
      }
      if (c > factor[p])
        factor[p] = c;
      // cout << A[i] << " " << primes[j] << " " << c << endl;
    }
  }
  /*
  rep(i, M) {
      if (factor[primes[i]] != 0) cout << primes[i] << " " << factor[primes[i]]
  << endl;
  }
  */
  ll lc = 1;
  rep(i, M) {
    rep(j, factor[primes[i]]) { lc = (lc * primes[i]) % MOD; }
  }
  // cout << lc << endl;
  ll ans = 0;
  rep(i, N) { ans = (ans + lc * mod_pow(A[i], MOD - 2, MOD)) % MOD; }
  cout << ans << endl;
}

Fixed submission:
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;
const ll MOD = 1e9 + 7;

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

int main() {
  vector<bool> prime(1000001, true);
  for (int i = 2; i < 1000; i++) {
    if (prime[i]) {
      for (int j = i * 2; j < 1000001; j += i)
        prime[j] = false;
    }
  }
  vector<ll> primes(0);
  for (int i = 2; i < 1000000; i++) {
    if (prime[i])
      primes.push_back(i);
  }
  int M = primes.size();
  // rep(i, 100) cout << primes[i] << " ";
  // cout << endl;
  int N;
  cin >> N;
  vector<ll> A(N), factor(1000000, 0);
  rep(i, N) {
    cin >> A[i];
    ll x = A[i];
    rep(j, M) {
      int p = primes[j];
      if (A[i] < p * p) {
        if (factor[x] == 0)
          factor[x] = 1;
        // cout << x << " 1" << endl;
        break;
      }
      int c = 0;
      while (x % p == 0) {
        x /= p;
        c++;
        // cout << "x=" <<  x << ", p=" << p << ", c=" << c << endl;
      }
      if (c > factor[p])
        factor[p] = c;
      // cout << A[i] << " " << primes[j] << " " << c << endl;
    }
  }
  /*
  rep(i, M) {
      if (factor[primes[i]] != 0) cout << primes[i] << " " << factor[primes[i]]
  << endl;
  }
  */
  ll lc = 1;
  rep(i, M) {
    rep(j, factor[primes[i]]) { lc = (lc * primes[i]) % MOD; }
  }
  // cout << lc << endl;
  ll ans = 0;
  rep(i, N) { ans = (ans + lc * mod_pow(A[i], MOD - 2, MOD)) % MOD; }
  cout << ans << endl;
}