Problem p02793 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using PL = pair<ll, ll>;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define IFOR(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define RPT(i, a, b) for (int i = (a); i < ((a) + (b)); ++i)
#define IRPT(i, a, b) for (int i = ((a) + (b)-1); i >= (a); --i)
constexpr int INF = (1 << 29);
constexpr ll INF_ = (1LL << 58);
constexpr ld EPS = (1e-10);
constexpr ld PI_ = 3.141592653589793238; // 46264338327950288
constexpr ld E_ = 2.71828182845904523;   // 5360287471352
constexpr ll MOD = 1000000007;
//// functions (integer)_________________________________
ll modpow(ll a, ll n, ll m) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = res * a % m;
    a = a * a % m;
    n >>= 1;
  }
  return res;
}
long long modinv(long long a, long long p, long long mod) {
  return modpow(a, p - 2, mod);
}
////-----------------------------------------------------

int main() {
  int N;
  vector<ll> A;
  set<ll> P;
  map<ll, ll> B;
  cin >> N;
  RPT(i, 0, N) {
    ll a;
    cin >> a;
    A.push_back(a);
  }
  sort(A.begin(), A.end());

  FOR(i, 2, 1000003) {
    bool ynprime = true;
    for (auto x : P) {
      if (i % x == 0) {
        ynprime = false;
        break;
      }
      if (x * x > i)
        break;
    }
    if (ynprime)
      P.insert(i);
  }

  RPT(i, 0, N) {
    ll ttl = A[i];
    for (auto x : P) {
      ll cnt = 0;
      if (x > A[i] || ttl == 1)
        break;
      while (ttl % x == 0) {
        ttl /= x;
        cnt++;
      }
      if (cnt) {
        if (B[x] < cnt)
          B[x] = cnt;
      }
    }
    if (ttl != 1) {
      P.insert(A[i]);
      B[A[i]] = 1;
    }
  }

  ll L = 1, Ainv = 0;
  for (auto x : P) {
    L = ((L * (modpow(x, B[x], MOD)) % MOD)) % MOD;
  }
  RPT(i, 0, N) {
    Ainv = (Ainv + ((modpow(A[i], MOD - 2, MOD) % MOD) % MOD)) % MOD;
  }
  ll ans = (Ainv * L) % MOD;
  // ll ans = (Ainv % MOD);
  cout << ans << endl;
  // cout << L << endl << Ainv << endl;for(auto x: P) if(B[x]) cout << x << "^"
  // << B[x] << " ";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using PL = pair<ll, ll>;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define IFOR(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define RPT(i, a, b) for (int i = (a); i < ((a) + (b)); ++i)
#define IRPT(i, a, b) for (int i = ((a) + (b)-1); i >= (a); --i)
constexpr int INF = (1 << 29);
constexpr ll INF_ = (1LL << 58);
constexpr ld EPS = (1e-10);
constexpr ld PI_ = 3.141592653589793238; // 46264338327950288
constexpr ld E_ = 2.71828182845904523;   // 5360287471352
constexpr ll MOD = 1000000007;
//// functions (integer)_________________________________
ll modpow(ll a, ll n, ll m) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = res * a % m;
    a = a * a % m;
    n >>= 1;
  }
  return res;
}
long long modinv(long long a, long long p, long long mod) {
  return modpow(a, p - 2, mod);
}
////-----------------------------------------------------

int main() {
  int N;
  vector<ll> A;
  set<ll> P;
  map<ll, ll> B;
  cin >> N;
  RPT(i, 0, N) {
    ll a;
    cin >> a;
    A.push_back(a);
  }
  sort(A.begin(), A.end());

  FOR(i, 2, 1000003) {
    bool ynprime = true;
    for (auto x : P) {
      if (i % x == 0) {
        ynprime = false;
        break;
      }
      if (x * x > i)
        break;
    }
    if (ynprime)
      P.insert(i);
  }

  RPT(i, 0, N) {
    ll ttl = A[i];
    set<ll>::iterator it = P.find(A[i]);
    if (it != P.end()) {
      ttl = 1;
      if (B[A[i]] < 1)
        B[A[i]] = 1;
    }
    for (auto x : P) {
      ll cnt = 0;
      if (x > A[i] || ttl == 1)
        break;
      while (ttl % x == 0) {
        ttl /= x;
        cnt++;
      }
      if (cnt) {
        if (B[x] < cnt)
          B[x] = cnt;
      }
    }
    if (ttl != 1) {
      P.insert(A[i]);
      B[A[i]] = 1;
    }
  }

  ll L = 1, Ainv = 0;
  for (auto x : P) {
    L = ((L * (modpow(x, B[x], MOD)) % MOD)) % MOD;
  }
  RPT(i, 0, N) {
    Ainv = (Ainv + ((modpow(A[i], MOD - 2, MOD) % MOD) % MOD)) % MOD;
  }
  ll ans = (Ainv * L) % MOD;
  // ll ans = (Ainv % MOD);
  cout << ans << endl;
  // cout << L << endl << Ainv << endl;for(auto x: P) if(B[x]) cout << x << "^"
  // << B[x] << " ";
  return 0;
}