Problem p02793 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const long long int mod = 1000000007;
const long long int INF = 99999999999999999;

// ユークリッドの互除法　a,bは最大公約数を求めたい２つの数
class Euclid_Gojyohou {
public:
  long long int gcd(long long int a, long long int b) {
    long long int tmp;
    long long int r = 1;
    if (b > a) {
      tmp = a;
      a = b;
      b = tmp;
    }
    r = a % b;
    while (r != 0) {

      a = b;
      b = r;
      r = a % b;
    }
    return b;
  }
};

// 階乗先に計算
class Kaizyou {
public:
  long long int *kaizyou;
  Kaizyou(){};
  Kaizyou(long long int n, long long int mod) {
    kaizyou = new long long int[n + 1];
    kai(1, n, mod);
    return;
  }

  long long int kai(long long int a, long long int b, long long int mod) {
    long long int tmp = 1;
    for (long long int i = a; i <= b; i++) {
      tmp *= i;
      tmp %= mod;

      kaizyou[i] = tmp;
    }
    return tmp;
  }
};

// 累乗(繰り返し2乗法)　aのb乗(mod)を求める。
class Ruizyou {
public:
  long long int rui(long long int a, long long int b, long long mod) {
    int memo[65] = {};
    long long int A[65] = {};
    long long int tmp = 1;
    for (int i = 0; i < 65; i++) {
      memo[i] = b % 2;
      b /= 2;
    }

    A[0] = a;
    A[0] %= mod;

    for (int i = 1; i < 65; i++) {
      A[i] = A[i - 1] * A[i - 1];
      A[i] %= mod;
    }
    for (int i = 0; i < 65; i++) {
      if (memo[i] == 1) {
        tmp *= A[i];
        tmp %= mod;
      }
    }
    tmp %= mod;
    return tmp;
  }
};

long long int LCMs[1000005] = {};
int main() {
  cout << fixed << setprecision(18);
  long long int N, A[1005], lcm = 1, res = 0, tmp2;
  cin >> N;
  Euclid_Gojyohou euc;
  Ruizyou ruizyou;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    long long int tmp = A[i];
    tmp2 = tmp;
    for (long long int j = 2; j * j <= tmp2; j++) {
      long long int Counter = 0;
      while (tmp % j == 0) {
        Counter++;
        tmp /= j;
      }
      LCMs[j] = max(LCMs[j], Counter);
    }
    if (tmp != 1) {
      LCMs[tmp] = max(LCMs[tmp], 1LL);
    }
  }
  for (long long int i = 2; i <= 1000000; i++) {
    for (long long int j = 0; j < LCMs[i]; j++) {
      lcm = i * lcm;
      lcm %= mod;
    }
  }
  for (int i = 0; i < N; i++) {
    res += lcm * ruizyou.rui(A[i], mod - 2, mod);
    res %= mod;
  }
  cout << res;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const long long int mod = 1000000007;
const long long int INF = 99999999999999999;

// ユークリッドの互除法　a,bは最大公約数を求めたい２つの数
class Euclid_Gojyohou {
public:
  long long int gcd(long long int a, long long int b) {
    long long int tmp;
    long long int r = 1;
    if (b > a) {
      tmp = a;
      a = b;
      b = tmp;
    }
    r = a % b;
    while (r != 0) {

      a = b;
      b = r;
      r = a % b;
    }
    return b;
  }
};

// 階乗先に計算
class Kaizyou {
public:
  long long int *kaizyou;
  Kaizyou(){};
  Kaizyou(long long int n, long long int mod) {
    kaizyou = new long long int[n + 1];
    kai(1, n, mod);
    return;
  }

  long long int kai(long long int a, long long int b, long long int mod) {
    long long int tmp = 1;
    for (long long int i = a; i <= b; i++) {
      tmp *= i;
      tmp %= mod;

      kaizyou[i] = tmp;
    }
    return tmp;
  }
};

// 累乗(繰り返し2乗法)　aのb乗(mod)を求める。
class Ruizyou {
public:
  long long int rui(long long int a, long long int b, long long mod) {
    int memo[65] = {};
    long long int A[65] = {};
    long long int tmp = 1;
    for (int i = 0; i < 65; i++) {
      memo[i] = b % 2;
      b /= 2;
    }

    A[0] = a;
    A[0] %= mod;

    for (int i = 1; i < 65; i++) {
      A[i] = A[i - 1] * A[i - 1];
      A[i] %= mod;
    }
    for (int i = 0; i < 65; i++) {
      if (memo[i] == 1) {
        tmp *= A[i];
        tmp %= mod;
      }
    }
    tmp %= mod;
    return tmp;
  }
};

long long int LCMs[1000005] = {};
int main() {
  cout << fixed << setprecision(18);
  long long int N, A[10005], lcm = 1, res = 0, tmp2;
  cin >> N;
  Euclid_Gojyohou euc;
  Ruizyou ruizyou;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    long long int tmp = A[i];
    tmp2 = tmp;
    for (long long int j = 2; j * j <= tmp2; j++) {
      long long int Counter = 0;
      while (tmp % j == 0) {
        Counter++;
        tmp /= j;
      }
      LCMs[j] = max(LCMs[j], Counter);
    }
    if (tmp != 1) {
      LCMs[tmp] = max(LCMs[tmp], 1LL);
    }
  }
  for (long long int i = 2; i <= 1000000; i++) {
    for (long long int j = 0; j < LCMs[i]; j++) {
      lcm = i * lcm;
      lcm %= mod;
    }
  }
  for (int i = 0; i < N; i++) {
    res += lcm * ruizyou.rui(A[i], mod - 2, mod);
    res %= mod;
  }
  cout << res;
}