Problem p02793 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "algorithm"
#include "cmath"
#include "cstring"
#include "iostream"
#include "math.h"
#include "queue"
#include "set"
#include "stack"
#include "string"
#include "vector"

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<vector<ll>> mat;

const ll mod = 1e9 + 7;

void decompPrimepair(ll n, vector<pll> &ans) {
  ll check = 0;
  while (n % 2 == 0) {
    check++;
    n /= 2;
  }
  ans.push_back(pll{2, check});

  ll m = sqrt(n);
  ll i = 3;
  while (true) {
    if (n == 1) {
      break;
    }
    if (m * m > n) {
      ans.push_back(pll{n, 1});
      break;
    }
    if (n % i == 0) {
      ll check = 0;
      while (n % i == 0) {
        check++;
        n /= i;
        m = sqrt(n);
      }
      ans.push_back(pll{i, check});
    }
    i += 2;
  }
}

ll power_ll_p(ll a, ll b, ll p) // F_pでのべき
{
  if (b > p) {
    b %= (p - 1); // 位数p-1
  }
  if (b == 0) {
    return 1;
  }
  if (b % 2 == 0) {
    ll t = power_ll_p(a, b / 2, p);
    return t * t % p;
  } else {
    return a * power_ll_p(a, b - 1, p) % p;
  }
}

ll extgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    ll q = a / b;
    ll g = extgcd(b, a - q * b, x, y);
    ll z = x - q * y;
    x = y;
    y = z;
    return g;
  }
}

// modp逆元
ll inversemod(ll a, ll p) {
  ll x, y;
  extgcd(a, p, x, y);
  x %= p;
  if (x < 0) {
    x += p;
  }
  return x;
}

int main() {
  int n;
  cin >> n;
  ll a[10010], copy[10010];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    copy[i] = a[i];
  }
  sort(a, a + n);
  vector<pll> v(0);
  decompPrimepair(a[0], v);
  for (int i = 1; i < n; i++) {
    int size = v.size();
    for (int j = 0; j < size; j++) {
      ll check = 0;
      ll p = v[j].first;
      while (a[i] % p == 0) {
        check++;
        a[i] /= p;
      }
      v[j].second = max(check, v[j].second);
    }
    vector<pll> u(0);
    decompPrimepair(a[i], u);
    v.insert(v.end(), u.begin(), u.end());
  }

  ll l = 1;
  int size = v.size();
  for (int j = 0; j < size; j++) {
    l *= power_ll_p(v[j].first, v[j].second, mod);
    l %= mod;
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans += inversemod(copy[i], mod);
    ans %= mod;
  }
  ans *= l;
  ans %= mod;
  cout << ans << "\n";
}

Fixed submission:
#include "algorithm"
#include "cmath"
#include "cstring"
#include "iostream"
#include "math.h"
#include "queue"
#include "set"
#include "stack"
#include "string"
#include "vector"

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<vector<ll>> mat;

const ll mod = 1e9 + 7;

void decompPrimepair(ll n, vector<pll> &ans) {
  ll check = 0;
  while (n % 2 == 0) {
    check++;
    n /= 2;
  }
  ans.push_back(pll{2, check});

  ll m = sqrt(n);
  ll i = 3;
  while (true) {
    if (n == 1) {
      break;
    }
    if (i > m) {
      ans.push_back(pll{n, 1});
      break;
    }
    if (n % i == 0) {
      ll check = 0;
      while (n % i == 0) {
        check++;
        n /= i;
        m = sqrt(n);
      }
      ans.push_back(pll{i, check});
    }
    i += 2;
  }
}

ll power_ll_p(ll a, ll b, ll p) // F_pでのべき
{
  if (b > p) {
    b %= (p - 1); // 位数p-1
  }
  if (b == 0) {
    return 1;
  }
  if (b % 2 == 0) {
    ll t = power_ll_p(a, b / 2, p);
    return t * t % p;
  } else {
    return a * power_ll_p(a, b - 1, p) % p;
  }
}

ll extgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    ll q = a / b;
    ll g = extgcd(b, a - q * b, x, y);
    ll z = x - q * y;
    x = y;
    y = z;
    return g;
  }
}

// modp逆元
ll inversemod(ll a, ll p) {
  ll x, y;
  extgcd(a, p, x, y);
  x %= p;
  if (x < 0) {
    x += p;
  }
  return x;
}

int main() {
  int n;
  cin >> n;
  ll a[10010], copy[10010];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    copy[i] = a[i];
  }
  sort(a, a + n);
  vector<pll> v(0);
  decompPrimepair(a[0], v);
  for (int i = 1; i < n; i++) {
    int size = v.size();
    for (int j = 0; j < size; j++) {
      ll check = 0;
      ll p = v[j].first;
      while (a[i] % p == 0) {
        check++;
        a[i] /= p;
      }
      v[j].second = max(check, v[j].second);
    }
    vector<pll> u(0);
    decompPrimepair(a[i], u);
    v.insert(v.end(), u.begin(), u.end());
  }

  ll l = 1;
  int size = v.size();
  for (int j = 0; j < size; j++) {
    l *= power_ll_p(v[j].first, v[j].second, mod);
    l %= mod;
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans += inversemod(copy[i], mod);
    ans %= mod;
  }
  ans *= l;
  ans %= mod;
  cout << ans << "\n";
}