Problem p03253 (C++)

Buggy submission (status: Runtime Error):

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define EXISTch(s, c)                                                          \
  ((((s).find_first_of(c)) != std::string::npos) ? 1 : 0) // cがあれば1 if(1)
#define SORT(c) sort((c).begin(), (c).end())

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = (int)1000000007;
const LL MOD = (LL)1000000007;          // 10^9+7
const LL INF2 = (LL)100000000000000000; // 10^18

map<int64_t, int> prime_factor(int64_t n) {
  map<int64_t, int> ret;
  for (int64_t i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }

  // 素数は1回も割られないのでそのままでてくる
  if (n != 1)
    ret[n] = 1;
  return ret;
}

// for (auto p : prime_factor(N)) {
//	cout << p.first << " " << p.second << endl;
// }

// auto primes=primefactor(n);

template <int mod> struct Combination {
  vector<int64_t> mfact, rfact;

  Combination(int sz) : mfact(sz + 1), rfact(sz + 1) {
    mfact[0] = 1;
    for (int i = 1; i < mfact.size(); i++) {
      mfact[i] = mfact[i - 1] * i % mod;
    }
    rfact[sz] = inv(mfact[sz]);
    for (int i = sz - 1; i >= 0; i--) {
      rfact[i] = rfact[i + 1] * (i + 1) % mod;
    }
  }

  int64_t fact(int k) const { return (mfact[k]); }

  int64_t pow(int64_t x, int64_t n) const {
    int64_t ret = 1;
    while (n > 0) {
      if (n & 1)
        (ret *= x) %= mod;
      (x *= x) %= mod;
      n >>= 1;
    }
    return (ret);
  }

  int64_t inv(int64_t x) const { return (pow(x, mod - 2)); }

  int64_t P(int n, int r) const {
    if (r < 0 || n < r)
      return (0);
    return (mfact[n] * rfact[n - r] % mod);
  }

  int64_t C(int p, int q) const {
    if (q < 0 || p < q)
      return (0);
    return (mfact[p] * rfact[q] % mod * rfact[p - q] % mod);
  }

  int64_t H(int n, int r) const {
    if (n < 0 || r < 0)
      return (0);
    return (r == 0 ? 1 : C(n + r - 1, r));
  }
};

int main() {

  int n, m;
  cin >> n >> m;

  Combination<MOD> com(90000);

  auto p = prime_factor(m);

  LL ans = 1;

  for (auto i : p) {
    ans *= com.C(i.second + n - 1, i.second);
    ans %= MOD;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define EXISTch(s, c)                                                          \
  ((((s).find_first_of(c)) != std::string::npos) ? 1 : 0) // cがあれば1 if(1)
#define SORT(c) sort((c).begin(), (c).end())

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = (int)1000000007;
const LL MOD = (LL)1000000007;          // 10^9+7
const LL INF2 = (LL)100000000000000000; // 10^18

map<int64_t, int> prime_factor(int64_t n) {
  map<int64_t, int> ret;
  for (int64_t i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }

  // 素数は1回も割られないのでそのままでてくる
  if (n != 1)
    ret[n] = 1;
  return ret;
}

// for (auto p : prime_factor(N)) {
//	cout << p.first << " " << p.second << endl;
// }

// auto primes=primefactor(n);

template <int mod> struct Combination {
  vector<int64_t> mfact, rfact;

  Combination(int sz) : mfact(sz + 1), rfact(sz + 1) {
    mfact[0] = 1;
    for (int i = 1; i < mfact.size(); i++) {
      mfact[i] = mfact[i - 1] * i % mod;
    }
    rfact[sz] = inv(mfact[sz]);
    for (int i = sz - 1; i >= 0; i--) {
      rfact[i] = rfact[i + 1] * (i + 1) % mod;
    }
  }

  int64_t fact(int k) const { return (mfact[k]); }

  int64_t pow(int64_t x, int64_t n) const {
    int64_t ret = 1;
    while (n > 0) {
      if (n & 1)
        (ret *= x) %= mod;
      (x *= x) %= mod;
      n >>= 1;
    }
    return (ret);
  }

  int64_t inv(int64_t x) const { return (pow(x, mod - 2)); }

  int64_t P(int n, int r) const {
    if (r < 0 || n < r)
      return (0);
    return (mfact[n] * rfact[n - r] % mod);
  }

  int64_t C(int p, int q) const {
    if (q < 0 || p < q)
      return (0);
    return (mfact[p] * rfact[q] % mod * rfact[p - q] % mod);
  }

  int64_t H(int n, int r) const {
    if (n < 0 || r < 0)
      return (0);
    return (r == 0 ? 1 : C(n + r - 1, r));
  }
};

int main() {

  int n, m;
  cin >> n >> m;

  Combination<MOD> com(10000000);

  auto p = prime_factor(m);

  LL ans = 1;

  for (auto i : p) {
    ans *= com.C(i.second + n - 1, i.second);
    ans %= MOD;
  }

  cout << ans << endl;

  return 0;
}