Problem p02793 (C++)

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

#define DIV 1000000007
#define MAX 1000001

long long inv[MAX];

int mypow(int x, int n) {
  long long ret = 1, p;
  int p2 = 1;
  p = x;
  while (1) {
    if (n & p2)
      ret *= p;
    p *= p;
    ret %= DIV;
    p %= DIV;
    p2 *= 2;
    if (p2 > n)
      break;
  }
  return ret;
}

void init() {
  inv[0] = 1;
  for (int i = 1; i < MAX; i++) {
    inv[i] = mypow(i, DIV - 2) % DIV;
  }
}

void pf_init(vector<int> &pf, int maxsize) {
  bool flag;
  pf.resize(0);
  for (int i = 2; i * i <= maxsize; i++) {
    flag = 1;
    for (int j = 0; j < pf.size(); j++) {
      if (i % pf.at(j) == 0) {
        flag = 0;
        break;
      }
    }
    if (flag) {
      pf.push_back(i);
    }
  }
}

void pf_make(int n, vector<int> &ret, vector<int> &pf) {
  if (ret.size() != pf.size())
    ret.resize(pf.size());
  for (int i = 0; i < pf.size(); i++) {
    ret.at(i) = 0;
    while (1) {
      if (n % pf.at(i) == 0) {
        n /= pf.at(i);
        ret.at(i)++;
      } else {
        break;
      }
    }
  }
  if (n > 1) {
    pf.push_back(n);
    ret.push_back(1);
  }
}

int main() {
  init();
  vector<int> pf;
  pf_init(pf, MAX);
  int N;
  cin >> N;
  int A[N];
  vector<vector<int>> pf_done_A(N, vector<int>(pf.size()));
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    pf_make(A[i], pf_done_A.at(i), pf);
  }
  vector<int> LCM(pf.size(), 0);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < pf.size(); j++) {
      LCM.at(j) = max(LCM.at(j), pf_done_A.at(i).at(j));
    }
  }
  long long L = 1;
  for (int i = 0; i < pf.size(); i++) {
    L *= mypow(pf.at(i), LCM.at(i));
    L %= DIV;
  }
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    ans += L * inv[A[i]];
    ans %= DIV;
  }
  cout << ans;
  return 0;
}


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

#define DIV 1000000007
#define MAX 1000001

long long inv[MAX];

int mypow(int x, int n) {
  long long ret = 1, p;
  int p2 = 1;
  p = x;
  while (1) {
    if (n & p2)
      ret *= p;
    p *= p;
    ret %= DIV;
    p %= DIV;
    p2 *= 2;
    if (p2 > n)
      break;
  }
  return ret;
}

void init() {
  inv[0] = 1;
  for (int i = 1; i < MAX; i++) {
    inv[i] = mypow(i, DIV - 2) % DIV;
  }
}

void pf_init(vector<int> &pf, int maxsize) {
  bool flag;
  pf.resize(0);
  for (int i = 2; i * i <= maxsize; i++) {
    flag = 1;
    for (int j = 0; j < pf.size(); j++) {
      if (i % pf.at(j) == 0) {
        flag = 0;
        break;
      }
    }
    if (flag) {
      pf.push_back(i);
    }
  }
}

void pf_make(int n, vector<int> &ret, vector<int> &pf) {
  if (ret.size() != pf.size())
    ret.resize(pf.size());
  for (int i = 0; i < pf.size(); i++) {
    ret.at(i) = 0;
    while (1) {
      if (n % pf.at(i) == 0) {
        n /= pf.at(i);
        ret.at(i)++;
      } else {
        break;
      }
    }
  }
  if (n > 1) {
    pf.push_back(n);
    ret.push_back(1);
  }
}

int main() {
  init();
  vector<int> pf;
  pf_init(pf, MAX);
  int N;
  cin >> N;
  int A[N];
  vector<vector<int>> pf_done_A(N, vector<int>(pf.size()));
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    pf_make(A[i], pf_done_A.at(i), pf);
  }
  vector<int> LCM(pf.size(), 0);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < pf_done_A.at(i).size(); j++) {
      LCM.at(j) = max(LCM.at(j), pf_done_A.at(i).at(j));
    }
  }
  long long L = 1;
  for (int i = 0; i < pf.size(); i++) {
    L *= mypow(pf.at(i), LCM.at(i));
    L %= DIV;
  }
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    ans += L * inv[A[i]];
    ans %= DIV;
  }
  cout << ans;
  return 0;
}