Problem p02793 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(a, b, c) for (int a = b; a <= c; a++)
#define per(a, b, c) for (int a = b; a >= c; a--)
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define eps 1e-11
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef double db;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
inline ll qsm(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
int n, m, k, rt;
int isp[N], num[N], cnt;
ll a[N], b[N];
ll inv[N];
void prime() {
  isp[cnt++] = 2;
  for (int i = 2; i < N; i++) {
    int flag = 0;
    for (int j = 2; j <= sqrt(i * 1.0) + 1; j++) {
      if (i % j == 0) {
        flag = 1;
        break;
      }
    }
    if (!flag)
      isp[cnt++] = i;
  }
}

int main() {
  prime();
  inv[1] = 1;
  rep(i, 2, 1000000) inv[i] = inv[mod % i] * (mod - mod / i) % mod;
  scanf("%d", &n);
  int bitgest;
  rep(i, 1, n) {
    scanf("%lld", &a[i]);
    b[i] = a[i];
    int tot;
    rep(j, 0, cnt - 1) {
      tot = 0;
      while (a[i] % isp[j] == 0) {
        a[i] /= isp[j];
        tot++;
      }
      if (tot > num[j])
        num[j] = tot;
    }
    if (a[i] > 1) {
      num[cnt] = 1;
      isp[cnt] = a[i];
      ++cnt;
    }
  }
  ll res = 1;
  rep(i, 0, cnt - 1) {
    if (num[i]) {
      rep(j, 1, num[i]) res = res * isp[i] % mod;
    }
  }
  ll ans = 0;
  rep(i, 1, n) ans = (ans + res % mod * inv[b[i]] % mod) % mod;
  printf("%lld\n", ans);
  return 0;
}
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//                     佛祖保佑      永无BUG


Fixed submission:
#include <bits/stdc++.h>
#define rep(a, b, c) for (int a = b; a <= c; a++)
#define per(a, b, c) for (int a = b; a >= c; a--)
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define eps 1e-11
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef double db;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
inline ll qsm(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
int n, m, k, rt;
int isp[N], num[N], cnt;
ll a[N], b[N];
ll inv[N];
void prime() {
  isp[cnt++] = 2;
  for (int i = 2; i < 1000; i++) {
    int flag = 0;
    for (int j = 2; j <= sqrt(i * 1.0) + 1; j++) {
      if (i % j == 0) {
        flag = 1;
        break;
      }
    }
    if (!flag)
      isp[cnt++] = i;
  }
}

int main() {
  prime();
  inv[1] = 1;
  rep(i, 2, 1000000) inv[i] = inv[mod % i] * (mod - mod / i) % mod;
  scanf("%d", &n);
  int bitgest;
  rep(i, 1, n) {
    scanf("%lld", &a[i]);
    b[i] = a[i];
    int tot;
    rep(j, 0, cnt - 1) {
      tot = 0;
      while (a[i] % isp[j] == 0) {
        a[i] /= isp[j];
        tot++;
      }
      if (tot > num[j])
        num[j] = tot;
    }
    if (a[i] > 1) {
      num[cnt] = 1;
      isp[cnt] = a[i];
      ++cnt;
    }
  }
  ll res = 1;
  rep(i, 0, cnt - 1) {
    if (num[i]) {
      rep(j, 1, num[i]) res = res * isp[i] % mod;
    }
  }
  ll ans = 0;
  rep(i, 1, n) ans = (ans + res % mod * inv[b[i]] % mod) % mod;
  printf("%lld\n", ans);
  return 0;
}
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//                     佛祖保佑      永无BUG