Problem p03253 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

map<int, int> divisor_count;
bool is_prime[1000];
const long long MOD = 1000000007;
const int MAX = 200001;
long long fac[MAX];
long long finv[MAX];
long long inv[MAX];

int BIT_SIZE = 64;
long long power(long long m, long long n) {
  long long ans = 1;
  long long tmp = m;
  for (int i = 0; i < BIT_SIZE; i++) {
    // cout << ans << endl;
    if (((n >> i) & 1) == 1) {
      ans = ans * tmp % MOD;
    }
    tmp = (tmp * tmp) % MOD;
  }
  return ans;
}

void comb_init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; 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 comb(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  int N;
  int M;
  cin >> N >> M;

  comb_init();

  // set prime array
  for (int i = 0; i < int(sqrt(M)) + 1; i++) {
    is_prime[i] = true;
  }
  for (int i = 2; i < int(sqrt(M)) + 1; i++) {
    if (!is_prime[i]) {
      continue;
    }
    for (int j = 2 * i; j < int(sqrt(M)) + 1; j += i) {
      is_prime[j] = false;
    }
  }
  /*
  for(int i = 0; i <int(sqrt(M)) + 1; i++){
      cout << is_prime[i] << " ";
  }
  cout << endl;
  */

  // get prime divisors of M
  int tmp = M;
  for (int i = 2; i < int(sqrt(M)) + 1; i++) {
    if (!is_prime[i]) {
      continue;
    }
    if (tmp % i == 0 && divisor_count.find(i) == divisor_count.end()) {
      divisor_count[i] = 0;
    }
    while (tmp % i == 0) {
      tmp /= i;
      divisor_count[i] += 1;
    }
  }
  if (tmp != 1) {
    divisor_count[tmp] = 1;
  }

  long long ans = 1;
  for (auto it = divisor_count.begin(); it != divisor_count.end(); it++) {
    ans =
        (ans * comb((long long)(N - 1) + (long long)(it->second), N - 1)) % MOD;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

map<int, int> divisor_count;
bool is_prime[100000];
const long long MOD = 1000000007;
const int MAX = 200001;
long long fac[MAX];
long long finv[MAX];
long long inv[MAX];

int BIT_SIZE = 64;
long long power(long long m, long long n) {
  long long ans = 1;
  long long tmp = m;
  for (int i = 0; i < BIT_SIZE; i++) {
    // cout << ans << endl;
    if (((n >> i) & 1) == 1) {
      ans = ans * tmp % MOD;
    }
    tmp = (tmp * tmp) % MOD;
  }
  return ans;
}

void comb_init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; 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 comb(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  int N;
  int M;
  cin >> N >> M;

  comb_init();

  // set prime array
  for (int i = 0; i < int(sqrt(M)) + 1; i++) {
    is_prime[i] = true;
  }
  for (int i = 2; i < int(sqrt(M)) + 1; i++) {
    if (!is_prime[i]) {
      continue;
    }
    for (int j = 2 * i; j < int(sqrt(M)) + 1; j += i) {
      is_prime[j] = false;
    }
  }
  /*
  for(int i = 0; i <int(sqrt(M)) + 1; i++){
      cout << is_prime[i] << " ";
  }
  cout << endl;
  */

  // get prime divisors of M
  int tmp = M;
  for (int i = 2; i < int(sqrt(M)) + 1; i++) {
    if (!is_prime[i]) {
      continue;
    }
    if (tmp % i == 0 && divisor_count.find(i) == divisor_count.end()) {
      divisor_count[i] = 0;
    }
    while (tmp % i == 0) {
      tmp /= i;
      divisor_count[i] += 1;
    }
  }
  if (tmp != 1) {
    divisor_count[tmp] = 1;
  }

  long long ans = 1;
  for (auto it = divisor_count.begin(); it != divisor_count.end(); it++) {
    ans =
        (ans * comb((long long)(N - 1) + (long long)(it->second), N - 1)) % MOD;
  }
  cout << ans << endl;
  return 0;
}