Problem p03253 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <map>
#include <vector>

using namespace std;

#define MOD 1000000007

long long factorial[100010] = {};
long long fact_inv[100010] = {};
long long inv[100010] = {};
int N = 100000;

long long power(long long n, int p) {
  if (p == 0) {
    return 1;
  }
  if (p == 1) {
    return n % MOD;
  }
  long long s;
  s = power(n, p / 2);
  if (p % 2 == 0) {
    return s * s % MOD;
  }
  if (p % 2 == 1) {
    return n * power(n, p - 1) % MOD;
  } else {
    return 0;
  }
}

void calc() {
  // printf("calc called\n");
  inv[1] = 1;
  for (int i = 2; i <= N; i++) {
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
  }
  factorial[0] = 1;
  fact_inv[0] = 1;
  for (int i = 1; i <= N; i++) {
    factorial[i] = (factorial[i - 1] * i) % MOD;
    fact_inv[i] = (fact_inv[i - 1] * inv[i]) % MOD;
  }
  // printf("calc fin\n");
}

long long combi(long long n, int p) {
  if (p > n || p < 0) {
    return 0;
  } else {
    return (((factorial[n] * fact_inv[p]) % MOD) * fact_inv[n - p]) % MOD;
  }
}

map<int, int> prime_factor(int n) {
  map<int, int> res;
  for (int i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      res[i] += 1;
      n = n / i;
    }
  }
  if (n != 1) {
    res[n] += 1;
  }
  return res;
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  map<int, int> ans_1 = prime_factor(m);
  /*printf("\n");
  for (pair<int, int> x: ans_1) {
    printf("(%d %d) ", x.first, x.second);
  }
  printf("\n");
  */
  calc();
  long long ans = 1;
  for (pair<int, int> x : ans_1) {
    // printf("(%d %d) ", x.first, x.second);
    ans = (ans * combi(n + x.second - 1, n - 1)) % MOD;
  }
  printf("%lld", ans);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <map>
#include <vector>

using namespace std;

#define MOD 1000000007

long long factorial[200010] = {};
long long fact_inv[200010] = {};
long long inv[200010] = {};
int N = 200001;

long long power(long long n, int p) {
  if (p == 0) {
    return 1;
  }
  if (p == 1) {
    return n % MOD;
  }
  long long s;
  s = power(n, p / 2);
  if (p % 2 == 0) {
    return s * s % MOD;
  }
  if (p % 2 == 1) {
    return n * power(n, p - 1) % MOD;
  } else {
    return 0;
  }
}

void calc() {
  // printf("calc called\n");
  inv[1] = 1;
  for (int i = 2; i <= N; i++) {
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
  }
  factorial[0] = 1;
  fact_inv[0] = 1;
  for (int i = 1; i <= N; i++) {
    factorial[i] = (factorial[i - 1] * i) % MOD;
    fact_inv[i] = (fact_inv[i - 1] * inv[i]) % MOD;
  }
  // printf("calc fin\n");
}

long long combi(long long n, int p) {
  if (p > n || p < 0) {
    return 0;
  } else {
    return (((factorial[n] * fact_inv[p]) % MOD) * fact_inv[n - p]) % MOD;
  }
}

map<int, int> prime_factor(int n) {
  map<int, int> res;
  for (int i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      res[i] += 1;
      n = n / i;
    }
  }
  if (n != 1) {
    res[n] += 1;
  }
  return res;
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  map<int, int> ans_1 = prime_factor(m);
  /*printf("\n");
  for (pair<int, int> x: ans_1) {
    printf("(%d %d) ", x.first, x.second);
  }
  printf("\n");
  */
  calc();
  long long ans = 1;
  for (pair<int, int> x : ans_1) {
    // printf("(%d %d) ", x.first, x.second);
    ans = (ans * combi(n + x.second - 1, n - 1)) % MOD;
  }
  printf("%lld", ans);
  return 0;
}