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

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

int64_t MOD = 1e9 + 7;

int64_t fact[1000005];

int64_t factorial(int n, int p) { return fact[n] % p; }

//-RECIPE-START-//
// Use 64 bits integers to avoid overflow errors during multiplication.
int64_t modPow(int64_t a, int64_t x, int64_t p) {
  // calculates a^x mod p in logarithmic time.
  int64_t res = 1;
  while (x > 0) {
    if (x % 2)
      res = (res * a) % p;
    a = (a * a) % p;
    x /= 2;
  }
  return res;
}

int64_t modInverse(int64_t a, int64_t p) { // (assuming p is prime).
  return modPow(a, p - 2,
                p); // calculates the modular multiplicative of a mod m.
}
int64_t
modBinomial(int64_t n, int64_t k,
            int64_t p) { // calculates C(n,k) mod p (assuming p is prime).
  int64_t numerator = factorial(n, p); // n!
  int64_t denominator =
      (factorial(k, p) * (factorial(n - k, p))) % p; // k!*(n-k)!
  return (numerator * modInverse(denominator, p)) %
         p; // numerator / denominator mod p.
}

int main() {
  fact[0] = 1;
  for (int64_t i = 1; i <= 1e6; ++i) {
    fact[i] = (fact[i - 1] * i) % MOD;
  }
  int R, C;
  cin >> R >> C;

  int d = C + R;

  if (d % 3) {
    cout << 0 << endl;
    return 0;
  }

  int cb = d / 3, rb = 2 * (d / 3);

  if (R > rb || C < cb) {
    cout << 0 << endl;
    return 0;
  }

  int diff = C - cb;

  // cout << "here " << cb << " " << rb << " " << diff << endl;
  if (R != rb - diff) {
    cout << 0 << endl;
    return 0;
  }

  cout << modBinomial(d / 3, diff, MOD) << endl;
}

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

int64_t MOD = 1e9 + 7;

int64_t fact[1000005];

int64_t factorial(int n, int p) { return fact[n] % p; }

//-RECIPE-START-//
// Use 64 bits integers to avoid overflow errors during multiplication.
int64_t modPow(int64_t a, int64_t x, int64_t p) {
  // calculates a^x mod p in logarithmic time.
  int64_t res = 1;
  while (x > 0) {
    if (x % 2)
      res = (res * a) % p;
    a = (a * a) % p;
    x /= 2;
  }
  return res;
}

int64_t modInverse(int64_t a, int64_t p) { // (assuming p is prime).
  return modPow(a, p - 2,
                p); // calculates the modular multiplicative of a mod m.
}
int64_t
modBinomial(int64_t n, int64_t k,
            int64_t p) { // calculates C(n,k) mod p (assuming p is prime).
  int64_t numerator = factorial(n, p); // n!
  int64_t denominator =
      (factorial(k, p) * (factorial(n - k, p))) % p; // k!*(n-k)!
  return (numerator * modInverse(denominator, p)) %
         p; // numerator / denominator mod p.
}

int main() {
  fact[0] = 1;
  for (int64_t i = 1; i <= 1e6; ++i) {
    fact[i] = (fact[i - 1] * i) % MOD;
  }
  int R, C;
  cin >> R >> C;

  int d = C + R;

  if (d % 3) {
    cout << 0 << endl;
    return 0;
  }

  int cb = d / 3, rb = 2 * (d / 3);

  if (R > rb || C < cb) {
    cout << 0 << endl;
    return 0;
  }

  int diff = C - cb;

  // cout << "here " << cb << " " << rb << " " << diff << endl;
  if (R != rb - diff) {
    cout << 0 << endl;
    return 0;
  }

  if (diff > d / 3) {
    cout << 0 << endl;
    return 0;
  }

  cout << modBinomial(d / 3, diff, MOD) << endl;
}
Problem p02862 (C++)

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

// const long long MOD = 1000000007;
const long long INF = 1LL << 60;
const int INT_INF = 1000000000;

// int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[8] = {-1, 0, 1, 1, -1, 1, 0, -1};

// 二項係数nCr
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() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll X, Y;
  cin >> X >> Y;
  ll ans = 0;
  if ((2 * X - Y) % 3 == 0 && (2 * Y - X) % 3 == 0) {
    COMinit();
    int x = (2 * X - Y) / 3;
    int y = (2 * Y - X) / 3;
    ans = COM(x + y, x);
    cout << ans << endl;
  } else
    cout << 0 << endl;
  return 0;
}


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

// const long long MOD = 1000000007;
const long long INF = 1LL << 60;
const int INT_INF = 1000000000;

// int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[8] = {-1, 0, 1, 1, -1, 1, 0, -1};

// 二項係数nCr
const int MAX = 5100000;
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() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll X, Y;
  cin >> X >> Y;
  ll ans = 0;
  if ((2 * X - Y) % 3 == 0 && (2 * Y - X) % 3 == 0) {
    COMinit();
    int x = (2 * X - Y) / 3;
    int y = (2 * Y - X) / 3;
    ans = COM(x + y, x);
    cout << ans << endl;
  } else
    cout << 0 << endl;
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

template <typename T> T extgcd(T a, T b, T &x, T &y) {
  T d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

// mを法としたときのaのモジュラ逆元を求める
template <typename T> T mod_inverse(T a, T m) {
  T x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

// return nCk % mod
template <typename T> T mod_comb(T n, T k, T mod) {
  T mul = 1, div = 1;
  for (T i = 0; i < k; i++) {
    mul *= (n - i);
    mul %= mod;
    div *= (i + 1);
    div %= mod;
  }
  return mul * mod_inverse(div, mod) % mod;
}
const long long MOD = 1000000007;
int main() {
  long long x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0) {
    cout << 0 << endl;
    return (0);
  }
  long long times = (x + y) / 3;
  long long a = 0;
  while (!(a + 2 * (times - a) == x && 2 * a + (times - a) == y)) {
    a++;
  }
  if (a < 0 || times - a < 0) {
    cout << 0 << endl;
  } else {
    cout << mod_comb(times, a, MOD) << endl;
  }
  return (0);
}


Fixed submission:
#include <iostream>
using namespace std;

template <typename T> T extgcd(T a, T b, T &x, T &y) {
  T d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

// mを法としたときのaのモジュラ逆元を求める
template <typename T> T mod_inverse(T a, T m) {
  T x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

// return nCk % mod
template <typename T> T mod_comb(T n, T k, T mod) {
  T mul = 1, div = 1;
  for (T i = 0; i < k; i++) {
    mul *= (n - i);
    mul %= mod;
    div *= (i + 1);
    div %= mod;
  }
  return mul * mod_inverse(div, mod) % mod;
}
const long long MOD = 1000000007;
int main() {
  long long x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0) {
    cout << 0 << endl;
    return (0);
  }
  long long times = (x + y) / 3;
  long long a = 0;
  while (!(a + 2 * (times - a) == x && 2 * a + (times - a) == y) &&
         a <= times) {
    a++;
  }
  if (a < 0 || times - a < 0) {
    cout << 0 << endl;
  } else {
    cout << mod_comb(times, a, MOD) << endl;
  }
  return (0);
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define dbg(x) cout << "( " << #x << " -> " << x << " )" << endl;
using namespace std;
ll Power(ll a, ll b, ll p) {
  ll res = 1;
  while (b > 0) {
    if (b % 2 != 0)
      res = ((res % p) * (a % p)) % p;
    a = ((a % p) * (a % p)) % p;
    b = b / 2;
  }
  return res;
}
ll fac[10000005];
ll infac[10000005];
int main() {
  ll x, y, mod = 1000000007;
  scanf("%lld%lld", &x, &y);
  fac[0] = 1;
  for (ll i = 1; i < 10000005; i++)
    fac[i] = ((fac[i - 1] % mod) * 1LL * i) % mod;
  infac[10000004] = Power(fac[10000004], mod - 2, mod);
  for (ll i = 10000003; i >= 0; i--)
    infac[i] = ((infac[i + 1] % mod) * 1LL * (i + 1) % mod) % mod;
  double m1 = (2.0 * x - y) / 3.0;
  double n1 = (2.0 * y - x) / 3.0;
  if (m1 == (ll)m1 && n1 == (ll)n1) {
    ll m = (ll)m1;
    ll n = (ll)n1;
    ll val = ((((fac[m + n]) * (infac[m])) % mod) * (infac[n])) % mod;
    printf("%lld\n", val);
  } else
    printf("0\n");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define dbg(x) cout << "( " << #x << " -> " << x << " )" << endl;
using namespace std;
ll Power(ll a, ll b, ll p) {
  ll res = 1;
  while (b > 0) {
    if (b % 2 != 0)
      res = ((res % p) * (a % p)) % p;
    a = ((a % p) * (a % p)) % p;
    b = b / 2;
  }
  return res;
}
ll fac[10000005];
ll infac[10000005];
int main() {
  ll x, y, mod = 1000000007;
  scanf("%lld%lld", &x, &y);
  fac[0] = 1;
  for (ll i = 1; i < 10000005; i++)
    fac[i] = ((fac[i - 1] % mod) * 1LL * i) % mod;
  infac[10000004] = Power(fac[10000004], mod - 2, mod);
  for (ll i = 10000003; i >= 0; i--)
    infac[i] = ((infac[i + 1] % mod) * 1LL * (i + 1) % mod) % mod;
  double m1 = (2.0 * x - y) / 3.0;
  double n1 = (2.0 * y - x) / 3.0;
  if (m1 == (ll)m1 && n1 == (ll)n1 && m1 >= 0 && n1 >= 0) {
    ll m = (ll)m1;
    ll n = (ll)n1;
    ll val = ((((fac[m + n]) * (infac[m])) % mod) * (infac[n])) % mod;
    printf("%lld\n", val);
  } else
    printf("0\n");
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define iint int
#define pll std::pair<iint, iint>
#define rep(i, a, b) for (iint i = (iint)a; i < (iint)b; i++)
#define per(i, a, b) for (iint i = (iint)a; i > (iint)b; i--)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define full(a) a.begin(), a.end()
#define rfull(a) a.rbegin(), a.rend()

using namespace std;
const double pi = acos(-1.0);
const double pii = 2 * pi;
const double eps = 1e-6;
const long long MOD = 1e9 + 7;
const long long mul = 1e6 + 1;

long long pow_mod(long long a, long long b) {
  if (a == 0)
    return 0ll;

  if (b == 0)
    return 1ll;

  if (b == 1)
    return a % MOD;

  long long x = pow_mod(a, b / 2);
  if (b % 2 == 1)
    return (((x * x) % MOD) * a) % MOD;

  return (x * x) % MOD;
}

void solve() {
  long long a, b, x, y;
  std::cin >> x >> y;
  a = 2 * y - x;
  b = 2 * x - y;

  if (a % 3 != 0 || b % 3 != 0) {
    std::cout << 0 << "\n";
    return;
  }

  a /= 3;
  b /= 3;

  std::vector<long long> dp(int(a + b + 1), 1ll);
  for (int i = 2; i < dp.size(); i++) {
    dp[i] = (dp[i - 1] * (1ll * i)) % MOD;
  }

  std::cout << (dp[a + b] * (pow_mod((dp[a] * dp[b]) % MOD, MOD - 2))) % MOD
            << "\n";
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(NULL);
  std::cout.tie(NULL);

  // int t;
  // std::cin >> t;
  // while(t--){
  // 	solve();
  // }

  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define iint int
#define pll std::pair<iint, iint>
#define rep(i, a, b) for (iint i = (iint)a; i < (iint)b; i++)
#define per(i, a, b) for (iint i = (iint)a; i > (iint)b; i--)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define full(a) a.begin(), a.end()
#define rfull(a) a.rbegin(), a.rend()

using namespace std;
const double pi = acos(-1.0);
const double pii = 2 * pi;
const double eps = 1e-6;
const long long MOD = 1e9 + 7;
const long long mul = 1e6 + 1;

long long pow_mod(long long a, long long b) {
  if (a == 0)
    return 0ll;

  if (b == 0)
    return 1ll;

  if (b == 1)
    return a % MOD;

  long long x = pow_mod(a, b / 2);
  if (b % 2 == 1)
    return (((x * x) % MOD) * a) % MOD;

  return (x * x) % MOD;
}

void solve() {
  long long a, b, x, y;
  std::cin >> x >> y;
  a = 2 * y - x;
  b = 2 * x - y;

  if (a < 0 || b < 0 || a % 3 != 0 || b % 3 != 0) {
    std::cout << 0 << "\n";
    return;
  }

  a /= 3;
  b /= 3;

  std::vector<long long> dp(int(a + b + 1), 1ll);
  for (int i = 2; i < dp.size(); i++) {
    dp[i] = (dp[i - 1] * (1ll * i)) % MOD;
  }

  std::cout << (dp[a + b] * (pow_mod((dp[a] * dp[b]) % MOD, MOD - 2))) % MOD
            << "\n";
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(NULL);
  std::cout.tie(NULL);

  // int t;
  // std::cin >> t;
  // while(t--){
  // 	solve();
  // }

  solve();
  return 0;
}
Problem p02862 (C++)

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

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

template <typename H> bool chmin(H &v1, const H v2) {
  if (v1 > v2) {
    v1 = v2;
    return true;
  }
  return false;
}
template <typename H> bool chmax(H &v1, const H v2) {
  if (v1 < v2) {
    v1 = v2;
    return true;
  }
  return false;
}
template <typename H> void read(H &head) { cin >> head; }
template <typename H, typename... T> void read(H &head, T &...tail) {
  cin >> head;
  read(tail...);
}
template <typename H> void write(H head) { cout << head << '\n'; }
template <typename H, typename... T> void write(H head, T... tail) {
  cout << head << " ";
  write(tail...);
}
template <typename... T> void die(T... tok) {
  write(tok...);
  exit(0);
}

const ll mod = 1'000'000'007;
ll fact[2000005];

ll bm(ll b, ll p, ll m) {
  ll r = 1;
  for (; p; p >>= 1, b = b * b % m)
    if (p & 1)
      r = r * b % m;
  return r;
}

ll modinv(ll b) { return bm(b, mod - 2, mod); }

ll ncr(ll n, ll r) {
  return fact[n] * modinv(fact[n - r]) % mod * modinv(fact[r]) % mod;
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  fact[0] = 1;
  rep(i, 1, 2000005) fact[i] = fact[i - 1] * i % mod;

  ll x, y;
  read(x, y);
  ll a = 2 * y - x;
  ll b = 2 * x - y;
  if (a % 3 != 0 || b % 3 != 0)
    die(0);
  a /= 3, b /= 3;

  die(ncr(a + b, a));
}


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

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

template <typename H> bool chmin(H &v1, const H v2) {
  if (v1 > v2) {
    v1 = v2;
    return true;
  }
  return false;
}
template <typename H> bool chmax(H &v1, const H v2) {
  if (v1 < v2) {
    v1 = v2;
    return true;
  }
  return false;
}
template <typename H> void read(H &head) { cin >> head; }
template <typename H, typename... T> void read(H &head, T &...tail) {
  cin >> head;
  read(tail...);
}
template <typename H> void write(H head) { cout << head << '\n'; }
template <typename H, typename... T> void write(H head, T... tail) {
  cout << head << " ";
  write(tail...);
}
template <typename... T> void die(T... tok) {
  write(tok...);
  exit(0);
}

const ll mod = 1'000'000'007;
ll fact[2000005];

ll bm(ll b, ll p, ll m) {
  ll r = 1;
  for (; p; p >>= 1, b = b * b % m)
    if (p & 1)
      r = r * b % m;
  return r;
}

ll modinv(ll b) { return bm(b, mod - 2, mod); }

ll ncr(ll n, ll r) {
  return fact[n] * modinv(fact[n - r]) % mod * modinv(fact[r]) % mod;
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  fact[0] = 1;
  rep(i, 1, 2000005) fact[i] = fact[i - 1] * i % mod;

  ll x, y;
  read(x, y);
  ll a = 2 * y - x;
  ll b = 2 * x - y;
  if (a % 3 != 0 || b % 3 != 0 || a < 0 || b < 0)
    die(0);
  a /= 3, b /= 3;

  die(ncr(a + b, a));
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

const ll MOD = 1e9 + 7;
ll kai[2000001];
ll gyaku[2000001];
ll kuri2(ll x, ll a) {
  ll ret = 1;
  ll tmp = x;
  while (a > 0) {
    if (a & 1) {
      ret *= tmp;
      ret %= MOD;
    }
    a >>= 1;
    tmp *= tmp;
    tmp %= MOD;
  }
  if (ret < 0)
    ret += MOD;
  return ret;
}
int main() {
  int x, y;
  int p, m, a, b;
  ll ans = 0;
  cin >> x >> y;
  kai[0] = 1;
  gyaku[0] = kuri2(kai[0], MOD - 2);
  for (ll i = 1; i < 2000001; i++) {
    kai[i] = kai[i - 1] * i;
    kai[i] %= MOD;
    if (kai[i] < 0)
      kai[i] += MOD;
    gyaku[i] = kuri2(kai[i], MOD - 2);
  }
  if ((x + y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  p = (x + y) / 3;
  m = x - y;
  a = (p + m) / 2;
  b = (p - m) / 2;
  if (a + b != p) {
    cout << 0 << endl;
    return 0;
  }
  ans = kai[p] * gyaku[a];
  ans %= MOD;
  ans *= gyaku[p - a];
  ans %= MOD;
  if (ans < 0)
    ans += MOD;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

const ll MOD = 1e9 + 7;
ll kai[2000001];
ll gyaku[2000001];
ll kuri2(ll x, ll a) {
  ll ret = 1;
  ll tmp = x;
  while (a > 0) {
    if (a & 1) {
      ret *= tmp;
      ret %= MOD;
    }
    a >>= 1;
    tmp *= tmp;
    tmp %= MOD;
  }
  if (ret < 0)
    ret += MOD;
  return ret;
}
int main() {
  int x, y;
  int p, m, a, b;
  ll ans = 0;
  cin >> x >> y;
  kai[0] = 1;
  gyaku[0] = kuri2(kai[0], MOD - 2);
  for (ll i = 1; i < 2000001; i++) {
    kai[i] = kai[i - 1] * i;
    kai[i] %= MOD;
    if (kai[i] < 0)
      kai[i] += MOD;
    gyaku[i] = kuri2(kai[i], MOD - 2);
  }
  if ((x + y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  p = (x + y) / 3;
  m = x - y;
  a = (p + m) / 2;
  b = (p - m) / 2;
  if (!(a + b == p && a >= 0 && b >= 0)) {
    cout << 0 << endl;
    return 0;
  }
  ans = kai[p] * gyaku[a];
  ans %= MOD;
  ans *= gyaku[p - a];
  ans %= MOD;
  if (ans < 0)
    ans += MOD;
  cout << ans << endl;
  return 0;
}
Problem p02862 (C++)

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

long long X, Y;
long long base = 1000000007LL;

long long kaijo(long long a) {
  if (a == 0)
    return 1LL;
  else {
    return a * kaijo(a - 1) % base;
  }
}

long long ruijo(long long a, long long b) {
  if (b == 0LL)
    return 1LL;
  else {
    if (b % 2 == 0)
      return (ruijo(a, b / 2) * ruijo(a, b / 2)) % base;
    else
      return (a * ruijo(a, b - 1)) % base;
  }
}

int main() {
  cin >> X >> Y;
  if ((X + Y) % 3LL != 0LL)
    cout << 0 << endl;
  else {
    long long k = (X + Y) / 3LL;
    long long x = (2 * Y - X) / 3LL;
    long long y = (2 * X - Y) / 3LL;
    long long a = (kaijo(x) * kaijo(y)) % base;
    cout << (kaijo(k) * ruijo(a, base - 2LL)) % base << endl;
  }
}

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

long long X, Y;
long long base = 1000000007LL;

long long kaijo(long long a) {
  if (a == 0)
    return 1LL;
  else {
    return a * kaijo(a - 1) % base;
  }
}

long long ruijo(long long a, long long b) {
  if (b == 0LL)
    return 1LL;
  else {
    if (b % 2 == 0)
      return (ruijo(a, b / 2) * ruijo(a, b / 2)) % base;
    else
      return (a * ruijo(a, b - 1)) % base;
  }
}

int main() {
  cin >> X >> Y;
  if ((X + Y) % 3LL != 0LL || Y > 2 * X || X > 2 * Y)
    cout << 0 << endl;
  else {
    long long k = (X + Y) / 3LL;
    long long x = (2 * Y - X) / 3LL;
    long long y = (2 * X - Y) / 3LL;
    long long a = (kaijo(x) * kaijo(y)) % base;
    cout << (kaijo(k) * ruijo(a, base - 2LL)) % base << endl;
  }
}
Problem p02862 (C++)

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

const int maxn = 1e6 + 6, M = 1e9 + 7;
int fac[maxn], ifac[maxn];
int mult(int a, int b) { return (1LL * a * b) % M; }
int modexp(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = mult(res, a);
    a = mult(a, a);
    b >>= 1;
  }
  return res;
}

int comb(int n, int r) { return mult(fac[n], mult(ifac[r], ifac[n - r])); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int x, y;
  cin >> x >> y;
  fac[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fac[i] = mult(fac[i - 1], i);
  }
  for (int i = 0; i < maxn; i++) {
    ifac[i] = modexp(fac[i], M - 2);
  }
  if ((2 * x - y) < 0 || (2 * x - y) % 3 != 0) {
    cout << 0 << '\n';
  } else {
    int b = (2 * x - y) / 3;
    int a = x - 2 * b;
    cout << comb(a + b, a) << '\n';
  }
}


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

const int maxn = 1e6 + 6, M = 1e9 + 7;
int fac[maxn], ifac[maxn];
int mult(int a, int b) { return (1LL * a * b) % M; }
int modexp(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = mult(res, a);
    a = mult(a, a);
    b >>= 1;
  }
  return res;
}

int comb(int n, int r) { return mult(fac[n], mult(ifac[r], ifac[n - r])); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int x, y;
  cin >> x >> y;
  fac[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fac[i] = mult(fac[i - 1], i);
  }
  for (int i = 0; i < maxn; i++) {
    ifac[i] = modexp(fac[i], M - 2);
  }
  if ((2 * x - y) < 0 || (2 * x - y) % 3 != 0) {
    cout << 0 << '\n';
  } else {
    int b = (2 * x - y) / 3;
    int a = x - 2 * b;
    if (a >= 0 && b >= 0) {
      cout << comb(a + b, a) << '\n';
    } else {
      cout << 0 << '\n';
    }
  }
}
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 M 1000000007
#define sz(a) (ll) a.size()
#define pll pair<ll, ll>
#define rep(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define sep(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define mll map<ll, ll>
#define vl vector<ll>
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define F first
#define S second
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int binpow(int a, int b, int mod) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * 1LL * a) % mod;
    a = (a * 1LL * a) % mod;
    b >>= 1;
  }
  return ans;
}
const ll P = 2000005;
ll fact[P], inv[P];
void preprocess() {
  fact[0] = 1;
  rep(i, 1, P) fact[i] = (i * fact[i - 1]) % M;
  inv[P - 1] = binpow(fact[P - 1], M - 2, M);
  sep(i, P - 2, 0) inv[i] = (inv[i + 1] * (i + 1)) % M;
}
ll C(ll n, ll r) { return (((fact[n] * inv[r]) % M) * inv[n - r]) % M; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  preprocess();
  ll x, y;
  cin >> x >> y;
  ll k = (2 * x - y);
  if (k % 3) {
    cout << 0 << "\n";
    return 0;
  }
  k /= 3;
  x -= 2 * k;
  if (x < 0) {
    cout << 0 << "\n";
    return 0;
  }
  cout << C(x + k, k);
}


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 M 1000000007
#define sz(a) (ll) a.size()
#define pll pair<ll, ll>
#define rep(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define sep(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define mll map<ll, ll>
#define vl vector<ll>
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define F first
#define S second
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int binpow(int a, int b, int mod) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * 1LL * a) % mod;
    a = (a * 1LL * a) % mod;
    b >>= 1;
  }
  return ans;
}
const ll P = 2000005;
ll fact[P], inv[P];
void preprocess() {
  fact[0] = 1;
  rep(i, 1, P) fact[i] = (i * fact[i - 1]) % M;
  inv[P - 1] = binpow(fact[P - 1], M - 2, M);
  sep(i, P - 2, 0) inv[i] = (inv[i + 1] * (i + 1)) % M;
}
ll C(ll n, ll r) { return (((fact[n] * inv[r]) % M) * inv[n - r]) % M; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  preprocess();
  ll x, y;
  cin >> x >> y;
  ll k = (2 * x - y);
  if (k % 3 or k < 0) {
    cout << 0 << "\n";
    return 0;
  }
  k /= 3;
  x -= 2 * k;
  if (x < 0) {
    cout << 0 << "\n";
    return 0;
  }
  cout << C(x + k, k);
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

const int mod = int(1e9) + 7;

int fact(int n) {
  if (n == 0)
    return 1;
  return ((long)n * (long)fact(n - 1)) % mod;
}

int pow(int n, int i) {
  long s = pow(n, i / 2);
  if (i % 2 == 0) {
    return (s * s) % mod;
  } else {
    return (((s * s) % mod) * (long)n) % mod;
  }
}

int comb(int n, int r) {
  if (r < 0 || r > n)
    return 0;
  long nf = fact(n), rf = fact(r), rnf = fact(n - r);
  return (((nf * pow(rf, mod - 2)) % mod) * pow(rnf, mod - 2)) % mod;
}

int main() {
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0) {
    cout << 0;
    return 0;
  }
  int n = (x + y) / 3;
  cout << comb(n, x - n);
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

const int mod = int(1e9) + 7;

int fact(int n) {
  if (n == 0)
    return 1;
  return ((long)n * (long)fact(n - 1)) % mod;
}

int pow(int n, int i) {
  if (i == 0)
    return 1;
  long s = pow(n, i / 2);
  if (i % 2 == 0) {
    return (s * s) % mod;
  } else {
    return (((s * s) % mod) * (long)n) % mod;
  }
}

int comb(int n, int r) {
  if (r < 0 || r > n)
    return 0;
  long nf = fact(n), rf = fact(r), rnf = fact(n - r);
  return (((nf * pow(rf, mod - 2)) % mod) * pow(rnf, mod - 2)) % mod;
}

int main() {
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0) {
    cout << 0;
    return 0;
  }
  int n = (x + y) / 3;
  cout << comb(n, x - n);
  return 0;
}
Problem p02862 (C++)

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

using namespace std;

typedef long long ll;
typedef long double ld;

const int MOD = (int)1e9 + 7;

int add(int a, int b) {
  a += b;
  if (a >= MOD) {
    return a - MOD;
  }
  if (a < 0) {
    return a + MOD;
  }
  return a;
}

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

int pw(int a, int b) {
  int r = 1;
  while (b) {
    if (b & 1) {
      r = mul(r, a);
    }
    a = mul(a, a);
    b /= 2;
  }
  return r;
}

const int N = (int)3e6 + 7;
int fact[N];

int comb(int n, int k) {
  int x = fact[n];
  int y = mul(fact[k], fact[n - k]);
  return mul(x, pw(y, MOD - 2));
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  fact[0] = 1;
  for (int i = 1; i < N; i++) {
    fact[i] = mul(fact[i - 1], i);
  }
  int x, y;
  cin >> x >> y;
  int t = x + y;
  if (t % 3) {
    cout << "0\n";
    return 0;
  }
  t /= 3;
  x -= t;
  y -= t;
  cout << comb(x + y, y) << "\n";
}


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

using namespace std;

typedef long long ll;
typedef long double ld;

const int MOD = (int)1e9 + 7;

int add(int a, int b) {
  a += b;
  if (a >= MOD) {
    return a - MOD;
  }
  if (a < 0) {
    return a + MOD;
  }
  return a;
}

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

int pw(int a, int b) {
  int r = 1;
  while (b) {
    if (b & 1) {
      r = mul(r, a);
    }
    a = mul(a, a);
    b /= 2;
  }
  return r;
}

const int N = (int)3e6 + 7;
int fact[N];

int comb(int n, int k) {
  int x = fact[n];
  int y = mul(fact[k], fact[n - k]);
  return mul(x, pw(y, MOD - 2));
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  fact[0] = 1;
  for (int i = 1; i < N; i++) {
    fact[i] = mul(fact[i - 1], i);
  }
  int x, y;
  cin >> x >> y;
  int t = x + y;
  if (t % 3) {
    cout << "0\n";
    return 0;
  }
  t /= 3;
  x -= t;
  y -= t;
  if (x < 0 || y < 0) {
    cout << "0\n";
    return 0;
  }
  cout << comb(x + y, y) << "\n";
}
Problem p02862 (C++)

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

using namespace std;

const int mod = 1e9 + 7;

int add(int a, int b) { return a + b - mod * (a + b >= mod); }

int mul(int a, int b) { return int(1ll * a * b % mod); }

int power(int a, int x) {
  if (x == 0) {
    return 1;
  }
  return mul(power(mul(a, a), x >> 1), (x & 1 ? a : 1));
}

int divide(int a, int b) { return mul(a, power(b, mod - 2)); }

int sub(int a, int b) { return a - b + mod * (a < b); }

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int x, y;
  cin >> x >> y;
  if (x > y) {
    swap(x, y);
  }
  if ((2 * y - x) % 3) {
    cout << 0;
    return 0;
  }
  int b = (2 * y - x) / 3;
  int a = y - 2 * b;
  vector<int> f(a + b + 1);
  f[0] = 1;
  for (int i = 1; i <= a + b; ++i) {
    f[i] = mul(f[i - 1], i);
  }
  cout << divide(f[a + b], mul(f[a], f[b]));

  return 0;
}

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

using namespace std;

const int mod = 1e9 + 7;

int add(int a, int b) { return a + b - mod * (a + b >= mod); }

int mul(int a, int b) { return int(1ll * a * b % mod); }

int power(int a, int x) {
  if (x == 0) {
    return 1;
  }
  return mul(power(mul(a, a), x >> 1), (x & 1 ? a : 1));
}

int divide(int a, int b) { return mul(a, power(b, mod - 2)); }

int sub(int a, int b) { return a - b + mod * (a < b); }

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int x, y;
  cin >> x >> y;
  if (x > y) {
    swap(x, y);
  }
  if ((2 * y - x) % 3) {
    cout << 0;
    return 0;
  }
  int b = (2 * y - x) / 3;
  int a = y - 2 * b;
  if (a < 0) {
    cout << 0;
    return 0;
  }
  vector<int> f(a + b + 1);
  f[0] = 1;
  for (int i = 1; i <= a + b; ++i) {
    f[i] = mul(f[i - 1], i);
  }
  cout << divide(f[a + b], mul(f[a], f[b]));

  return 0;
}
Problem p02862 (C++)

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

using namespace std;

static const int INF = 1e9 + 7;
vector<long long> fact;
vector<long long> revfact;

long long powMod(long long x, int k) {
  if (k == 0)
    return 1;
  if (k % 2 == 0)
    return powMod(x * x % INF, k / 2);
  else
    return x * powMod(x, k - 1) % INF;
}

void setfact(int N) {
  fact.resize(N + 1);
  revfact.resize(N + 1);
  fact[0] = 1;
  for (int i = 1; i < N + 1; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= INF;
  }
  revfact[N] = powMod(fact[N], INF - 2);
  for (int i = N - 1; i >= 0; i--) {
    revfact[i] = revfact[i + 1] * (i + 1);
    revfact[i] %= INF;
  }
}

long long getC(int a, int b) {
  return (((fact[a] * revfact[b]) % INF) * revfact[a - b]) % INF;
}

int main() {
  long long x, y;
  cin >> x >> y;
  long long a = 2 * y - x, b = 2 * x - y;
  if (a % 3 || b % 3) {
    cout << 0 << "\n";
    return 0;
  }
  a /= 3;
  b /= 3;
  setfact(a + b + 15);
  cout << getC(a + b, a) << "\n";
}

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

using namespace std;

static const int INF = 1e9 + 7;
vector<long long> fact;
vector<long long> revfact;

long long powMod(long long x, int k) {
  if (k == 0)
    return 1;
  if (k % 2 == 0)
    return powMod(x * x % INF, k / 2);
  else
    return x * powMod(x, k - 1) % INF;
}

void setfact(int N) {
  fact.resize(N + 1);
  revfact.resize(N + 1);
  fact[0] = 1;
  for (int i = 1; i < N + 1; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= INF;
  }
  revfact[N] = powMod(fact[N], INF - 2);
  for (int i = N - 1; i >= 0; i--) {
    revfact[i] = revfact[i + 1] * (i + 1);
    revfact[i] %= INF;
  }
}

long long getC(int a, int b) {
  return (((fact[a] * revfact[b]) % INF) * revfact[a - b]) % INF;
}

int main() {
  long long x, y;
  cin >> x >> y;
  long long a = 2 * y - x, b = 2 * x - y;
  if (a < 0 || b < 0 || a % 3 || b % 3) {
    cout << 0 << "\n";
    return 0;
  }
  a /= 3;
  b /= 3;
  setfact(a + b + 15);
  cout << getC(a + b, a) << "\n";
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e7 + 5;
const ll mod = 1e9 + 7;
ll fct[N], inv[N], invfct[N];
void init() {
  fct[0] = fct[1] = 1;
  inv[1] = 1;
  invfct[0] = invfct[1] = 1;
  for (ll i = 2; i < N; i++) {
    fct[i] = i * fct[i - 1] % mod;
    inv[i] = ll(mod - inv[mod % i]) * (mod / i) % mod;
    invfct[i] = ll(inv[i]) * invfct[i - 1] % mod;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  init();
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3)
    return cout << 0, 0;
  if (x > y)
    swap(x, y);
  ll n = (x + y) / 3;
  ll r = (2 * x - y) / 3;
  if (r > n)
    return cout << 0, 0;
  cout << ((fct[n] * invfct[r]) % mod * invfct[n - r]) % mod;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e7 + 5;
const ll mod = 1e9 + 7;
ll fct[N], inv[N], invfct[N];
void init() {
  fct[0] = fct[1] = 1;
  inv[1] = 1;
  invfct[0] = invfct[1] = 1;
  for (ll i = 2; i < N; i++) {
    fct[i] = i * fct[i - 1] % mod;
    inv[i] = ll(mod - inv[mod % i]) * (mod / i) % mod;
    invfct[i] = ll(inv[i]) * invfct[i - 1] % mod;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  init();
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3)
    return cout << 0, 0;
  if (x > y)
    swap(x, y);
  ll n = (x + y) / 3;
  ll r = (2 * x - y) / 3;
  if (r > n || r < 0)
    return cout << 0, 0;
  cout << ((fct[n] * invfct[r]) % mod * invfct[n - r]) % mod;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int ll
#define ll long long
// const ll mod = 998244353;
const ll mod = 1000000007;
const int MX = 1000000;
ll power(ll x, ll y, ll p) {
  ll res = 1ll;
  x = x % p;
  while (y) {
    if (y & 1ll)
      res = (res * x) % p;
    y = y >> 1ll;
    x = (x * x) % p;
  }
  return res;
}
int modInverse(int n, int p) { return power(n, p - 2, p); }
int fac[MX + 1];
int C(int n, int r, int p) {
  if (r == 0)
    return 1;
  return fac[n] * (modInverse(fac[r], p) * modInverse(fac[n - r], p) % p) % p;
}

void init(int p) {
  fac[0] = 1;
  for (int i = 1; i <= MX; i++)
    fac[i] = fac[i - 1] * i % p;
}

signed main() {
  int x, y;
  cin >> x >> y;
  int n = (x + y) / 3;
  if ((x + y) % 3)
    return cout << 0, 0;
  init(mod);
  x -= n;
  y -= n;
  cout << C(x + y, min(x, y), mod);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int ll
#define ll long long
// const ll mod = 998244353;
const ll mod = 1000000007;
const int MX = 1000000;
ll power(ll x, ll y, ll p) {
  ll res = 1ll;
  x = x % p;
  while (y) {
    if (y & 1ll)
      res = (res * x) % p;
    y = y >> 1ll;
    x = (x * x) % p;
  }
  return res;
}
int modInverse(int n, int p) { return power(n, p - 2, p); }
int fac[MX + 1];
int C(int n, int r, int p) {
  if (r == 0)
    return 1;
  return fac[n] * (modInverse(fac[r], p) * modInverse(fac[n - r], p) % p) % p;
}

void init(int p) {
  fac[0] = 1;
  for (int i = 1; i <= MX; i++)
    fac[i] = fac[i - 1] * i % p;
}

signed main() {
  int x, y;
  cin >> x >> y;
  int n = (x + y) / 3;
  if ((x + y) % 3)
    return cout << 0, 0;
  init(mod);
  x -= n;
  y -= n;
  if (x < 0 || y < 0)
    return cout << 0, 0;
  cout << C(x + y, min(x, y), mod);
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define endl '\n'
#define debug(X) cout << #X << " = " << X << endl

using namespace std;

const int MN = 1000010;
const int MD = 1e9 + 7;

long long fact[MN];

long long fastExp(long long b, long long e) {
  long long ans = 1;
  while (e > 0) {
    if (e & 1)
      ans = (ans * b) % MD;
    b = (b * b) % MD;
    e >>= 1;
  }
  return ans;
}

int comb(int a, int b) {
  long long p = fact[a];
  long long q = (fact[b] * fact[a - b]) % MD;
  return (p * fastExp(q, MD - 2)) % MD;
}

int solve(int x, int y) {
  if ((x + y) % 3)
    return 0;
  int n = (x + y) / 3;
  if (2 * n - x < 0)
    return 0;
  return comb(n, 2 * n - x);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifdef UTP
  freopen("input.txt", "r", stdin);
#endif
  fact[0] = 1;
  for (int i = 1; i < MN; i++)
    fact[i] = (fact[i - 1] * i) % MD;
  int x, y;
  cin >> x >> y;
  int ans = solve(x, y);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define endl '\n'
#define debug(X) cout << #X << " = " << X << endl

using namespace std;

const int MN = 1000010;
const int MD = 1e9 + 7;

long long fact[MN];

long long fastExp(long long b, long long e) {
  long long ans = 1;
  while (e > 0) {
    if (e & 1)
      ans = (ans * b) % MD;
    b = (b * b) % MD;
    e >>= 1;
  }
  return ans;
}

int comb(int a, int b) {
  long long p = fact[a];
  long long q = (fact[b] * fact[a - b]) % MD;
  return (p * fastExp(q, MD - 2)) % MD;
}

int solve(int x, int y) {
  if ((x + y) % 3)
    return 0;
  int n = (x + y) / 3;
  if (2 * n - x < 0 || 2 * n - x > n)
    return 0;
  return comb(n, 2 * n - x);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifdef UTP
  freopen("input.txt", "r", stdin);
#endif
  fact[0] = 1;
  for (int i = 1; i < MN; i++)
    fact[i] = (fact[i - 1] * i) % MD;
  int x, y;
  cin >> x >> y;
  int ans = solve(x, y);
  cout << ans << endl;
  return 0;
}
Problem p02862 (C++)

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

vector<long long> fact, fact_inv, inv;

template <typename T> void init_combi(size_t size, const T mod) {
  size += 1;
  fact.resize(size);
  fact_inv.resize(size);
  inv.resize(size);
  fact[0] = fact[1] = 1;
  fact_inv[0] = fact_inv[1] = 1;
  inv[1] = 1;
  for (int i = 1; i < size - 1; i++) {
    fact[i + 1] = fact[i] * (i + 1) % mod;
    inv[i + 1] = mod - inv[mod % (i + 1)] * (mod / (i + 1)) % mod;
    fact_inv[i + 1] = fact_inv[i] * inv[i + 1] % mod;
  }
}

template <typename T> T mod_combi(T n, T k, T mod) {
  // return kCn % mod
  return fact[n] * (fact_inv[k] * fact_inv[n - k] % mod) % mod;
}

int main() {
  int X, Y;
  cin >> X >> Y;
  if ((X + Y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  // X = a + 2b
  // Y = 2a + b
  // A = a + b = (X + Y) / 3
  int A = (X + Y) / 3;
  // B = a - b = (Y - X)
  int B = Y - X;
  if ((A + B) % 2 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll a = (A + B) / 2;
  if ((A - B) % 2 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll b = (A - B) / 2;
  init_combi((size_t)a + b, MOD);
  cout << mod_combi(a + b, a, MOD) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = (ll)1e9 + 7;

vector<long long> fact, fact_inv, inv;

template <typename T> void init_combi(size_t size, const T mod) {
  size += 1;
  fact.resize(size);
  fact_inv.resize(size);
  inv.resize(size);
  fact[0] = fact[1] = 1;
  fact_inv[0] = fact_inv[1] = 1;
  inv[1] = 1;
  for (int i = 1; i < size - 1; i++) {
    fact[i + 1] = fact[i] * (i + 1) % mod;
    inv[i + 1] = mod - inv[mod % (i + 1)] * (mod / (i + 1)) % mod;
    fact_inv[i + 1] = fact_inv[i] * inv[i + 1] % mod;
  }
}

template <typename T> T mod_combi(T n, T k, T mod) {
  // return kCn % mod
  return fact[n] * (fact_inv[k] * fact_inv[n - k] % mod) % mod;
}

int main() {
  int X, Y;
  cin >> X >> Y;
  if ((X + Y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  // X = a + 2b
  // Y = 2a + b
  // A = a + b = (X + Y) / 3
  int A = (X + Y) / 3;
  // B = a - b = (Y - X)
  int B = Y - X;
  if ((A + B) % 2 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll a = (A + B) / 2;
  if ((A - B) % 2 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll b = (A - B) / 2;
  if (a < 0 || b < 0) {
    cout << 0 << endl;
    return 0;
  }
  init_combi((size_t)a + b, MOD);
  cout << mod_combi(a + b, a, MOD) << endl;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define _USE_MATH_DEFINES
#include <algorithm>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define print(s) cout << s << endl
#define acc(v) accumulate(v.begin(), v.end(), 0)

using namespace std;
typedef long long ll;
const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;

const int MAX = 100100;

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();

  ll x, y;
  cin >> x >> y;
  ll over = 2 * x - y;
  // a->1,2  b->2,1
  if (over % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll a, b;
  b = over / 3;
  a = x - b * 2;
  ll ans = COM(a + b, a);

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define _USE_MATH_DEFINES
#include <algorithm>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define print(s) cout << s << endl
#define acc(v) accumulate(v.begin(), v.end(), 0)

using namespace std;
typedef long long ll;
const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;

const int MAX = 2000100;

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();

  ll x, y;
  cin >> x >> y;
  ll over = 2 * x - y;
  // a->1,2  b->2,1
  if (over % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll a, b;
  b = over / 3;
  a = x - b * 2;
  ll ans = COM(a + b, a);

  cout << ans << endl;
}
Problem p02862 (C++)

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

using namespace std;
using ll = long long int;

template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto i = v.begin(); i != v.end(); i++) {
    os << *i << " ";
  }
  return os;
}

const long long MOD = 1000000007;
ll f[1010101], rf[1010101];
ll inv(ll x) {
  ll res = 1;
  ll k = MOD - 2;
  ll 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 (ll i = 1; i < 1010101; i++)
    f[i] = (f[i - 1] * i) % MOD;

  for (ll i = 0; i < 1010101; i++)
    rf[i] = inv(f[i]);
}
//---------------------------------------------------------------------------------------------------
ll C(int n, int k) {
  ll a = f[n];      // = n!
  ll b = rf[n - k]; // = (n-k)!
  ll c = rf[k];     // = k!

  ll bc = (b * c) % MOD;

  return (a * bc) % MOD;
}

void solve(long long X, long long Y) {
  init();
  if ((2 * X - Y) % 3 != 0) {
    std::cout << 0 << std::endl;
    return;
  }
  ll n = (2 * X - Y) / 3;
  std::cout << C(X - n, n) << std::endl;
}

int main() {
  long long X;
  scanf("%lld", &X);
  long long Y;
  scanf("%lld", &Y);
  solve(X, Y);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long int;

template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto i = v.begin(); i != v.end(); i++) {
    os << *i << " ";
  }
  return os;
}

const long long MOD = 1000000007;
ll f[1010101], rf[1010101];
ll inv(ll x) {
  ll res = 1;
  ll k = MOD - 2;
  ll 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 (ll i = 1; i < 1010101; i++)
    f[i] = (f[i - 1] * i) % MOD;

  for (ll i = 0; i < 1010101; i++)
    rf[i] = inv(f[i]);
}
//---------------------------------------------------------------------------------------------------
ll C(int n, int k) {
  if (k < 0 or n < k)
    return 0;
  ll a = f[n];      // = n!
  ll b = rf[n - k]; // = (n-k)!
  ll c = rf[k];     // = k!

  ll bc = (b * c) % MOD;

  return (a * bc) % MOD;
}

void solve(long long X, long long Y) {
  init();
  if ((2 * X - Y) % 3 != 0) {
    std::cout << 0 << std::endl;
    return;
  }
  ll n = (2 * X - Y) / 3;
  std::cout << C(X - n, n) << std::endl;
}

int main() {
  long long X;
  scanf("%lld", &X);
  long long Y;
  scanf("%lld", &Y);
  solve(X, Y);
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
// #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int mod = 1e9 + 7;
const int N = 2e6 + 5;
long long fac[N];
long long inv[N];
long long quickmod(long long a, long long b) {

  long long ans = 1;
  while (b) {
    if (b % 2 == 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b = b / 2;
  }
  return ans;
}
inline long long C(int n, int m) {
  return fac[n] * inv[m] % mod * inv[n - m] % mod;
}

int main() {
  int n, m;
  fac[0] = 1;
  for (int i = 1; i < N; i++)
    fac[i] = fac[i - 1] * i % mod;
  inv[N - 1] = quickmod(fac[N - 1], mod - 2);
  for (int i = N - 2; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % mod;
  while (cin >> n >> m) {
    int a = n, b = 0;
    while (2 * a + b > m) {
      a -= 2;
      b++;
    }
    if (a + b * 2 != n || a * 2 + b != m) {
      printf("0\n");
      continue;
    }
    printf("%lld\n", C(a + b, a));
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
// #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int mod = 1e9 + 7;
const int N = 2e6 + 5;
long long fac[N];
long long inv[N];
long long quickmod(long long a, long long b) {

  long long ans = 1;
  while (b) {
    if (b % 2 == 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b = b / 2;
  }
  return ans;
}
inline long long C(int n, int m) {
  return fac[n] * inv[m] % mod * inv[n - m] % mod;
}

int main() {
  int n, m;
  fac[0] = 1;
  for (int i = 1; i < N; i++)
    fac[i] = fac[i - 1] * i % mod;
  inv[N - 1] = quickmod(fac[N - 1], mod - 2);
  for (int i = N - 2; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % mod;
  while (cin >> n >> m) {
    int a = n, b = 0;
    while (2 * a + b > m) {
      a -= 2;
      b++;
    }
    if (a + b * 2 != n || a * 2 + b != m || a + b <= 0 || a < 0 || b < 0) {
      printf("0\n");
      continue;
    }
    printf("%lld\n", C(a + b, a));
  }
  return 0;
}