Problem p02793 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define lli long long int
#define rep(i, a, b) for (i = a; i < b; i++)
#define M 1000000007
#define vll vector<long long int>
#define mll map<long long int, long long int>
#define pi 3.1415926535897932384626433832795028841971
using namespace std;

lli mpower(lli a, lli b, lli m) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a % m;
  lli res = 1;
  a = a % m;
  while (b) {
    if (b & 1) {
      res = (res * a) % m;
    }
    a = ((a % m) * (a % m)) % m;
    b >>= 1;
  }
  return res;
}

long long int spf[1000005];
void SPF() {
  long long int i, j;
  spf[0] = 1;
  for (i = 1; i <= 1000004; i++)
    spf[i] = i;
  for (i = 2; i <= 1000004; i++) {
    if (spf[i] == i) {
      for (j = i * i; j <= 1000004; j += i) {
        spf[j] = min(i, spf[j]);
      }
    }
  }
}

mll ma;
mll::iterator et;
lli mlcm(vll v, lli n) {
  lli i, num, factor, ans = 1;
  for (i = 0; i < n; i++) {
    num = v[i];
    mll temp;
    mll::iterator it;
    while (num > 1) {
      temp[spf[num]]++;
      num /= spf[num];
    }
    for (it = temp.begin(); it != temp.end(); ++it) {
      ma[it->first] = max(ma[it->first], it->second);
    }
  }
  for (et = ma.begin(); et != ma.end(); ++et) {
    ans = (ans * mpower(et->first, et->second, M)) % M;
  }
  return ans;
}

lli imod(lli a, lli m) {
  lli i = 1;
  a = a % m;
  for (i = 1; i < m; i++) {
    if ((a * i) % m == 1)
      return i;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  char ch, ch1, ch2;
  string s1, s2, s3;
  lli i, j, k, s, a, b, t, c, n, m, sum = 0, ans = 0, temp;
  cin >> n;
  vll v(n);
  rep(i, 0, n) cin >> v[i];
  SPF();
  a = mlcm(v, n);
  for (i = 0; i < n; i++) {
    temp = a * imod(v[i], M);
    ans += (temp);
    ans %= M;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define lli long long int
#define rep(i, a, b) for (i = a; i < b; i++)
#define M 1000000007
#define vll vector<long long int>
#define mll map<long long int, long long int>
#define pi 3.1415926535897932384626433832795028841971
using namespace std;

lli mpower(lli a, lli b, lli m) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a % m;
  lli res = 1;
  a = a % m;
  while (b) {
    if (b & 1) {
      res = (res * a) % m;
    }
    a = ((a % m) * (a % m)) % m;
    b >>= 1;
  }
  return res;
}

long long int spf[1000005];
void SPF() {
  long long int i, j;
  spf[0] = 1;
  for (i = 1; i <= 1000004; i++)
    spf[i] = i;
  for (i = 2; i <= 1000004; i++) {
    if (spf[i] == i) {
      for (j = i * i; j <= 1000004; j += i) {
        spf[j] = min(i, spf[j]);
      }
    }
  }
}

mll ma;
mll::iterator et;
lli mlcm(vll v, lli n) {
  lli i, num, factor, ans = 1;
  for (i = 0; i < n; i++) {
    num = v[i];
    mll temp;
    mll::iterator it;
    while (num > 1) {
      temp[spf[num]]++;
      num /= spf[num];
    }
    for (it = temp.begin(); it != temp.end(); ++it) {
      ma[it->first] = max(ma[it->first], it->second);
    }
  }
  for (et = ma.begin(); et != ma.end(); ++et) {
    ans = (ans * mpower(et->first, et->second, M)) % M;
  }
  return ans;
}

lli imod(lli a, lli m) {
  lli g;
  return mpower(a, m - 2, m);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  char ch, ch1, ch2;
  string s1, s2, s3;
  lli i, j, k, s, a, b, t, c, n, m, sum = 0, ans = 0, temp;
  cin >> n;
  vll v(n);
  rep(i, 0, n) cin >> v[i];
  SPF();
  a = mlcm(v, n);
  for (i = 0; i < n; i++) {
    temp = a * imod(v[i], M);
    ans += (temp);
    ans %= M;
  }
  cout << ans << endl;

  return 0;
}