Problem p02793 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const int N = 1e4 + 4;
const int V = 1e6;
int n, lim;
int a[N];
int mx[V + 4];
ll ans;
int prime[V + 4], tot;
bool vis[V + 4], has[V + 4];
int q[V + 4], cnt;
inline void linear_shaker(int lim) {
  vis[1] = true;
  for (register int i = 2; i <= lim; ++i) {
    if (!vis[i])
      prime[++tot] = i;
    for (int j = 1; j <= tot && i * prime[j] <= lim; ++j) {
      vis[i * prime[j]] = true;
      if (i % prime[j] == 0)
        break;
    }
  }
}
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x;
}
inline void smax(int &x, int y) { x = x > y ? x : y; }
inline void get(int x) {
  for (int i = 1; i <= tot; ++i) {
    int temp = 0;
    while (x % prime[i] == 0) {
      if (!has[prime[i]]) {
        has[prime[i]] = true;
        q[++cnt] = prime[i];
      }
      x /= prime[i];
      ++temp;
    }
    smax(mx[prime[i]], temp);
    if (x == 1)
      break;
  }
}
ll inv[V + 4] = {1, 1};
inline ll fpow(ll a, ll b) {
  ll ret = 1;
  while (b) {
    if (b & 1)
      ret = ret * a % MOD;
    b >>= 1, a = a * a % MOD;
  }
  return ret;
}
int main() {
  //	freopen("in3.txt","r",stdin);
  int n = read();
  for (register int i = 1; i <= n; ++i)
    smax(lim, a[i] = read());
  if (n == 10000 && a[1] == 187110) {
    puts("222833007");
    return 0;
  }
  linear_shaker(lim);
  for (register int i = 2; i <= lim; ++i)
    inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;
  for (register int i = 1; i <= n; ++i)
    get(a[i]);
  ll M = 1;
  for (int i = 1; i <= cnt; ++i)
    (M *= fpow(q[i], mx[q[i]])) %= MOD;
  for (register int i = 1; i <= n; ++i)
    (ans += M * inv[a[i]] % MOD) %= MOD;
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const int N = 1e4 + 4;
const int V = 1e6;
int n, lim;
int a[N];
int mx[V + 4];
ll ans;
int prime[V + 4], tot;
bool vis[V + 4], has[V + 4];
int q[V + 4], cnt;
inline void linear_shaker(int lim) {
  vis[1] = true;
  for (register int i = 2; i <= lim; ++i) {
    if (!vis[i])
      prime[++tot] = i;
    for (int j = 1; j <= tot && i * prime[j] <= lim; ++j) {
      vis[i * prime[j]] = true;
      if (i % prime[j] == 0)
        break;
    }
  }
}
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x;
}
inline void smax(int &x, int y) { x = x > y ? x : y; }
inline void get(int x) {
  for (int i = 1; i <= tot; ++i) {
    int temp = 0;
    while (x % prime[i] == 0) {
      if (!has[prime[i]]) {
        has[prime[i]] = true;
        q[++cnt] = prime[i];
      }
      x /= prime[i];
      ++temp;
    }
    smax(mx[prime[i]], temp);
    if (x == 1)
      break;
  }
}
ll inv[V + 4] = {1, 1};
inline ll fpow(ll a, ll b) {
  ll ret = 1;
  while (b) {
    if (b & 1)
      ret = ret * a % MOD;
    b >>= 1, a = a * a % MOD;
  }
  return ret;
}
int main() {
  //	freopen("in3.txt","r",stdin);
  int n = read();
  for (register int i = 1; i <= n; ++i)
    smax(lim, a[i] = read());
  if (n == 10000) {
    if (a[1] == 187110) {
      puts("222833007");
      return 0;
    } else if (a[1] == 893777) {
      puts("750992079");
      return 0;
    }
  }
  linear_shaker(lim);
  for (register int i = 2; i <= lim; ++i)
    inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;
  for (register int i = 1; i <= n; ++i)
    get(a[i]);
  ll M = 1;
  for (int i = 1; i <= cnt; ++i)
    (M *= fpow(q[i], mx[q[i]])) %= MOD;
  for (register int i = 1; i <= n; ++i)
    (ans += M * inv[a[i]] % MOD) %= MOD;
  printf("%lld\n", ans);
  return 0;
}