prithivMLmods/Coder-Stat huggingface dataset:
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const int max_size = 1000000;

vector<long long> fac(max_size + 1), ifac(max_size + 1);

long long pow_mod(long long x, long long n) {
  long long ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    n = n >> 1;
  }
  return ans;
}

long long comb(long long a, long long b) {
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  long long tmp = ifac[a - b] * ifac[b] % mod;
  return tmp * fac[a] % mod;
}
int main() {
  fac[0] = 1;
  ifac[0] = 1;
  for (long long i = 0; i < max_size; i++) {
    fac[i + 1] = fac[i] * (i + 1) % mod;
    ifac[i + 1] = ifac[i] * pow_mod(i + 1, mod - 2) % mod;
  }

  long long x, y;
  cin >> x >> y;
  if ((-2 * x + y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  long long a = (-x + 2 * y) / 3;
  long long b = (-y + 2 * x) / 3;
  cout << comb(a + b, a) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const int max_size = 1000000;

vector<long long> fac(max_size + 1), ifac(max_size + 1);

long long pow_mod(long long x, long long n) {
  long long ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    n = n >> 1;
  }
  return ans;
}

long long comb(long long a, long long b) {
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  long long tmp = ifac[a - b] * ifac[b] % mod;
  return tmp * fac[a] % mod;
}
int main() {
  fac[0] = 1;
  ifac[0] = 1;
  for (long long i = 0; i < max_size; i++) {
    fac[i + 1] = fac[i] * (i + 1) % mod;
    ifac[i + 1] = ifac[i] * pow_mod(i + 1, mod - 2) % mod;
  }

  long long x, y;
  cin >> x >> y;
  if ((-2 * x + y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  } else if ((-x + 2 * y) < 0 || (-y + 2 * x) < 0) {
    cout << 0 << endl;
    return 0;
  }
  long long a = (-x + 2 * y) / 3;
  long long b = (-y + 2 * x) / 3;
  cout << comb(a + b, a) << endl;
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  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 COM(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() {
  COMinit();
  int n, k;
  cin >> n >> k;
  int x = 2 * n - k;
  if (x % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  x /= 3;
  int y = n - 2 * x;
  long long xy = x + y;
  int64_t ans = 1;
  long long u = min(x, y);

  cout << COM(xy, u) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAX = 7000000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  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 COM(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() {
  COMinit();
  int n, k;
  cin >> n >> k;
  int x = 2 * n - k;
  if (x % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  x /= 3;
  int y = n - 2 * x;
  long long xy = x + y;
  int64_t ans = 1;
  long long u = min(x, y);

  cout << COM(xy, u) << endl;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const ll M = 1e9 + 7;

ll mul(ll a, ll b) { return a * b % M; }

ll add(ll a, ll b) {
  ll res = a + b;
  if (res >= M)
    res -= M;
  return res;
}

ll mod_pow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n & 1)
    return mul(x, mod_pow(mul(x, x), n / 2));
  return mod_pow(mul(x, x), n / 2);
}

ll inverse(ll a) { return mod_pow(a, M - 2); }

ll fac[1000010];

void compute_fac() {
  fac[0] = 1;
  for (int i = 1; i < 1000010; i++) {
    fac[i] = mul(fac[i - 1], i);
  }
}

int combo(int n, int k) {
  return mul(fac[n], inverse(mul(fac[k], fac[n - k])));
}

int main(int argc, char *argv[]) {
  compute_fac();
  int X, Y;
  cin >> X >> Y;
  if ((X + Y) % 3 != 0)
    return cout << 0 << endl, 0;
  int n = (2 * X - Y) / 3;
  int m = X - 2 * n;
  cout << combo(n + m, n) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const ll M = 1e9 + 7;

ll mul(ll a, ll b) { return a * b % M; }

ll add(ll a, ll b) {
  ll res = a + b;
  if (res >= M)
    res -= M;
  return res;
}

ll mod_pow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n & 1)
    return mul(x, mod_pow(mul(x, x), n / 2));
  return mod_pow(mul(x, x), n / 2);
}

ll inverse(ll a) { return mod_pow(a, M - 2); }

ll fac[1000010];

void compute_fac() {
  fac[0] = 1;
  for (int i = 1; i < 1000010; i++) {
    fac[i] = mul(fac[i - 1], i);
  }
}

int combo(int n, int k) {
  return mul(fac[n], inverse(mul(fac[k], fac[n - k])));
}

int main(int argc, char *argv[]) {
  compute_fac();
  int X, Y;
  cin >> X >> Y;
  if ((X + Y) % 3 != 0)
    return cout << 0 << endl, 0;
  int n = (2 * X - Y) / 3;
  int m = X - 2 * n;
  if (n < 0 || m < 0)
    return cout << 0 << endl, 0;
  cout << combo(n + m, n) << endl;
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

const long long mod = 1e9 + 7;

long long pow(const long long &v, const long long &n, const long long &mod) {
  long long ret = 1;
  long long num = n;
  long long now = v;

  while (num > 0) {
    if (num & 1) {
      ret = ret * now % mod;
    }
    now = now * now % mod;
    num = num >> 1;
  }

  return ret;
}

long long calc(const long long &n, const long long &k, const long long &mod) {
  long long ret;
  vector<long long> fac = vector<long long>(n + 1, 1);
  vector<long long> inv = vector<long long>(n + 1, 1);

  for (int i = 1; i <= n; ++i) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = pow(fac[i], mod - 2ll, mod);
  }

  ret = fac[n] * inv[k] % mod * inv[n - k] % mod;

  return ret;
}

int main() {

  ll x, y;
  cin >> x >> y;
  ll c = 0;
  for (int i = 0; i <= y + 1; ++i) {
    if (y * 2 == x) {
      break;
    }
    x++;
    y--;
    c++;
  }
  if (y < 0) {
    cout << 0 << endl;
    return 0;
  }

  ll ret = calc(y, c, mod);

  cout << ret << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

const long long mod = 1e9 + 7;

long long pow(const long long &v, const long long &n, const long long &mod) {
  long long ret = 1;
  long long num = n;
  long long now = v;

  while (num > 0) {
    if (num & 1) {
      ret = ret * now % mod;
    }
    now = now * now % mod;
    num = num >> 1;
  }

  return ret;
}

long long calc(const long long &n, const long long &k, const long long &mod) {
  long long ret;
  vector<long long> fac = vector<long long>(n + 1, 1);
  vector<long long> inv = vector<long long>(n + 1, 1);

  for (int i = 1; i <= n; ++i) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = pow(fac[i], mod - 2ll, mod);
  }

  ret = fac[n] * inv[k] % mod * inv[n - k] % mod;

  return ret;
}

int main() {

  ll x, y;
  cin >> x >> y;
  ll c = 0;
  for (int i = 0; i <= y + 1; ++i) {
    if (y * 2 == x) {
      break;
    }
    x++;
    y--;
    c++;
  }
  if (y < 0 || y < c) {
    cout << 0 << endl;
    return 0;
  }

  ll ret = calc(y, c, mod);

  cout << ret << endl;

  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

long long fact[1000005];

const long long MOD = 1e9 + 7;

long long powmod(long long a, long long b) {
  if (b == 0) {
    return 1 % MOD;
  } else if (b == 1) {
    return a % MOD;
  } else if (b % 2 == 0) {
    long long temp = powmod(a, b / 2);
    return (temp * temp) % MOD;
  } else {
    long long temp = powmod(a, b / 2);
    return (((temp * temp) % MOD) * a) % MOD;
  }
}

int main() {
  int X, Y;
  scanf("%d%d", &X, &Y);

  if ((X + Y) % 3 != 0) {
    printf("0");
    return 0;
  }

  int n = (X + Y) / 3;
  int r = Y - n;

  fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
  }

  long long ans = fact[n];
  long long div = (fact[n - r] * fact[r]) % MOD;
  printf("%lld", ans * powmod(div, MOD - 2) % MOD);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

long long fact[1000005];

const long long MOD = 1e9 + 7;

long long powmod(long long a, long long b) {
  if (b == 0) {
    return 1 % MOD;
  } else if (b == 1) {
    return a % MOD;
  } else if (b % 2 == 0) {
    long long temp = powmod(a, b / 2);
    return (temp * temp) % MOD;
  } else {
    long long temp = powmod(a, b / 2);
    return (((temp * temp) % MOD) * a) % MOD;
  }
}

int main() {
  int X, Y;
  scanf("%d%d", &X, &Y);

  if ((X + Y) % 3 != 0) {
    printf("0");
    return 0;
  }

  int n = (X + Y) / 3;
  int r = Y - n;
  if (r < 0 || r > n) {
    printf("0");
    return 0;
  }

  fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
  }

  long long ans = fact[n];
  long long div = (fact[n - r] * fact[r]) % MOD;
  printf("%lld", ans * powmod(div, MOD - 2) % MOD);

  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

#define MOD (1000000007)
using namespace std;

typedef long long ll;

vector<ll> fac(2000100, 0);  // n!(mod M)
vector<ll> ifac(2000100, 0); // k!^{M-2} (mod M)

ll mpow(ll x,
        ll n) { // x^n(mod M) ←普通にpow(x,n)では溢れてしまうため，随時mod計算
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans;
}

ll combination(ll a, ll b) {
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  ll tmp = ifac[a - b] * ifac[b] % MOD;
  return tmp * fac[a] % MOD;
}

int main() {
  ll X, Y;
  cin >> X >> Y;

  // 大した量ではないので，先にfax[i]とifax[i]を全て計算しておく
  fac[0] = 1;
  ifac[0] = 1;
  for (ll i = 0; i < 2000099; i++) {
    fac[i + 1] = fac[i] * (i + 1) % MOD; // n!(mod M)
    ifac[i + 1] = ifac[i] * mpow(i + 1, MOD - 2) %
                  MOD; // k!^{M-2} (mod M) ←累乗にmpowを採用
  }

  ll a_3 = 2 * Y - X;
  ll b_3 = 2 * X - Y;

  if (a_3 % 3 != 0 || b_3 % 3 != 0) {
    cout << 0 << endl;
  } else {
    ll a = a_3 / 3;
    ll b = b_3 / 3;
    cout << combination(a + b, a) << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

#define MOD (1000000007)
using namespace std;

typedef long long ll;

vector<ll> fac(2000100, 0);  // n!(mod M)
vector<ll> ifac(2000100, 0); // k!^{M-2} (mod M)

ll mpow(ll x,
        ll n) { // x^n(mod M) ←普通にpow(x,n)では溢れてしまうため，随時mod計算
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans;
}

ll combination(ll a, ll b) {
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  ll tmp = ifac[a - b] * ifac[b] % MOD;
  return tmp * fac[a] % MOD;
}

int main() {
  ll X, Y;
  cin >> X >> Y;

  // 大した量ではないので，先にfax[i]とifax[i]を全て計算しておく
  fac[0] = 1;
  ifac[0] = 1;
  for (ll i = 0; i < 2000099; i++) {
    fac[i + 1] = fac[i] * (i + 1) % MOD; // n!(mod M)
    ifac[i + 1] = ifac[i] * mpow(i + 1, MOD - 2) %
                  MOD; // k!^{M-2} (mod M) ←累乗にmpowを採用
  }

  ll a_3 = 2 * Y - X;
  ll b_3 = 2 * X - Y;

  // cout << a_3 << "," << b_3 << endl;

  if (a_3 % 3 != 0 || b_3 % 3 != 0 || a_3 < 0 || b_3 < 0) {
    cout << 0 << endl;
  } else {
    ll a = a_3 / 3;
    ll b = b_3 / 3;
    cout << combination(a + b, a) << endl;
  }

  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define SPEED                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll unsigned long long
const ll mod = 1e9 + 7; // adjust mod value according to your need
const ll limit = 670000;
void init_fact(vector<ll> &arr) {
  arr[0] = 1;
  for (ll i = 1; i <= limit; ++i)
    arr[i] = (arr[i - 1] * i) % mod;
}
ll power(ll x, ll y) {
  ll res = 1;
  x = x % mod;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res;
}
ll modInverse(ll n) { return power(n, mod - 2); }
ll nCrmod_fermat(ll n, ll r, vector<ll> &fac) {
  if (r == 0)
    return 1;
  return (fac[n] * modInverse(fac[r]) % mod * modInverse(fac[n - r]) % mod) %
         mod;
}
int main() {
  ll x, y;
  vector<ll> factorial(limit + 1);
  init_fact(factorial);
  cin >> x >> y;

  if ((x + y) % 3 != 0) {
    cout << 0;
    return 0;
  }
  ll k = (x + y) / 3;
  if (x - k < 0 || (2 * k) - x < 0) {
    cout << 0;
    return 0;
  }

  cout << nCrmod_fermat(k, x - k, factorial);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define SPEED                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
const ll mod = 1e9 + 7; // adjust mod value according to your need
const ll limit = 670000;
void init_fact(vector<ll> &arr) {
  arr[0] = 1;
  for (ll i = 1; i <= limit; ++i)
    arr[i] = (arr[i - 1] * i) % mod;
}
ll power(ll x, ll y) {
  ll res = 1;
  x = x % mod;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res;
}
ll modInverse(ll n) { return power(n, mod - 2); }
ll nCrmod_fermat(ll n, ll r, vector<ll> &fac) {
  if (r == 0)
    return 1;
  return (fac[n] * modInverse(fac[r]) % mod * modInverse(fac[n - r]) % mod) %
         mod;
}
int main() {
  ll x, y;
  vector<ll> factorial(limit + 1);
  init_fact(factorial);
  cin >> x >> y;

  if ((x + y) % 3 != 0) {
    cout << 0;
    return 0;
  }
  ll k = (x + y) / 3;
  if (x - k < 0 || (2 * k) - x < 0) {
    cout << 0;
    return 0;
  }

  cout << nCrmod_fermat(k, x - k, factorial);
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; ++i)
#define per(i, n) for (int i = n - 1; i >= 0; --i)
#define sc1(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d %d", &a, &b)
#define sc3(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define sl1(a) scanf("%lld", &a)
#define sl2(a, b) scanf("%lld %lld", &a, &b)
#define sl3(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define PI 3.1415926535897932

// typedef long long ll;

const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
  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 COM(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() {
  COMinit();
  int x, y, ans = 0;
  sc2(x, y);
  if ((x + y) % 3 != 0 || x > (x + y) / 3 * 2 || y > (x + y) / 3 * 2) {
    printf("0\n");
    return 0;
  }
  ans = COM((x + y) / 3, min(x, y) - (x + y) / 3);
  printf("%d\n", ans % MOD);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; ++i)
#define per(i, n) for (int i = n - 1; i >= 0; --i)
#define sc1(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d %d", &a, &b)
#define sc3(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define sl1(a) scanf("%lld", &a)
#define sl2(a, b) scanf("%lld %lld", &a, &b)
#define sl3(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define PI 3.1415926535897932

// typedef long long ll;

const int MAX = 710000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
  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 COM(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() {
  COMinit();
  int x, y, ans = 0;
  sc2(x, y);
  if ((x + y) % 3 != 0 || x > (x + y) / 3 * 2 || y > (x + y) / 3 * 2) {
    printf("0\n");
    return 0;
  }
  ans = COM((x + y) / 3, min(x, y) - (x + y) / 3);
  printf("%d\n", ans % MOD);
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int, int> P;

const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  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 COM(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;
}
/*x+yは3の倍数になる*/
int main() {
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3) {
    cout << 0 << endl;
    return 0;
  }
  int n = (x + y) / 3;
  x -= n, y -= n;
  if (x < 0 || y < 0) {
    cout << 0 << endl;
    return 0;
  }
  COMinit();
  cout << COM(x + y, x) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int, int> P;

const int MAX = 1000001;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  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 COM(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;
}
/*x+yは3の倍数になる*/
int main() {
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3) {
    cout << 0 << endl;
    return 0;
  }
  int n = (x + y) / 3;
  x -= n, y -= n;
  if (x < 0 || y < 0) {
    cout << 0 << endl;
    return 0;
  }
  COMinit();
  cout << COM(x + y, x) << endl;
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main() {
  ll X, Y;
  cin >> X >> Y;
  if ((X + Y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll Mod = 1000000007;
  ll ans = 0;
  ll a = (X + Y) / 3;
  X = X - a;
  Y = Y - a;
  ll B[666669];
  B[0] = 1;
  B[1] = 1;
  for (int i = 2; i <= 666668; i++) {
    B[i] = B[i - 1] * i;
    B[i] %= Mod;
  }
  if (X > Y) {
    swap(X, Y);
  }
  ll n = (B[X] * B[Y]) % Mod;
  ll m = B[X + Y];
  cout << (modinv(n, Mod) * m) % Mod << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main() {
  ll X, Y;
  cin >> X >> Y;
  if ((X + Y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  if (min(X, Y) * 2 < max(X, Y)) {
    cout << 0 << endl;
    return 0;
  }
  ll Mod = 1000000007;
  ll ans = 0;
  ll a = (X + Y) / 3;
  X = X - a;
  Y = Y - a;
  ll B[666669];
  B[0] = 1;
  B[1] = 1;
  for (int i = 2; i <= 666668; i++) {
    B[i] = B[i - 1] * i;
    B[i] %= Mod;
  }
  if (X > Y) {
    swap(X, Y);
  }
  ll n = (B[X] * B[Y]) % Mod;
  ll m = B[X + Y];
  cout << (modinv(n, Mod) * m) % Mod << endl;
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
typedef long long ll;

const ll M = 1000000007;

vector<ll> fac(2000005);
vector<ll> ifac(2000005);
// べき乗高速化
ll mpow(ll x, ll n) {
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % M;
    x = x * x % M;
    n = n >> 1;
  }
  return ans;
}

// aCbを計算
ll comb(ll a, ll b) {
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0) {
    return 0;
  }
  ll temp = ifac[a - b] * ifac[b] % M;
  return temp * fac[a] % M;
}

int main() {
  ll X, Y;
  cin >> X >> Y;
  fac[0] = 1;
  ifac[0] = 1;
  for (ll i = 0; i <= 2000000; i++) {
    fac[i + 1] = fac[i] * (i + 1) % M; // n!(mod M)
    ifac[i + 1] =
        ifac[i] * mpow(i + 1, M - 2) % M; // k!^{M-2} (mod M) ←累乗にmpowを採用
  }

  if ((X + Y) % 3 != 0) {
    cout << 0 << endl;
  } else {
    // 何回移動するか
    ll temp = (X + Y) / 3;
    cout << comb(temp, X - temp) << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
typedef long long ll;

const ll M = 1000000007;

vector<ll> fac(2000005);
vector<ll> ifac(2000005);
// べき乗高速化
ll mpow(ll x, ll n) {
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % M;
    x = x * x % M;
    n = n >> 1;
  }
  return ans;
}

// aCbを計算
ll comb(ll a, ll b) {
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0) {
    return 0;
  }
  ll temp = ifac[a - b] * ifac[b] % M;
  return temp * fac[a] % M;
}

int main() {
  ll X, Y;
  cin >> X >> Y;
  fac[0] = 1;
  ifac[0] = 1;
  for (ll i = 0; i <= 2000000; i++) {
    fac[i + 1] = fac[i] * (i + 1) % M; // n!(mod M)
    ifac[i + 1] =
        ifac[i] * mpow(i + 1, M - 2) % M; // k!^{M-2} (mod M) ←累乗にmpowを採用
  }

  if ((X + Y) % 3 != 0) {
    cout << 0 << endl;
  } else {
    // 何回移動するか
    ll temp = (X + Y) / 3;
    if (X - temp < 0) {
      cout << 0 << endl;
    } else {
      cout << comb(temp, X - temp) << endl;
    }
  }
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N = 1e6 + 6;
const int mod = 1e9 + 7;
ll fact[N], inv[N];

ll fp(ll b, ll p) {
  if (p == 0)
    return 1;
  if (p & 1)
    return (b * fp(b, p - 1)) % mod;
  return fp((b * b) % mod, p / 2);
}

ll nCk(ll n, ll k) { return ((fact[n] * inv[k]) % mod * inv[n - k]) % mod; }

int main() {

#ifndef ONLINE_JUDGE
  freopen("test.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  fact[0] = 1;
  inv[0] = fp(fact[0], mod - 2);
  for (int i = 1; i <= 1000000; i++) {
    fact[i] = fact[i - 1] * i % mod;
    inv[i] = fp(fact[i], mod - 2);
  }
  ll x, y, ans = 0;
  cin >> x >> y;
  if ((x + y) % 3 != 0)
    return cout << 0, 0;
  ll n = 2 * y - x;
  n = n / 3;
  ll m = y - 2 * n;
  if (m < 0 || n < 0)
    return cout << 0, 0;
  cout << nCk(n + m, m) << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N = 1e6 + 6;
const int mod = 1e9 + 7;
ll fact[N], inv[N];

ll fp(ll b, ll p) {
  if (p == 0)
    return 1;
  if (p & 1)
    return (b * fp(b, p - 1)) % mod;
  return fp((b * b) % mod, p / 2);
}

ll nCk(ll n, ll k) { return ((fact[n] * inv[k]) % mod * inv[n - k]) % mod; }

int main() {

  /* #ifndef ONLINE_JUDGE
   freopen("test.txt", "r", stdin);
   #endif*/
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  fact[0] = 1;
  inv[0] = fp(fact[0], mod - 2);
  for (int i = 1; i <= 1000000; i++) {
    fact[i] = fact[i - 1] * i % mod;
    inv[i] = fp(fact[i], mod - 2);
  }
  ll x, y, ans = 0;
  cin >> x >> y;
  if ((x + y) % 3 != 0)
    return cout << 0, 0;
  ll n = 2 * y - x;
  n = n / 3;
  ll m = y - 2 * n;
  if (m < 0 || n < 0)
    return cout << 0, 0;
  cout << nCk(n + m, m) << '\n';

  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const int mod = 1e9 + 7;
const int N = 2e6 + 23;

int f[N];

int binpow(long long b, long long e) {
  if (!e)
    return 1;
  if (e & 1)
    return b * binpow(b, e - 1) % mod;
  return binpow(b * b % mod, e / 2);
}
int C(int n, int k) {
  return 1LL * f[n] * binpow(f[k], mod - 2) % mod * binpow(f[n - k], mod - 2) %
         mod;
}

int main() {

  ll x, y;
  cin >> x >> y;
  f[0] = 1;
  for (int i = 1; i <= 1000001; ++i) {
    f[i] = 1LL * f[i - 1] * i % mod;
  }
  if (2 * x - y < 0)
    cout << "0";
  else if ((x + y) % 3 != 0)
    cout << "0";
  else
    cout << C((x + y) / 3, (2 * x - y) / 3);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const int mod = 1e9 + 7;
const int N = 2e6 + 23;

int f[N];

int binpow(long long b, long long e) {
  if (!e)
    return 1;
  if (e & 1)
    return b * binpow(b, e - 1) % mod;
  return binpow(b * b % mod, e / 2);
}
int C(int n, int k) {
  if (n - k < 0) {
    cout << 0;
    exit(0);
  }
  return 1LL * f[n] * binpow(f[k], mod - 2) % mod * binpow(f[n - k], mod - 2) %
         mod;
}

int main() {

  ll x, y;
  cin >> x >> y;
  f[0] = 1;
  for (int i = 1; i <= 1000001; ++i) {
    f[i] = 1LL * f[i - 1] * i % mod;
  }
  if (2 * x - y < 0)
    cout << "0";
  else if ((x + y) % 3 != 0)
    cout << "0";
  else
    cout << C((x + y) / 3, (2 * x - y) / 3);

  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define pb push_back
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define ordered_set                                                            \
  tree<int, null_type, std::less<int>, rb_tree_tag,                            \
       tree_order_statistics_node_update>

using namespace __gnu_pbds;
using namespace std;

ll mod = 1e9 + 7;

ll add(ll a, ll b) { return (a % mod + b % mod) % mod; }
ll mul(ll a, ll b) { return (a % mod * b % mod) % mod; }
ll debug(vector<int> a) {
  for (int i : a)
    cout << i << " ";
  return puts("\n"), 0;
}

ll power(ll base, ll exp) {
  ll res = 1;
  while (exp) {
    if (exp & 1)
      res = mul(res, base);
    base = mul(base, base);
    exp >>= 1;
  }
  return res;
}
ll inv(ll p) { return power(p, mod - 2); }
ll fact[2000003] = {0};
int main() {
  fastio fact[0] = 1;
  for (int i = 1; i <= 2000002; i++)
    fact[i] = mul(fact[i - 1], i);
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0)
    return puts("0"), 0;
  ll n = (2 * y - x) / 3, m = (2 * x - y) / 3;
  ll num = fact[n + m];
  ll den = mul(fact[n], fact[m]);
  ll ans = mul(num, inv(den));
  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define pb push_back
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define ordered_set                                                            \
  tree<int, null_type, std::less<int>, rb_tree_tag,                            \
       tree_order_statistics_node_update>

using namespace __gnu_pbds;
using namespace std;

ll mod = 1e9 + 7;

ll add(ll a, ll b) { return (a % mod + b % mod) % mod; }
ll mul(ll a, ll b) { return (a % mod * b % mod) % mod; }
ll debug(vector<int> a) {
  for (int i : a)
    cout << i << " ";
  return puts("\n"), 0;
}

ll power(ll base, ll exp) {
  ll res = 1;
  while (exp) {
    if (exp & 1)
      res = mul(res, base);
    base = mul(base, base);
    exp >>= 1;
  }
  return res;
}
ll inv(ll p) { return power(p, mod - 2); }
ll fact[2000003] = {0};
int main() {
  fastio fact[0] = 1;
  for (int i = 1; i <= 2000002; i++)
    fact[i] = mul(fact[i - 1], i);
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0)
    return puts("0"), 0;
  ll n = (2 * y - x) / 3, m = (2 * x - y) / 3;
  if (n < 0 || m < 0) {
    cout << "0";
    return 0;
  }
  ll num = fact[n + m];
  ll den = mul(fact[n], fact[m]);
  ll ans = mul(num, inv(den));
  cout << ans << "\n";
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

//-------------------- Modulo --------------------//
#define MOD (1000000007LL)
#define SIZE (1000000LL)

long long fac[SIZE + 1];
long long inv[SIZE + 1];
long long finv[SIZE + 1];

void modinit(void) {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i <= SIZE; ++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 modpow(long long a, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
}

long long modcom(long long n, long long k) {
  assert(n >= k);
  assert(n <= SIZE);
  assert(k <= SIZE);
  return fac[n] * finv[n - k] % MOD * finv[k] % MOD;
}
//-------------------- Modulo --------------------//

int main() {
  modinit();

  int X, Y;
  cin >> X >> Y;

  if ((X + Y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }

  int N = (X + Y) / 3;
  int a = X - N;
  int b = Y - N;

  cout << modcom(N, a) << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

//-------------------- Modulo --------------------//
#define MOD (1000000007LL)
#define SIZE (1000000LL)

long long fac[SIZE + 1];
long long inv[SIZE + 1];
long long finv[SIZE + 1];

void modinit(void) {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i <= SIZE; ++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 modpow(long long a, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
}

long long modcom(long long n, long long k) {
  assert(n >= k);
  assert(n <= SIZE);
  assert(k <= SIZE);
  return fac[n] * finv[n - k] % MOD * finv[k] % MOD;
}
//-------------------- Modulo --------------------//

int main() {
  modinit();

  int X, Y;
  cin >> X >> Y;

  if ((X + Y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }

  int N = (X + Y) / 3;
  int a = X - N;
  int b = Y - N;

  if (a < 0 or b < 0) {
    cout << 0 << endl;
    return 0;
  }

  cout << modcom(N, a) << endl;

  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 7;
ll fac[maxn];
ll inv[maxn];
ll C(int m, int n) {
  if (m > n)
    return -1;
  return fac[n] * inv[m] % mod * inv[n - m] % mod;
}
ll quick_mod(ll a, ll m) {
  ll tmp = a % mod;
  ll ans = 1;
  while (m) {
    if (m & 1)
      ans = ans * tmp % mod;
    tmp = tmp * tmp % mod;
    m >>= 1;
  }
  return ans;
}
void init() {
  fac[0] = 1;
  for (int i = 1; i < maxn; i++)
    fac[i] = (fac[i - 1] * i) % mod;
  inv[maxn - 1] = quick_mod(fac[maxn - 1], mod - 2);
  for (int i = maxn - 2; i >= 0; i--)
    inv[i] = (inv[i + 1] * (i + 1)) % mod;
}

int main() {
  init();
  int x, y;
  scanf("%d %d", &x, &y);
  if ((x + y) % 3 != 0)
    printf("0\n");
  else {
    ll t = (x + y) / 3;
    ll k1 = x - t;
    ll k2 = y - t;
    printf("%lld\n", C(k1, k1 + k2));
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 7;
ll fac[maxn];
ll inv[maxn];
ll C(int m, int n) {
  if (m > n)
    return -1;
  return fac[n] * inv[m] % mod * inv[n - m] % mod;
}
ll quick_mod(ll a, ll m) {
  ll tmp = a % mod;
  ll ans = 1;
  while (m) {
    if (m & 1)
      ans = ans * tmp % mod;
    tmp = tmp * tmp % mod;
    m >>= 1;
  }
  return ans;
}
void init() {
  fac[0] = 1;
  for (int i = 1; i < maxn; i++)
    fac[i] = (fac[i - 1] * i) % mod;
  inv[maxn - 1] = quick_mod(fac[maxn - 1], mod - 2);
  for (int i = maxn - 2; i >= 0; i--)
    inv[i] = (inv[i + 1] * (i + 1)) % mod;
}

int main() {
  init();
  int x, y;
  scanf("%d %d", &x, &y);
  if ((x + y) % 3 != 0)
    printf("0\n");
  else {
    ll t = (x + y) / 3;
    ll k1 = x - t;
    ll k2 = y - t;
    if (x * 2 >= y && y * 2 >= x)
      printf("%lld\n", C(k1, k1 + k2));
    else
      printf("0\n");
  }
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MOD = 1e9 + 7;
const int N = 1e6 + 5;
int x, y;
ll fac[N], inv[N];

ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a) % MOD;
    }
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

void init() {
  fac[0] = 1;
  for (int i = 1; i < N; ++i) {
    fac[i] = fac[i - 1] * i % MOD;
  }
  inv[N - 1] = power(fac[N - 1], MOD - 2);
  for (int i = N - 2; i >= 0; --i) {
    inv[i] = inv[i + 1] * (i + 1) % MOD;
  }
}

ll C(ll a, ll b) { return fac[a] * inv[b] % MOD * inv[a - b] % MOD; }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  init();
  cin >> x >> y;
  int sum = x + y;
  if (sum % 3) {
    cout << 0 << endl;
  } else {
    int cnt = sum / 3;
    int two = x - cnt;
    ll res = C(cnt, two);
    cout << res << endl;
  }

  return 0;
}


Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MOD = 1e9 + 7;
const int N = 1e6 + 5;
int x, y;
ll fac[N], inv[N];

ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a) % MOD;
    }
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

void init() {
  fac[0] = 1;
  for (int i = 1; i < N; ++i) {
    fac[i] = fac[i - 1] * i % MOD;
  }
  inv[N - 1] = power(fac[N - 1], MOD - 2);
  for (int i = N - 2; i >= 0; --i) {
    inv[i] = inv[i + 1] * (i + 1) % MOD;
  }
}

ll C(ll a, ll b) { return fac[a] * inv[b] % MOD * inv[a - b] % MOD; }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  init();
  cin >> x >> y;
  int sum = x + y;
  if (sum % 3) {
    cout << 0 << endl;
  } else {
    int cnt = sum / 3;
    int two = x - cnt;
    if (two < 0 || two > cnt) {
      cout << 0 << endl;
      exit(0);
    }
    ll res = C(cnt, two);
    cout << res << endl;
  }

  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

const ll MOD = 1e9 + 7;
const int INF32 = 1 << 30;
const ll INF64 = 1LL << 60;
const int N = 1e6 + 10;
const ll mod = 1e9 + 7;

ll fac[N];

ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}

void init() {
  fac[0] = 1;
  for (ll i = 1; i < N; i++)
    fac[i] = fac[i - 1] * i % mod;
}

ll inv(ll a) { return binpow(a, mod - 2); }

ll A(ll n, ll m) { return fac[n] * inv(fac[n - m]) % mod; }

ll C(ll n, ll m) { return fac[n] * inv(fac[m]) % mod * inv(fac[n - m]) % mod; }

void solve() {
  ll x, y;
  cin >> x >> y;
  if (y > x)
    swap(x, y);
  ll j = x - y;
  if ((y - j) % 3) {
    cout << 0;
    return;
  }
  ll k = (y - j) / 3;
  ll k2 = k + j;
  init();
  cout << A(k + k2, k + k2) * inv(A(k, k) * A(k2, k2) % mod) % mod;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

const ll MOD = 1e9 + 7;
const int INF32 = 1 << 30;
const ll INF64 = 1LL << 60;
const int N = 1e6 + 10;
const ll mod = 1e9 + 7;

ll fac[N];

ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}

void init() {
  fac[0] = 1;
  for (ll i = 1; i < N; i++)
    fac[i] = fac[i - 1] * i % mod;
}

ll inv(ll a) { return binpow(a, mod - 2); }

ll A(ll n, ll m) { return fac[n] * inv(fac[n - m]) % mod; }

ll C(ll n, ll m) { return fac[n] * inv(fac[m]) % mod * inv(fac[n - m]) % mod; }

void solve() {
  ll x, y;
  cin >> x >> y;
  if (y > x)
    swap(x, y);
  ll j = x - y;
  if ((y - j) % 3 || y - j < 0) {
    cout << 0;
    return;
  }
  ll k = (y - j) / 3;
  ll k2 = k + j;
  init();
  cout << A(k + k2, k + k2) * inv(A(k, k) * A(k2, k2) % mod) % mod;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

long long f[1000000], rf[1000000];
long long mod = 1000000007;

long long inv(long long x) {
  long long res = 1;
  long long k = mod - 2;
  long long y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % mod;
    y = (y * y) % mod;
    k /= 2;
  }
  return res;
}

void init() {
  f[0] = 1;
  for (int i = 1; i < 1000000; i++)
    f[i] = (f[i - 1] * i) % mod;
  for (int i = 0; i < 1000000; i++)
    rf[i] = inv(f[i]);
}
int main() {
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0) {
    // 到達不能
    cout << 0 << endl;
    return 0;
  }
  init();
  long long n = (x + y) / 3;
  long long k = x - n;
  if (k < 0) {
    cout << 0 << endl;
    return 0;
  }
  long long a = f[n];
  long long b = rf[n - k];
  long long c = rf[k];

  long long bc = (b * c) % mod;
  cout << ((a * bc) % mod) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

long long f[1000000], rf[1000000];
long long mod = 1000000007;

long long inv(long long x) {
  long long res = 1;
  long long k = mod - 2;
  long long y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % mod;
    y = (y * y) % mod;
    k /= 2;
  }
  return res;
}

void init() {
  f[0] = 1;
  for (int i = 1; i < 1000000; i++)
    f[i] = (f[i - 1] * i) % mod;
  for (int i = 0; i < 1000000; i++)
    rf[i] = inv(f[i]);
}
int main() {
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0) {
    // 到達不能
    cout << 0 << endl;
    return 0;
  }
  init();
  long long n = (x + y) / 3;
  long long k = x - n;
  if (k < 0 || n - k < 0) {
    cout << 0 << endl;
    return 0;
  }
  long long a = f[n];
  long long b = rf[n - k];
  long long c = rf[k];

  long long bc = (b * c) % mod;
  cout << ((a * bc) % mod) << endl;
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

#define MOD 1000000007

using namespace std;

int X, Y;

uint64_t mod_power(uint64_t x, uint64_t y) {
  uint64_t result = 1;

  while (y > 0) {
    if (y & 1) {
      result = (result * x) % MOD;
    }

    y = y >> 1;
    x = (x * x) % MOD;
  }

  return result;
}

uint64_t mod_inverse(uint64_t x) { return mod_power(x, MOD - 2); }

uint64_t comb(int n, int r) {
  if (r == 0) {
    return 1;
  }

  vector<uint64_t> f(n + 1, 0);
  f[0] = 1;
  for (int i = 1; i <= n; ++i) {
    f[i] = (i * f[i - 1]) % MOD;
  }

  return (f[n] * mod_inverse(f[r]) % MOD * mod_inverse(f[n - r]) % MOD) % MOD;
}

int solve() {
  if (X == 0) {
    return (Y % 4 == 0) ? 1 : 0;
  }

  if (Y == 0) {
    return (X % 4 == 0) ? 1 : 0;
  }

  if ((2 * X - Y) % 3 != 0 || (2 * Y - X) % 3 != 0) {
    return 0;
  }

  int a = (2 * X - Y) / 3;
  int b = (2 * Y - X) / 3;

  return comb(a + b, a);
}

int main() {
  cin >> X >> Y;

  cout << solve() << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

#define MOD 1000000007

using namespace std;

int X, Y;

uint64_t mod_power(uint64_t x, uint64_t y) {
  uint64_t result = 1;

  while (y > 0) {
    if (y & 1) {
      result = (result * x) % MOD;
    }

    y = y >> 1;
    x = (x * x) % MOD;
  }

  return result;
}

uint64_t mod_inverse(uint64_t x) { return mod_power(x, MOD - 2); }

uint64_t comb(int n, int r) {
  if (r == 0) {
    return 1;
  }

  vector<uint64_t> f(n + 1, 0);
  f[0] = 1;
  for (int i = 1; i <= n; ++i) {
    f[i] = (i * f[i - 1]) % MOD;
  }

  return (f[n] * mod_inverse(f[r]) % MOD * mod_inverse(f[n - r]) % MOD) % MOD;
}

int solve() {
  if ((2 * X - Y) % 3 != 0 || (2 * Y - X) % 3 != 0 || 2 * X - Y < 0 ||
      2 * Y - X < 0) {
    return 0;
  }

  int a = (2 * X - Y) / 3;
  int b = (2 * Y - X) / 3;

  return comb(a + b, a);
}

int main() {
  cin >> X >> Y;

  cout << solve() << endl;

  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
typedef long long ll;

const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  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 COM(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 abs(int a) { return a < 0 ? -a : a; }
void swap(int *a, int *b) {
  int c = *a;
  *a = *b;
  *b = c;
}

int main() {
  COMinit();
  int x, y;
  cin >> x >> y;
  if (x < y)
    swap(&x, &y);

  if ((x + y) % 3 != 0) {
    cout << 0;
    return 0;
  }

  int cy = ((x + y) / 3), cx = (x - cy);
  cout << COM(cy, cx) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
typedef long long ll;

const int MAX = 10000000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  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 COM(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 abs(int a) { return a < 0 ? -a : a; }
void swap(int *a, int *b) {
  int c = *a;
  *a = *b;
  *b = c;
}

int main() {
  COMinit();
  int x, y;
  cin >> x >> y;
  if (x < y)
    swap(&x, &y);

  if ((x + y) % 3 != 0) {
    cout << 0;
    return 0;
  }

  int cy = ((x + y) / 3), cx = (x - cy);
  cout << COM(cy, cx) << endl;
}
Problem p02862 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

int main() {
  using namespace std;
  unsigned long X, Y;
  cin >> X >> Y;
  if ((X + Y) % 3)
    return 0 & puts("0");
  {
    unsigned long k{(X + Y) / 3};
    X -= k;
    Y -= k;
  }
  unsigned long N{X + Y}, d{1}, n{1};
  constexpr unsigned long MOD = 1000000007;
  for (unsigned long i = 0, j = N; i < X;) {
    (d *= j--) %= MOD;
    (n *= ++i) %= MOD;
  }
  unsigned long a{MOD - 2};
  while (a) {
    if (a & 1)
      (d *= n) %= MOD;
    (n *= n) %= MOD;
    a >>= 1;
  }
  cout << d << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

int main() {
  using namespace std;
  unsigned long X, Y;
  cin >> X >> Y;
  if ((X + Y) % 3)
    return 0 & puts("0");
  {
    unsigned long k{(X + Y) / 3};
    if (X < k || Y < k)
      return 0 & puts("0");
    X -= k;
    Y -= k;
  }
  unsigned long N{X + Y}, d{1}, n{1};
  constexpr unsigned long MOD = 1000000007;
  for (unsigned long i = 0, j = N; i < X;) {
    (d *= j--) %= MOD;
    (n *= ++i) %= MOD;
  }
  unsigned long a{MOD - 2};
  while (a) {
    if (a & 1)
      (d *= n) %= MOD;
    (n *= n) %= MOD;
    a >>= 1;
  }
  cout << d << endl;
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

using namespace std;
typedef int64_t ll;
typedef vector<int64_t> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
typedef vector<P> vp;
typedef vector<double> vd;
typedef vector<vd> vvd;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SIZE(a) int((a).size())
const int MAX = 1000010;
const int MOD = 1000000007;
ll fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
  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;
  }
}
ll COM(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() {
  ll x, y;
  cin >> x >> y;
  ll s, t;
  if ((x + y) % 3 != 0) {
    cout << 0 << endl;
  } else {
    t = (x * 2 - y) / 3;
    s = x - s * 2;
    if (s < 0 || t < 0) {
      cout << 0 << endl;
    } else {
      COMinit();
      cout << COM(s + t, max(s, t)) << endl;
    }
  }
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;
typedef int64_t ll;
typedef vector<int64_t> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
typedef vector<P> vp;
typedef vector<double> vd;
typedef vector<vd> vvd;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SIZE(a) int((a).size())
const int MAX = 1000010;
const int MOD = 1000000007;
ll fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
  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;
  }
}
ll COM(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() {
  ll x, y;
  cin >> x >> y;
  ll s, t;
  if ((x + y) % 3 != 0) {
    cout << 0 << endl;
  } else {
    t = (x * 2 - y) / 3;
    s = x - t * 2;
    if (s < 0 || t < 0) {
      cout << 0 << endl;
    } else {
      COMinit();
      cout << COM(s + t, max(s, t)) << endl;
    }
  }
}