Problem p02793 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define F(i, a, b) for (int i = a; i <= (b); ++i)
#define dF(i, a, b) for (int i = a; i >= (b); --i)
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3f
#define pb push_back
#define maxnkp make_pair
#define fi first
#define se second
#define eps 1e-6
using namespace std;
typedef long long ll;
const int mods = 1e9 + 7;
const int maxn = 1e4 + 10;
const int N = 1e6 + 10;

int n;
int a[maxn];
int num[N];
int prime[N]; // 下标从1开始
bool not_prime[N];

int sieve(int n) { // 把2到n内所有素数筛选出来
  int p = 0;
  for (int i = 2; i <= n; ++i) {
    if (!not_prime[i])
      prime[++p] = i;
    for (int j = 1; j <= p && i * prime[j] <= n; j++) {
      not_prime[i * prime[j]] = true; // 把比最小素因子的素数的倍数筛除
      if (i % prime[j] == 0)
        break; // 已找到最小素因子
    }
  }
  return p; //  返回素数个数
}

void pri_fac(ll x) {
  for (int i = 1; prime[i] <= x; i++) {
    int y = prime[i];
    if (x % y == 0) {
      int p = 0;
      while (x % y == 0) {
        x /= y;
        p++;
      }
      num[y] = max(p, num[y]);
    }
  }
  if (x > 1)
    num[x] = max(1, num[x]);
}

ll qpow(ll n, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * n % mods;
    n = n * n % mods;
    b >>= 1;
  }
  return res;
}

int main() {
  scanf("%d", &n);
  int maa = 0;
  sieve(N);
  F(i, 1, n) {
    scanf("%d", &a[i]);
    // a[i] = rand()%1000000+1;
    maa = max(a[i], maa);
    pri_fac(a[i]);
  }
  ll sum = 1;
  F(i, 1, maa) { sum = sum * qpow(i, num[i]) % mods; }
  int ans = 0;
  F(i, 1, n) { ans = (ans + qpow(a[i], mods - 2)) % mods; }
  ans = ans * sum % mods;
  printf("%d\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
#define F(i, a, b) for (int i = a; i <= (b); ++i)
#define dF(i, a, b) for (int i = a; i >= (b); --i)
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3f
#define pb push_back
#define maxnkp make_pair
#define fi first
#define se second
#define eps 1e-6
using namespace std;
typedef long long ll;
const int mods = 1e9 + 7;
const int maxn = 1e4 + 10;
const int N = 1e6 + 10;

int n;
int a[maxn];
int num[N];
int prime[N]; // 下标从1开始
bool not_prime[N];

int sieve(int n) { // 把2到n内所有素数筛选出来
  int p = 0;
  for (int i = 2; i <= n; ++i) {
    if (!not_prime[i])
      prime[++p] = i;
    for (int j = 1; j <= p && i * prime[j] <= n; j++) {
      not_prime[i * prime[j]] = true; // 把比最小素因子的素数的倍数筛除
      if (i % prime[j] == 0)
        break; // 已找到最小素因子
    }
  }
  return p; //  返回素数个数
}

void pri_fac(ll x) {
  for (int i = 1; prime[i] * prime[i] <= x; i++) {
    int y = prime[i];
    if (x % y == 0) {
      int p = 0;
      while (x % y == 0) {
        x /= y;
        p++;
      }
      num[y] = max(p, num[y]);
    }
  }
  if (x > 1)
    num[x] = max(1, num[x]);
}

ll qpow(ll n, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * n % mods;
    n = n * n % mods;
    b >>= 1;
  }
  return res;
}

int main() {
  scanf("%d", &n);
  int maa = 0;
  sieve(N);
  F(i, 1, n) {
    scanf("%d", &a[i]);
    // a[i] = rand()%1000000+1;
    maa = max(a[i], maa);
    pri_fac(a[i]);
  }
  ll sum = 1;
  F(i, 1, maa) { sum = sum * qpow(i, num[i]) % mods; }
  int ans = 0;
  F(i, 1, n) { ans = (ans + qpow(a[i], mods - 2)) % mods; }
  ans = ans * sum % mods;
  printf("%d\n", ans);
}