Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const long long int mod = 1000000007;
const long long int INF = 99999999999999999;

long long int n, m, res, di = 1;
long double x[20], y[20], sum = 0;
string str;
// 階乗先に計算
long long int kaizyou[3000005] = {1};
long long int kai(long long int a, long long int b, long long int mod) {
  long long int tmp = 1;
  for (long long int i = a; i <= b; i++) {
    tmp *= i;
    tmp %= mod;

    kaizyou[i] = tmp;
  }
  return tmp;
}

// 累乗(繰り返し2乗法)　aのb乗(mod)を求める。
long long int rui(long long int a, long long int b, long long mod) {
  int memo[65] = {};
  long long int A[65] = {};
  long long int tmp = 1;
  for (int i = 0; i < 65; i++) {
    memo[i] = b % 2;
    b /= 2;
  }

  A[0] = a;
  A[0] %= mod;

  for (int i = 1; i < 65; i++) {
    A[i] = A[i - 1] * A[i - 1];
    A[i] %= mod;
  }
  for (int i = 0; i < 65; i++) {
    if (memo[i] == 1) {
      tmp *= A[i];
      tmp %= mod;
    }
  }
  tmp %= mod;
  return tmp;
}

// コンビネーション計算
long long int comb(long long int n, long long int r, long long int mod) {
  long long int tmp;

  tmp = (kaizyou[n] * rui(kaizyou[r], mod - 2, mod)) % mod;
  tmp *= rui(kaizyou[n - r], mod - 2, mod);
  tmp %= mod;
  if (tmp < 0) {
    tmp = (mod - tmp) % mod;
  }
  return tmp;
}

int main() {
  cout << fixed << setprecision(10);
  cin >> n >> m;
  kai(1, 3000000, mod);
  if ((n + m) % 3 != 0) {
    cout << 0;
    return 0;
  }
  sum = (n + m) / 3;
  n -= sum;
  m -= sum;

  cout << comb(n + m, n, mod);
}


Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const long long int mod = 1000000007;
const long long int INF = 99999999999999999;

long long int n, m, res, di = 1;
long double x[20], y[20], sum = 0;
string str;
// 階乗先に計算
long long int kaizyou[3000005] = {1};
long long int kai(long long int a, long long int b, long long int mod) {
  long long int tmp = 1;
  for (long long int i = a; i <= b; i++) {
    tmp *= i;
    tmp %= mod;

    kaizyou[i] = tmp;
  }
  return tmp;
}

// 累乗(繰り返し2乗法)　aのb乗(mod)を求める。
long long int rui(long long int a, long long int b, long long mod) {
  int memo[65] = {};
  long long int A[65] = {};
  long long int tmp = 1;
  for (int i = 0; i < 65; i++) {
    memo[i] = b % 2;
    b /= 2;
  }

  A[0] = a;
  A[0] %= mod;

  for (int i = 1; i < 65; i++) {
    A[i] = A[i - 1] * A[i - 1];
    A[i] %= mod;
  }
  for (int i = 0; i < 65; i++) {
    if (memo[i] == 1) {
      tmp *= A[i];
      tmp %= mod;
    }
  }
  tmp %= mod;
  return tmp;
}

// コンビネーション計算
long long int comb(long long int n, long long int r, long long int mod) {
  long long int tmp;

  tmp = (kaizyou[n] * rui(kaizyou[r], mod - 2, mod)) % mod;
  tmp *= rui(kaizyou[n - r], mod - 2, mod);
  tmp %= mod;
  if (tmp < 0) {
    tmp = (mod - tmp) % mod;
  }
  return tmp;
}

int main() {
  cout << fixed << setprecision(10);
  cin >> n >> m;
  kai(1, 3000000, mod);
  if ((n + m) % 3 != 0 || n > 2 * m || m > 2 * n) {
    cout << 0;
    return 0;
  }
  sum = (n + m) / 3;
  n -= sum;
  m -= sum;

  cout << comb(n + m, n, mod);
}