Problem p02793 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

vector<int> prime;

void setPrime() {
  for (int i = 2; i <= 1000; i++) {
    bool ok = true;
    for (int e : prime) {
      if (i % e == 0) {
        ok = false;
        break;
      }
    }
    if (ok)
      prime.push_back(i);
  }
}

int main() {
  int n;
  cin >> n;
  mint ans(0);
  setPrime();
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  map<int, int> m;
  rep(i, n) {
    int j = 0;
    int v = a[i];
    map<int, int> temp;
    while (prime[j] * prime[j] <= v) {
      if (v % prime[j] == 0) {
        v /= prime[j];
        temp[prime[j]]++;
      } else {
        j++;
      }
    }
    temp[v]++;
    for (auto t : temp) {
      if (m[t.first] < t.second) {
        m[t.first] = t.second;
      }
    }
  }

  mint lcm(1);
  for (auto e : m) {
    rep(i, e.second) { lcm *= mint(e.first); }
  }

  rep(i, n) {
    mint v = mint(a[i]);
    ans += (lcm / v);
  }

  cout << ans.x << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

vector<int> prime;

void setPrime() {
  for (int i = 2; i <= 1000; i++) {
    bool ok = true;
    for (int e : prime) {
      if (i % e == 0) {
        ok = false;
        break;
      }
    }
    if (ok)
      prime.push_back(i);
  }
}

int main() {
  int n;
  cin >> n;
  mint ans(0);
  setPrime();
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  map<int, int> m;
  rep(i, n) {
    int j = 0;
    int v = a[i];
    map<int, int> temp;
    while (j < prime.size() && prime[j] * prime[j] <= v) {
      if (v % prime[j] == 0) {
        v /= prime[j];
        temp[prime[j]]++;
      } else {
        j++;
      }
    }
    temp[v]++;
    for (auto t : temp) {
      if (m[t.first] < t.second) {
        m[t.first] = t.second;
      }
    }
  }

  mint lcm(1);
  for (auto e : m) {
    rep(i, e.second) { lcm *= mint(e.first); }
  }

  rep(i, n) {
    mint v = mint(a[i]);
    ans += (lcm / v);
  }

  cout << ans.x << endl;
  return 0;
}