Problem p03253 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REREP(i, n) for (int i = int(n - 1); i >= 0; i--)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define REFOR(i, m, n) for (int i = int(n - 1); i >= int(m); i--)
#define ALL(obj) (obj).begin(), (obj).end()

#define VI vector<int>
#define VVI vector<vector<int>>
#define VVVI vector<vector<vector<int>>>
#define VD vector<double>
#define VVD vector<vector<double>>
#define VVVD vector<vector<vector<double>>>
#define VL vector<ll>
#define VVL vector<vector<ll>>
#define VVVL vector<vector<vector<ll>>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define VVVB vector<vector<vector<bool>>>
#define VS vector<string>
#define VVS vector<vector<string>>
#define VVVS vector<vector<vector<string>>>

#define PII pair<int, int>
#define PDD pair<double, double>
#define PLL pair<ll, ll>
#define VPII vector<pair<int, int>>
#define VVPII vector<vector<pair<int, int>>>
#define VVVPII vector<vector<vector<pair<int, int>>>>
#define VPLL vector<pair<ll, ll>>
#define VVPLL vector<vector<pair<ll, ll>>>
#define VVVPLL vector<vector<vector<pair<ll, ll>>>>

#define SUMI(obj) accumulate((obj).begin(), (obj).end(), 0)
#define SUMD(obj) accumulate((obj).begin(), (obj).end(), 0.)
#define SUML(obj) accumulate((obj).begin(), (obj).end(), 0LL)
#define SORT(obj) sort((obj).begin(), (obj).end()) // 1,2,3,4,5...
#define RESORT(obj)                                                            \
  sort((obj).begin(), (obj).end(), greater<int>())            // 5,4,3,2,1...
#define UB(obj, n) upper_bound((obj).begin(), (obj).end(), n) // itr > n
#define LB(obj, n) lower_bound((obj).begin(), (obj).end(), n) // itr>= n

const ll HMOD = (ll)1e9 + 7;
const ll LMOD = 998244353;
const ll HINF = (ll)1e18;
const ll LINF = (ll)1e9;
const long double PI = 3.1415926535897932384626433;
const long double EPS = 1e-10;

void YN(bool flag) { cout << ((flag) ? "YES" : "NO") << endl; }

void Yn(bool flag) { cout << ((flag) ? "Yes" : "No") << endl; }

void yn(bool flag) { cout << ((flag) ? "yes" : "no") << endl; }

// Prime Factorization
vector<long long> PrimeFact(long long N) {
  vector<long long> ret;
  if (N == 1)
    ret.push_back(1);
  for (long long i = 2, M = N; i * i <= M; ++i) {
    if (N % i == 0)
      ret.push_back(i);
    while (N % i == 0)
      N /= i;
  }
  if (N != 1)
    ret.push_back(N);
  return ret;
}

// Combination Mod
class Combination_Mod {
public:
  vector<long long> fac, finv, inv;
  long long MOD;

  Combination_Mod(int N, long long mod)
      : fac(vector<long long>(N + 1)), finv(vector<long long>(N + 1)),
        inv(vector<long long>(N + 1)), MOD(mod) {
    fac[0] = fac[1] = finv[0] = finv[1] = inv[1] = 1;
    for (int i = 2; i <= N; ++i) {
      fac[i] = fac[i - 1] * i % MOD;
      inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
      finv[i] = finv[i - 1] * inv[i] % MOD;
    }
  }

  long long com(int n, int k) {
    return ((n < 0 || k < 0 || n < k)
                ? 0
                : fac[n] * (finv[k] * finv[n - k] % MOD) % MOD);
  }
};

int main() {
  ll N, M, K, maxi = 0;
  cin >> N >> M;
  VL p = PrimeFact(M);
  map<ll, ll> mp;
  K = M;
  for (auto i : p)
    for (; K % i == 0; K /= i)
      mp[i]++;
  for (auto i : mp)
    maxi = max(i.second, maxi);
  Combination_Mod CM(N + maxi, HMOD);
  ll ans = 1;
  for (auto i : mp)
    (ans *= CM.com(N - 1 + i.second, N - 1)) %= HMOD;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REREP(i, n) for (int i = int(n - 1); i >= 0; i--)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define REFOR(i, m, n) for (int i = int(n - 1); i >= int(m); i--)
#define ALL(obj) (obj).begin(), (obj).end()

#define VI vector<int>
#define VVI vector<vector<int>>
#define VVVI vector<vector<vector<int>>>
#define VD vector<double>
#define VVD vector<vector<double>>
#define VVVD vector<vector<vector<double>>>
#define VL vector<ll>
#define VVL vector<vector<ll>>
#define VVVL vector<vector<vector<ll>>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define VVVB vector<vector<vector<bool>>>
#define VS vector<string>
#define VVS vector<vector<string>>
#define VVVS vector<vector<vector<string>>>

#define PII pair<int, int>
#define PDD pair<double, double>
#define PLL pair<ll, ll>
#define VPII vector<pair<int, int>>
#define VVPII vector<vector<pair<int, int>>>
#define VVVPII vector<vector<vector<pair<int, int>>>>
#define VPLL vector<pair<ll, ll>>
#define VVPLL vector<vector<pair<ll, ll>>>
#define VVVPLL vector<vector<vector<pair<ll, ll>>>>

#define SUMI(obj) accumulate((obj).begin(), (obj).end(), 0)
#define SUMD(obj) accumulate((obj).begin(), (obj).end(), 0.)
#define SUML(obj) accumulate((obj).begin(), (obj).end(), 0LL)
#define SORT(obj) sort((obj).begin(), (obj).end()) // 1,2,3,4,5...
#define RESORT(obj)                                                            \
  sort((obj).begin(), (obj).end(), greater<int>())            // 5,4,3,2,1...
#define UB(obj, n) upper_bound((obj).begin(), (obj).end(), n) // itr > n
#define LB(obj, n) lower_bound((obj).begin(), (obj).end(), n) // itr>= n

const ll HMOD = (ll)1e9 + 7;
const ll LMOD = 998244353;
const ll HINF = (ll)1e18;
const ll LINF = (ll)1e9;
const long double PI = 3.1415926535897932384626433;
const long double EPS = 1e-10;

void YN(bool flag) { cout << ((flag) ? "YES" : "NO") << endl; }

void Yn(bool flag) { cout << ((flag) ? "Yes" : "No") << endl; }

void yn(bool flag) { cout << ((flag) ? "yes" : "no") << endl; }

// Prime Factorization
vector<long long> PrimeFact(long long N) {
  vector<long long> ret;
  if (N == 1)
    ret.push_back(1);
  for (long long i = 2, M = N; i * i <= M; ++i) {
    if (N % i == 0)
      ret.push_back(i);
    while (N % i == 0)
      N /= i;
  }
  if (N != 1)
    ret.push_back(N);
  return ret;
}

// Combination Mod
class Combination_Mod {
public:
  vector<long long> fac, finv, inv;
  long long MOD;

  Combination_Mod(int N, long long mod)
      : fac(vector<long long>(N + 1)), finv(vector<long long>(N + 1)),
        inv(vector<long long>(N + 1)), MOD(mod) {
    fac[0] = fac[1] = finv[0] = finv[1] = inv[1] = 1;
    for (int i = 2; i <= N; ++i) {
      fac[i] = fac[i - 1] * i % MOD;
      inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
      finv[i] = finv[i - 1] * inv[i] % MOD;
    }
  }

  long long com(int n, int k) {
    return ((n < 0 || k < 0 || n < k)
                ? 0
                : fac[n] * (finv[k] * finv[n - k] % MOD) % MOD);
  }
};

int main() {
  ll N, M, K, maxi = 0;
  cin >> N >> M;
  if (M == 1) {
    cout << 1 << endl;
    return 0;
  }
  VL p = PrimeFact(M);
  map<ll, ll> mp;
  K = M;
  for (auto i : p)
    for (; K % i == 0; K /= i)
      mp[i]++;
  for (auto i : mp)
    maxi = max(i.second, maxi);
  Combination_Mod CM(N + maxi, HMOD);
  ll ans = 1;
  for (auto i : mp)
    (ans *= CM.com(N - 1 + i.second, N - 1)) %= HMOD;
  cout << ans << endl;
  return 0;
}