Problem p02793 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

using int32 = int_fast32_t;
using int64 = int_fast64_t;

const int32 INF = 1e9;
const int32 MOD = 1e9 + 7;
const int64 LLINF = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n")
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n")
#define ANS(n) cout << (n) << "\n"
#define REP(i, n) for (int64 i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define FORR(i, a, b) for (int64 i = (a); i >= (b); i--)
#define ALL(obj) (obj).begin(), (obj).end()
#define pii pair<int32, int32>
#define pll pair<int64, int64>
#define pb(a) push_back(a)
#define mp make_pair

int64 modpow(int64 a, int64 r) {
  if (r == 0)
    return 1;
  if (r % 2 == 0) {
    int64 x = modpow(a, r / 2);
    return x * x % MOD;
  } else {
    return modpow(a, r - 1) * a % MOD;
  }
}

// int64 gcd(int64 a, int64 b){
//     if(b == 0)return a;
//     return gcd(b, a % b);
// }

// int64 lcm(int64 a, int64 b){
//     return a / gcd(a, b) * b;
// }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 n;
  cin >> n;
  vector<int64> a(n);
  // int64 l = 1;
  map<int32, int32> factors;
  REP(i, n) {
    cin >> a[i];
    // l = lcm(l, a[i]);
    map<int32, int32> m;
    int64 b = a[i];
    int64 p = 2;
    while (b >= 2) {
      // cout << b << endl;
      if (b % p == 0) {
        m[p]++;
        b /= p;
      } else {
        factors[p] = max(factors[p], m[p]);
        p++;
      }
    }
    factors[p] = max(factors[p], m[p]);
  }
  int64 l = 1;
  for (pii p : factors) {
    // cout << p.first << " " << p.second << endl;
    l *= modpow(p.first, p.second);
    l %= MOD;
  }
  // cout << l << endl;
  int64 ans = 0;
  REP(i, n) {
    // cout << g * modpow(a[i] , MOD-2) % MOD << endl;
    ans += l * modpow(a[i], MOD - 2) % MOD;
    ans %= MOD;
  }
  ANS(ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using int32 = int_fast32_t;
using int64 = int_fast64_t;

const int32 INF = 1e9;
const int32 MOD = 1e9 + 7;
const int64 LLINF = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n")
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n")
#define ANS(n) cout << (n) << "\n"
#define REP(i, n) for (int64 i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define FORR(i, a, b) for (int64 i = (a); i >= (b); i--)
#define ALL(obj) (obj).begin(), (obj).end()
#define pii pair<int32, int32>
#define pll pair<int64, int64>
#define pb(a) push_back(a)
#define mp make_pair

int64 modpow(int64 a, int64 r) {
  if (r == 0)
    return 1;
  if (r % 2 == 0) {
    int64 x = modpow(a, r / 2);
    return x * x % MOD;
  } else {
    return modpow(a, r - 1) * a % MOD;
  }
}

// int64 gcd(int64 a, int64 b){
//     if(b == 0)return a;
//     return gcd(b, a % b);
// }

// int64 lcm(int64 a, int64 b){
//     return a / gcd(a, b) * b;
// }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 n;
  cin >> n;
  vector<int64> a(n);
  // int64 l = 1;
  map<int32, int32> factors;
  REP(i, n) {
    cin >> a[i];
    // l = lcm(l, a[i]);
    map<int32, int32> m;
    int64 b = a[i];
    int64 p = 2;
    while (b >= 2) {
      // cout << b << endl;
      if (b % p == 0) {
        m[p]++;
        b /= p;
      } else {
        factors[p] = max(factors[p], m[p]);
        if (p * p > b) {
          p = b;
        } else {
          p++;
        }
      }
    }
    factors[p] = max(factors[p], m[p]);
  }
  int64 l = 1;
  for (pii p : factors) {
    // cout << p.first << " " << p.second << endl;
    l *= modpow(p.first, p.second);
    l %= MOD;
  }
  // cout << l << endl;
  int64 ans = 0;
  REP(i, n) {
    // cout << g * modpow(a[i] , MOD-2) % MOD << endl;
    ans += l * modpow(a[i], MOD - 2) % MOD;
    ans %= MOD;
  }
  ANS(ans);
  return 0;
}