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

Buggy submission (status: Runtime Error):
/**
 * いやなんでこれでサンプルと合わないのか全然分からない
 **/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

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(ll n, ll 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;
  cin >> X >> Y;

  if (X == Y * 2 || X * 2 == Y) {
    cout << 1 << "\n";
    return 0;
  }
  if ((X % 3 == 1 && Y % 3 == 2) || (X % 3 == 2 && Y % 3 == 1) ||
      (X % 3 == 0 && Y % 3 == 0)) {
    ll A = abs(X - Y);
    ll B = (min(X, Y) - A) / 3;
    A += B;
    cout << COM(A + B, A) << "\n";
    return 0;
  }
  cout << 0 << "\n";
  return 0;
}


Fixed submission:
/**
 * いやなんでこれでサンプルと合わないのか全然分からない
 **/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

const int MAX = 910000;
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(ll n, ll 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;
  cin >> X >> Y;

  if (X == Y * 2 || X * 2 == Y) {
    cout << 1 << "\n";
    return 0;
  }
  if ((X % 3 == 1 && Y % 3 == 2) || (X % 3 == 2 && Y % 3 == 1) ||
      (X % 3 == 0 && Y % 3 == 0)) {
    ll A = abs(X - Y);
    ll B = (min(X, Y) - A) / 3;
    A += B;
    cout << COM(A + B, A) << "\n";
    return 0;
  }
  cout << 0 << "\n";
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
const int INF = 1 << 29;

const int MOD = 1000000007;
long long ruizyou(int m, int n) {
  if (m == 0)
    return 0;
  if (m == 1)
    return 1;
  long long ans = 1;
  long long tmp = m;
  for (int i = 0; i <= 30; i++) {
    if (n & (1 << i)) {
      ans *= tmp;
      ans %= MOD;
    }
    tmp *= tmp;
    tmp %= MOD;
  }
  return ans;
}
long long kaizyou(int x) {
  if (x == 1)
    return 1;
  return x * kaizyou(x - 1) % MOD;
}
long long comb(int x, int y) {
  long long bunsi = kaizyou(x);
  long long bunbo = kaizyou(x - y) * kaizyou(y) % MOD;
  return bunsi * ruizyou((int)bunbo, MOD - 2) % MOD;
}

int main() {
  int X, Y;
  cin >> X >> Y;
  double x, y;
  x = (-1 / 3.0) * (X - 2 * Y);
  y = (-1 / 3.0) * (-2 * X + Y);
  if (abs(x - int(x)) > 0.01 || abs(y - int(y)) > 0.01 || x < 0 || y < 0) {
    cout << 0 << endl;
  } else {
    cout << comb(int(x + y), int(x)) << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
const int INF = 1 << 29;

const int MOD = 1000000007;
long long ruizyou(int m, int n) {
  if (m == 0)
    return 0;
  if (m == 1)
    return 1;
  long long ans = 1;
  long long tmp = m;
  for (int i = 0; i <= 30; i++) {
    if (n & (1 << i)) {
      ans *= tmp;
      ans %= MOD;
    }
    tmp *= tmp;
    tmp %= MOD;
  }
  return ans;
}
long long kaizyou(int x) {
  if (x == 0)
    return 1;
  return x * kaizyou(x - 1) % MOD;
}
long long comb(int x, int y) {
  long long bunsi = kaizyou(x);
  long long bunbo = kaizyou(x - y) * kaizyou(y) % MOD;
  return bunsi * ruizyou((int)bunbo, MOD - 2) % MOD;
}

int main() {
  int X, Y;
  cin >> X >> Y;
  double x, y;
  x = (-1 / 3.0) * (X - 2 * Y);
  y = (-1 / 3.0) * (-2 * X + Y);
  if (abs(x - int(x)) > 0.01 || abs(y - int(y)) > 0.01 || x < 0 || y < 0) {
    cout << 0 << endl;
  } else {
    cout << comb(int(x + y), int(x)) << endl;
  }
}
Problem p02862 (Python)

Buggy submission (status: Runtime Error):
from sys import stdin, setrecursionlimit

setrecursionlimit(10**9)
INF = 1 << 60


def input():
    return stdin.readline().strip()


MOD = 1000000007
X, Y = map(int, input().split())

n = (X + Y) // 3
x = (2 * X - Y) // 3
y = (-X + 2 * Y) // 3
if (X + Y) % 3 or x < 0 or y < 0:
    print(0)
    exit()


def mod_com(n, k):
    if n < k:
        return 0
    if n < 0 or k < 0:
        return 0
    if k > n - k:
        k = n - k
    ans = 1
    for i in range(n - k + 1, n + 1):
        ans = ans * i % MOD

    inv = [0] * (k + 1)
    inv[1] = 1
    for i in range(2, k + 1):
        inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
        ans = ans * inv[i] % MOD

    return ans


print(mod_com(n, x))


Fixed submission:
from sys import stdin, setrecursionlimit

setrecursionlimit(10**9)
INF = 1 << 60


def input():
    return stdin.readline().strip()


MOD = 1000000007
X, Y = map(int, input().split())

n = (X + Y) // 3
x = (2 * X - Y) // 3
y = (-X + 2 * Y) // 3
if (X + Y) % 3 or x < 0 or y < 0:
    print(0)
    exit()


def mod_com(n, k):
    if n < k:
        return 0
    if n < 0 or k < 0:
        return 0
    if k > n - k:
        k = n - k
    if k == 0:
        return 1
    ans = 1
    for i in range(n - k + 1, n + 1):
        ans = ans * i % MOD

    inv = [0] * (k + 1)
    inv[1] = 1
    for i in range(2, k + 1):
        inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
        ans = ans * inv[i] % MOD

    return ans


print(mod_com(n, x))
Problem p02862 (Python)

Buggy submission (status: Runtime Error):
MOD = 10**9 + 7


def prepare(n):
    global MOD
    modFacts = [0] * (n + 1)
    modFacts[0] = 1
    for i in range(n):
        modFacts[i + 1] = (modFacts[i] * (i + 1)) % MOD

    invs = [1] * (n + 1)
    invs[n] = pow(modFacts[n], MOD - 2, MOD)
    for i in range(n, 1, -1):
        invs[i - 1] = (invs[i] * i) % MOD

    return modFacts, invs


X, Y = map(int, input().split())
if (X + Y) % 3 == 0:
    p = (2 * Y - X) // 3
    q = (2 * X - Y) // 3
    n = p + q
    r = min(p, q)
    modFacts, invs = prepare(n)
    ans = (modFacts[n] * invs[n - r] * invs[r]) % MOD
else:
    ans = 0

print(ans)


Fixed submission:
MOD = 10**9 + 7


def prepare(n):
    global MOD
    modFacts = [0] * (n + 1)
    modFacts[0] = 1
    for i in range(n):
        modFacts[i + 1] = (modFacts[i] * (i + 1)) % MOD

    invs = [1] * (n + 1)
    invs[n] = pow(modFacts[n], MOD - 2, MOD)
    for i in range(n, 1, -1):
        invs[i - 1] = (invs[i] * i) % MOD

    return modFacts, invs


X, Y = map(int, input().split())
if (X + Y) % 3 == 0 and 2 * Y - X >= 0 and 2 * X - Y >= 0:
    p = (2 * Y - X) // 3
    q = (2 * X - Y) // 3
    n = p + q
    r = min(p, q)
    modFacts, invs = prepare(n)
    ans = (modFacts[n] * invs[n - r] * invs[r]) % MOD
else:
    ans = 0

print(ans)
Problem p02862 (Python)

Buggy submission (status: Runtime Error):
import sys

X, Y = map(int, input().split())

# 直交座標に直す
if (2 * X - Y) % 3 != 0 or (2 * Y - X) % 3 != 0:
    print(0)
    sys.exit()
x, y = (2 * X - Y) // 3, (2 * Y - X) // 3


def comb_inv(n, k, mod):
    f = [1 for _ in range(n + 1)]
    inv = [1 for _ in range(n + 1)]
    finv = [1 for _ in range(n + 1)]

    for i in range(2, n + 1):
        f[i] = f[i - 1] * i % mod
        inv[i] = mod - inv[mod % i] * (mod // i) % mod
        finv[i] = finv[i - 1] * inv[i] % mod
    return f[n] * (finv[k] * finv[n - k] % mod) % mod


print(comb_inv(x + y, x, 10**9 + 7))


Fixed submission:
import sys

X, Y = map(int, input().split())

# 直交座標に直す
if X > 2 * Y or Y > 2 * X or (2 * X - Y) % 3 != 0 or (2 * Y - X) % 3 != 0:
    print(0)
    sys.exit()
x, y = (2 * X - Y) // 3, (2 * Y - X) // 3


def comb_inv(n, k, mod):
    f = [1 for _ in range(n + 1)]
    inv = [1 for _ in range(n + 1)]
    finv = [1 for _ in range(n + 1)]

    for i in range(2, n + 1):
        f[i] = f[i - 1] * i % mod
        inv[i] = mod - inv[mod % i] * (mod // i) % mod
        finv[i] = finv[i - 1] * inv[i] % mod
    return f[n] * (finv[k] * finv[n - k] % mod) % mod


print(comb_inv(x + y, x, 10**9 + 7))
Problem p02862 (Python)

Buggy submission (status: Runtime Error):
from functools import reduce


def modcombination(n, k, mod):
    # nCk % mod
    num = reduce(lambda x, y: x * y % mod, range(n, n - k, -1))
    den = reduce(lambda x, y: x * y % mod, range(1, k + 1))
    return num * pow(den, mod - 2, mod) % mod


if __name__ == "__main__":
    MOD = 10**9 + 7
    x, y = map(int, input().split())
    if (x + y) % 3 != 0:
        print(0)
        exit(0)

    m = (2 * x - y) // 3
    n = x - (2 * m)
    if n < 0 or m < 0:
        print(0)
        exit(0)
    print(modcombination(n + m, n, MOD) % MOD)


Fixed submission:
from functools import reduce


def modcombination(n, k, mod):
    # nCk % mod
    num = reduce(lambda x, y: x * y % mod, range(n, n - k, -1))
    den = reduce(lambda x, y: x * y % mod, range(1, k + 1))
    return num * pow(den, mod - 2, mod) % mod


if __name__ == "__main__":
    MOD = 10**9 + 7
    x, y = map(int, input().split())
    if (x + y) % 3 != 0:
        print(0)
        exit(0)

    m = (2 * x - y) // 3
    n = x - (2 * m)
    if n < 0 or m < 0:
        print(0)
        exit(0)
    if n == 0 or m == 0:
        print(1)
        exit(0)
    print(modcombination(n + m, n, MOD) % MOD)
Problem p02862 (Python)

Buggy submission (status: Runtime Error):
#!/usr/bin/python3

import os
import sys


def main():
    X, Y = read_ints()
    print(solve(X, Y))


MOD = 10**9 + 7


def mpow(a, n):
    p = a
    v = 1
    while n > 0:
        if n & 1:
            v *= p
            v %= MOD
        n >>= 1
        p *= p
        p %= MOD
    return v


def solve(X, Y):
    s = X + Y
    if s % 3 != 0:
        return 0
    s //= 3

    a = 2 * s - X
    b = 2 * s - Y
    c = a + b

    facts = [0] * (c + 1)
    facts[0] = 1
    for i in range(1, c + 1):
        facts[i] = (facts[i - 1] * i) % MOD

    return (facts[c] * mpow(facts[a], MOD - 2) * mpow(facts[b], MOD - 2)) % MOD


###############################################################################

DEBUG = "DEBUG" in os.environ


def inp():
    return sys.stdin.readline().rstrip()


def read_int():
    return int(inp())


def read_ints():
    return [int(e) for e in inp().split()]


def dprint(*value, sep=" ", end="\n"):
    if DEBUG:
        print(*value, sep=sep, end=end)


if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/python3

import os
import sys


def main():
    X, Y = read_ints()
    print(solve(X, Y))


MOD = 10**9 + 7


def mpow(a, n):
    p = a
    v = 1
    while n > 0:
        if n & 1:
            v *= p
            v %= MOD
        n >>= 1
        p *= p
        p %= MOD
    return v


def solve(X, Y):
    s = X + Y
    if s % 3 != 0:
        return 0
    s //= 3

    a = 2 * s - X
    b = 2 * s - Y
    if a < 0 or b < 0:
        return 0

    c = a + b

    facts = [0] * (c + 1)
    facts[0] = 1
    for i in range(1, c + 1):
        facts[i] = (facts[i - 1] * i) % MOD

    return (facts[c] * mpow(facts[a], MOD - 2) * mpow(facts[b], MOD - 2)) % MOD


###############################################################################

DEBUG = "DEBUG" in os.environ


def inp():
    return sys.stdin.readline().rstrip()


def read_int():
    return int(inp())


def read_ints():
    return [int(e) for e in inp().split()]


def dprint(*value, sep=" ", end="\n"):
    if DEBUG:
        print(*value, sep=sep, end=end)


if __name__ == "__main__":
    main()
Problem p02862 (Python)

Buggy submission (status: Time Limit Exceeded):
LARGE = 10**9 + 7


def solve(x, y):
    if (x + y) % 3 != 0:
        return 0
    z = (x + y) // 3
    if x < z or y < z:
        return 0
    # zC(x-z)
    r = min(x - z, y - z)
    res = 1
    for i in range(r):
        res *= z - i
        res *= pow(i + 1, LARGE - 2, LARGE)
        res %= LARGE
    return res


def main():
    x, y = map(int, input().split())
    res = solve(x, y)
    print(res)


def test():
    assert solve(3, 3) == 2
    assert solve(2, 2) == 0
    assert solve(999999, 999999) == 151840682


if __name__ == "__main__":
    test()
    main()


Fixed submission:
LARGE = 10**9 + 7


def solve(x, y):
    if (x + y) % 3 != 0:
        return 0
    z = (x + y) // 3
    if x < z or y < z:
        return 0
    # zC(x-z)
    r = min(x - z, y - z)
    res = 1
    for i in range(r):
        res *= z - i
        res *= pow(i + 1, LARGE - 2, LARGE)
        res %= LARGE
    return res


def main():
    x, y = map(int, input().split())
    res = solve(x, y)
    print(res)


def test():
    assert solve(3, 3) == 2
    assert solve(2, 2) == 0
    assert solve(999999, 999999) == 151840682


if __name__ == "__main__":
    # test()
    main()
Problem p02862 (Python)

Buggy submission (status: Runtime Error):
import sys

input = sys.stdin.readline

_x, _y = map(int, input().split())


def combination(n, r, mod=10**9 + 7):
    n1, r = n + 1, min(r, n - r)
    numer = denom = 1
    for i in range(1, r + 1):
        numer = numer * (n1 - i) % mod
        denom = denom * i % mod
    return numer * pow(denom, mod - 2, mod) % mod


X, Y = map(int, input().split())
if (X + Y) % 3 != 0 or not (X <= 2 * Y and Y <= 2 * X):
    print(0)
    exit()

n = (X + Y) // 3
r = Y - n
print(combination(n, r))


def nCk(n, k, m=10**9 + 7):
    a = b = 1
    k = min(k, n - k)
    for i in range(k):
        a = (a * (n - i)) % m
        b = (b * (i + 1)) % m

    return (a * pow(b, m - 2, m)) % m


def chker(x, y):
    if (x + y) % 3 != 0 or x > 2 * y or y > 2 * x:
        print(0)
        return

    if x == y:
        b = x // 3
        print(nCk(2 * b, b))
        return

    x, y = (x, y) if x < y else (y, x)

    b = 2 * x - y
    if b % 3 != 0:
        print(0)
        return

    d1, d2 = b // 3, x - 2 * (b // 3)
    u = d1 + d2

    print(nCk(u, d1))


chker(_x, _y)


Fixed submission:
import sys

input = sys.stdin.readline

_x, _y = map(int, input().split())


def nCk(n, k, m=10**9 + 7):
    a = b = 1
    k = min(k, n - k)
    for i in range(k):
        a = (a * (n - i)) % m
        b = (b * (i + 1)) % m

    return (a * pow(b, m - 2, m)) % m


def chker(x, y):
    if (x + y) % 3 != 0 or x > 2 * y or y > 2 * x:
        print(0)
        return

    if x == y:
        b = x // 3
        print(nCk(2 * b, b))
        return

    x, y = (x, y) if x < y else (y, x)

    b = 2 * x - y
    if b % 3 != 0:
        print(0)
        return

    d1, d2 = b // 3, x - 2 * (b // 3)
    u = d1 + d2

    print(nCk(u, d1))


chker(_x, _y)
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;
using ll = long long;
using P = pair<int, int>;

ll nck(ll n, ll k) {

  ll p = 1000000007;
  ll p2;
  p2 = p - 2;
  vector<int> r(35);
  vector<ll> r2(35);

  rep(j, 35) { r[j] = (p2 >> j) & 1; }

  ll ans;
  ans = 1;

  if (k != 0) {
    rep(i, k - 1) {

      r2[0] = i + 2;
      rep(j, 35) {

        r2[j + 1] = (r2[j] * r2[j]) % p;
        if (r[j] == 1) {
          ans *= r2[j];
          ans = ans % p;
        }
      }
    }

    rep(i, k) {
      ans *= n - i;
      ans = ans % p;
    }
  }

  return ans;
}

int main() {
  ll x, y, a, b;
  cin >> x >> y;
  int k;
  k = 0;
  if ((2 * y - x) % 3 != 0 or (2 * x - y) % 3 != 0) {
    k = 1;
  }
  ll m;
  a = 2 * y - x;
  b = 2 * x - y;

  if (a < 0 or b < 0) {
    k = 2;
  }
  a /= 3;
  b /= 3;
  m = min(a, b);

  ll ans;

  if (k == 0) {
    ans = nck(a + b, m);
  }
  if (k == 1 or k == 2) {
    cout << 0 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}

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

ll nck(ll n, ll k) {

  ll p = 1000000007;
  ll p2;
  p2 = p - 2;
  vector<int> r(35);
  vector<ll> r2(36);

  rep(j, 35) { r[j] = (p2 >> j) & 1; }

  ll ans;
  ans = 1;

  if (k != 0) {
    rep(i, k - 1) {

      r2[0] = i + 2;
      rep(j, 35) {

        r2[j + 1] = (r2[j] * r2[j]) % p;
        if (r[j] == 1) {
          ans *= r2[j];
          ans = ans % p;
        }
      }
    }

    rep(i, k) {
      ans *= n - i;
      ans = ans % p;
    }
  }

  return ans;
}

int main() {
  ll x, y, a, b;
  cin >> x >> y;
  int k;
  k = 0;
  if ((2 * y - x) % 3 != 0 or (2 * x - y) % 3 != 0) {
    k = 1;
  }
  ll m;
  a = 2 * y - x;
  b = 2 * x - y;

  if (a < 0 or b < 0) {
    k = 2;
  }
  a /= 3;
  b /= 3;
  m = min(a, b);

  ll ans;

  if (k == 0) {
    ans = nck(a + b, m);
  }
  if (k == 1 or k == 2) {
    cout << 0 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}
Problem p02862 (C++)

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

const ll M = 1e9 + 7;

vector<ll> fac(1000001);  // n!(mod M)
vector<ll> ifac(1000001); // k!^{M-2} (mod M)
// a,bの範囲的にこれだけ配列を用意していけば十分

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 % M;
    x = x * x % M;
    n = n >> 1;
  }
  return ans;
}

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

int main() {
  cin >> X >> Y;
  ll k, n;
  if ((X + Y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  } else {
    n = (X + Y) / 3;
    k = X - n;
  }
  if (n < k || n < 0) {
    cout << 0 << endl;
    return 0;
  }

  fac[0] = 1;
  ifac[0] = 1;
  for (ll i = 0; i <= 1000000; 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を採用
  }
  // cout << n <<" " << k << endl;
  ll ans = comb(n, k) % M;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll mod = 1e9 + 7;
ll X, Y;

const ll M = 1e9 + 7;

vector<ll> fac(1000001);  // n!(mod M)
vector<ll> ifac(1000001); // k!^{M-2} (mod M)
// a,bの範囲的にこれだけ配列を用意していけば十分

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 % M;
    x = x * x % M;
    n = n >> 1;
  }
  return ans;
}

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

int main() {
  cin >> X >> Y;
  ll k, n;
  if ((X + Y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  } else {
    n = (X + Y) / 3;
    k = X - n;
  }
  if (n < k || k < 0) {
    cout << 0 << endl;
    return 0;
  }

  fac[0] = 1;
  ifac[0] = 1;
  for (ll i = 0; i <= 1000000; 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を採用
  }
  // cout << n <<" " << k << endl;
  ll ans = comb(n, k) % M;
  cout << ans << endl;
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define Hello                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl '\n'
#define ll long long
const int mod = 1e9 + 7, N = 2e6 + 5;
int x, y;
ll fact[N], inv[N];
ll fp(ll base, ll exp) {
  if (exp == 0)
    return 1;
  ll ans = fp(base, exp / 2);
  if (exp % 2 == 0)
    return (ans * ans) % mod;
  else
    return (((ans * ans) % mod) * base) % mod;
}
void calcFacAndInv() {
  fact[0] = inv[0] = 1;
  for (ll i = 1; i <= N; i++) {
    fact[i] = (i * fact[i - 1]) % mod;
    inv[i] = fp(fact[i], mod - 2);
  }
}
ll ncr(ll n, ll r) { return ((fact[n] * inv[r]) % mod * inv[n - r]) % mod; }

int main() {
  Hello calcFacAndInv();
  cin >> x >> y;
  if ((x + y) % 3 || (2 * x - y) < 0)
    cout << 0;
  else
    cout << ncr((x + y) / 3, (2 * x - y) / 3);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define Hello                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl '\n'
#define ll long long
const int mod = 1e9 + 7, N = 2e6 + 5;
int x, y;
ll fact[N], inv[N];
ll fp(ll base, ll exp) {
  if (exp == 0)
    return 1;
  ll ans = fp(base, exp / 2);
  if (exp % 2 == 0)
    return (ans * ans) % mod;
  else
    return (((ans * ans) % mod) * base) % mod;
}
void calcFacAndInv() {
  fact[0] = inv[0] = 1;
  for (ll i = 1; i <= N; i++) {
    fact[i] = (i * fact[i - 1]) % mod;
    inv[i] = fp(fact[i], mod - 2);
  }
}
ll ncr(ll n, ll r) {
  if (n - r < 0) {
    cout << 0;
    exit(0);
  }
  return ((fact[n] * inv[r]) % mod * inv[n - r]) % mod;
}
int main() {
  Hello calcFacAndInv();
  cin >> x >> y;
  if ((x + y) % 3 || (2 * x - y) < 0)
    cout << 0;
  else
    cout << ncr((x + y) / 3, (2 * x - y) / 3);
  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)
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() {
  int x, y;
  cin >> x >> y;
  int a, b;
  ll ans = 0;
  COMinit();
  if ((2 * y - x) % 3 == 0 && (2 * x - y) % 3 == 0) {
    a = (2 * y - x) / 3;
    b = (2 * x - y) / 3;
    cout << COM(a + b, a) << endl;
  } else
    cout << 0 << endl;
}


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

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() {
  int x, y;
  cin >> x >> y;
  int a, b;
  ll ans = 0;
  COMinit();
  if ((2 * y - x) % 3 == 0 && (2 * x - y) % 3 == 0) {
    a = (2 * y - x) / 3;
    b = (2 * x - y) / 3;
    cout << COM(a + b, a) << endl;
  } else
    cout << 0 << endl;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int64_t, int64_t> pll;
typedef vector<int64_t> vl;
int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
const long double PI = acos(-1);
#define INF (2147483647)
#define mod (1000000007)
#define limit (7368791)
#define rep(i, a, b) for (int64_t i = (a); i < (b); i++)
#define REP(i, n) rep(i, 0, n)
#define sz(s) (s).size()
#define ALL(a) begin(a), end(a)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

const int MAX = 1010000;
ll fact[MAX], fact_inv[MAX];

ll power(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

void init(int n) {
  fact[0] = 1;
  for (int i = 0; i < n; i++)
    fact[i + 1] = fact[i] * (i + 1) % mod;
  fact_inv[n] = power(fact[n], mod - 2);
  for (int i = n - 1; i >= 0; i--)
    fact_inv[i] = fact_inv[i + 1] * (i + 1) % mod;
}

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

void solve() {
  init(1010000);
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3) {
    cout << 0 << endl;
    return;
  }
  int m = (2 * x - y) / 3, n = (2 * y - x) / 3;
  cout << comb(n + m, n) << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int64_t, int64_t> pll;
typedef vector<int64_t> vl;
int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
const long double PI = acos(-1);
#define INF (2147483647)
#define mod (1000000007)
#define limit (7368791)
#define rep(i, a, b) for (int64_t i = (a); i < (b); i++)
#define REP(i, n) rep(i, 0, n)
#define sz(s) (s).size()
#define ALL(a) begin(a), end(a)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

const int MAX = 1010000;
ll fact[MAX], fact_inv[MAX];

ll power(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

void init(int n) {
  fact[0] = 1;
  for (int i = 0; i < n; i++)
    fact[i + 1] = fact[i] * (i + 1) % mod;
  fact_inv[n] = power(fact[n], mod - 2);
  for (int i = n - 1; i >= 0; i--)
    fact_inv[i] = fact_inv[i + 1] * (i + 1) % mod;
}

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

void solve() {
  init(1010000);
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3) {
    cout << 0 << endl;
    return;
  }
  int m = (2 * x - y) / 3, n = (2 * y - x) / 3;
  if (n < 0 || m < 0) {
    cout << 0 << endl;
    return;
  }
  cout << comb(n + m, n) << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>

using namespace std;

#define int long long

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

int fac[N];
int inv[N];

inline void add(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}

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

int fp(int a, int b) {
  int ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans = mul(ans, a);
    }
    b >>= 1;
    a = mul(a, a);
  }
  return ans;
}

int inver(int x) { return fp(x, mod - 2); }

int comb(int n, int k) {
  int ans = (1ll * fac[n] * inv[k]) % mod;
  ans = (1ll * ans * inv[n - k]) % mod;
  return ans;
}

int32_t main() {
  int x, y;
  cin >> x >> y;
  fac[0] = 1;
  for (int i = 1; i < N; i++)
    fac[i] = (1ll * i * fac[i - 1]) % mod;
  for (int i = 0; i < N; i++)
    inv[i] = inver(fac[i]);
  int ans = 0;
  for (int i = 0; i <= x; i++) {
    if (2 * i > y)
      break;
    int aa = x - i, bb = y - 2 * i;
    if (aa % 2 != 0 || aa / bb != 2 || aa % bb != 0)
      continue;
    add(ans, comb(bb + i, bb));
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>

using namespace std;

#define int long long

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

int fac[N];
int inv[N];

inline void add(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}

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

int fp(int a, int b) {
  int ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans = mul(ans, a);
    }
    b >>= 1;
    a = mul(a, a);
  }
  return ans;
}

int inver(int x) { return fp(x, mod - 2); }

int comb(int n, int k) {
  int ans = (1ll * fac[n] * inv[k]) % mod;
  ans = (1ll * ans * inv[n - k]) % mod;
  return ans;
}

int32_t main() {
  int x, y;
  cin >> x >> y;
  fac[0] = 1;
  for (int i = 1; i < N; i++)
    fac[i] = (1ll * i * fac[i - 1]) % mod;
  for (int i = 0; i < N; i++)
    inv[i] = inver(fac[i]);
  int ans = 0;
  for (int i = 0; i <= x; i++) {
    if (2 * i > y)
      break;
    int aa = x - i, bb = y - 2 * i;
    if (bb == 0) {
      if (aa == 0)
        add(ans, 1);
      continue;
    }
    if (aa % 2 != 0 || aa / bb != 2 || aa % bb != 0)
      continue;
    add(ans, comb(bb + i, bb));
  }
  cout << ans << endl;
  return 0;
}
Problem p02862 (C++)

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

#define ll long long
#define ld long double
#define ft first
#define sd second
#define pb push_back
using namespace std;
const ll mod = 1e9 + 7;

ll power(ll a, ll b, ll m = mod) {
  ll x = 1;
  while (b) {
    if (b & 1) {
      x = 1ll * x * a % m;
    }
    a = 1ll * a * a % m;
    b /= 2;
  }
  return x;
}

const int N = 2e6 + 9;
// int a[N];
ll f[N], invf[N];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  f[0] = invf[0] = 1;
  for (int i = 1; i < N - 4; i++) {
    f[i] = f[i - 1] * i % mod;
    invf[i] = power(f[i], mod - 2);
  }

  int x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0) {
    return cout << "0\n", 0;
  }
  int n = (x + y) / 3;
  int a = x - n, b = y - n;
  // a = 5; b = 0;
  int ans = 0;
  if (a == 0 || b == 0) {
    ans = 1;
  } else if (a < 0 && b < 0) {
    ans = 0;
  } else {
    ans = f[a + b] * invf[a] % mod * invf[b] % mod;
  }
  cout << ans << endl;

  return 0;
}


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

#define ll long long
#define ld long double
#define ft first
#define sd second
#define pb push_back
using namespace std;
const ll mod = 1e9 + 7;

ll power(ll a, ll b, ll m = mod) {
  ll x = 1;
  while (b) {
    if (b & 1) {
      x = 1ll * x * a % m;
    }
    a = 1ll * a * a % m;
    b /= 2;
  }
  return x;
}

const int N = 2e6 + 9;
// int a[N];
ll f[N], invf[N];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  f[0] = invf[0] = 1;
  for (int i = 1; i < N - 4; i++) {
    f[i] = f[i - 1] * i % mod;
    invf[i] = power(f[i], mod - 2);
  }

  int x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0) {
    return cout << "0\n", 0;
  }
  int n = (x + y) / 3;
  int a = x - n, b = y - n;
  // a = 5; b = 0;
  int ans = 0;
  if (a == 0 || b == 0) {
    ans = 1;
  } else if (a < 0 || b < 0) {
    ans = 0;
  } else {
    ans = f[a + b] * invf[a] % mod * invf[b] % mod;
  }
  cout << ans << endl;

  return 0;
}
Problem p02862 (C++)

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

const long long md = 1e9 + 7; // mod
const int maxn = 1001000;
long long fac[maxn], inv[maxn];

// cal x^y in logy order
long long powd(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1)
      res = res * x % md;
    x = x * x % md;
    y >>= 1;
  }
  return res;
}
// calc n! by fermat's little theory
void csh() {
  fac[0] = 1;
  for (int i = 1; i <= 1000000; i++)
    fac[i] = fac[i - 1] * i % md;
  inv[1000000] = powd(fac[1000000], md - 2);
  for (int i = 1000000; i >= 1; i--)
    inv[i - 1] = inv[i] * i % md;
}
// calc combination in xlonx
long long C(int x, int y) { return fac[x] * inv[y] % md * inv[x - y] % md; }

int main() {
  csh();
  int X, Y;
  cin >> X >> Y;
  if ((X + Y) % 3 != 0) {
    cout << 0 << endl;
  } else {
    cout << C((X + Y) / 3, (2 * Y - X) / 3) << endl;
  }
}


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

const long long md = 1e9 + 7; // mod
const int maxn = 1001000;
long long fac[maxn], inv[maxn];

// cal x^y in logy order
long long powd(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1)
      res = res * x % md;
    x = x * x % md;
    y >>= 1;
  }
  return res;
}
// calc n! by fermat's little theory
void csh() {
  fac[0] = 1;
  for (int i = 1; i <= 1000000; i++)
    fac[i] = fac[i - 1] * i % md;
  inv[1000000] = powd(fac[1000000], md - 2);
  for (int i = 1000000; i >= 1; i--)
    inv[i - 1] = inv[i] * i % md;
}
// calc combination in xlonx
long long C(int x, int y) { return fac[x] * inv[y] % md * inv[x - y] % md; }

int main() {
  csh();
  int X, Y;
  cin >> X >> Y;
  if ((X + Y) % 3 != 0 || 2 * Y - X < 0 || 2 * X - Y < 0) {
    cout << 0 << endl;
  } else {
    cout << C((X + Y) / 3, (2 * Y - X) / 3) << endl;
  }
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define ll long long
#define INF 1000000007

ll f[1000001];

ll pow(ll a, ll b, ll MOD) {
  ll x = 1, y = a;
  while (b > 0) {
    if (b % 2 == 1) {
      x = (x * y);
      if (x > MOD)
        x %= MOD;
    }
    y = (y * y);
    if (y > MOD)
      y %= MOD;
    b /= 2;
  }
  return x;
}

ll InverseEuler(ll n, ll MOD) { return pow(n, MOD - 2, MOD); }

ll C(ll n, ll r, ll MOD) {

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

int main() {
  f[0] = 1;
  for (int i = 1; i <= 1000000; i++)
    f[i] = (f[i - 1] * i) % INF;
  ll x, y;
  cin >> x >> y;
  ll a = (x + y) / 3;
  ll b = 2 * a - x;
  if ((x + y) % 3 || b > a) {
    cout << 0;
    return 0;
  }
  cout << C(a, b, INF);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define ll long long
#define INF 1000000007

ll f[1000001];

ll pow(ll a, ll b, ll MOD) {
  ll x = 1, y = a;
  while (b > 0) {
    if (b % 2 == 1) {
      x = (x * y);
      if (x > MOD)
        x %= MOD;
    }
    y = (y * y);
    if (y > MOD)
      y %= MOD;
    b /= 2;
  }
  return x;
}

ll InverseEuler(ll n, ll MOD) { return pow(n, MOD - 2, MOD); }

ll C(ll n, ll r, ll MOD) {

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

int main() {
  f[0] = 1;
  for (int i = 1; i <= 1000000; i++)
    f[i] = (f[i - 1] * i) % INF;
  ll x, y;
  cin >> x >> y;
  ll a = (x + y) / 3;
  ll b = 2 * a - x;
  if ((x + y) % 3 || b > a || b < 0) {
    cout << 0;
    return 0;
  }
  cout << C(a, b, INF);
}
Problem p02862 (C++)

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

using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define INF LONG_MAX
#define MOD 1000000007
#define rng(a) a.begin(), a.end()
#define rrng(a) a.end(), a.begin()

#define Max 10000000

ll fact[Max], fact_inv[Max];

ll power(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;
}

ll comb(ll n, ll r) {
  return (fact[n] * fact_inv[r]) % MOD * fact_inv[n - r] % MOD;
}

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

  fact[0] = 1;
  for (ll i = 0; i < Max - 1; i++)
    fact[i + 1] = fact[i] * (i + 1) % MOD;
  fact_inv[Max - 1] = power(fact[Max - 1], MOD - 2);
  for (ll i = Max - 2; i >= 0; i--)
    fact_inv[i] = fact_inv[i + 1] * (i + 1) % MOD;
  ll X, Y;
  cin >> X >> Y;
  ll D = X + Y;
  if (D % 3) {
    cout << 0 << endl;
    return 0;
  }
  D /= 3;
  ll DD = abs(X - Y);
  ll n = (D + 1) / 2 + DD;
  cout << comb(D, n) << endl;
  return 0;
}


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

using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define INF LONG_MAX
#define MOD 1000000007
#define rng(a) a.begin(), a.end()
#define rrng(a) a.end(), a.begin()

#define Max 10000000

ll fact[Max], fact_inv[Max];

ll power(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;
}

ll comb(ll n, ll r) {
  return (fact[n] * fact_inv[r]) % MOD * fact_inv[n - r] % MOD;
}

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

  fact[0] = 1;
  for (ll i = 0; i < Max - 1; i++)
    fact[i + 1] = fact[i] * (i + 1) % MOD;
  fact_inv[Max - 1] = power(fact[Max - 1], MOD - 2);
  for (ll i = Max - 2; i >= 0; i--)
    fact_inv[i] = fact_inv[i + 1] * (i + 1) % MOD;
  ll X, Y;
  cin >> X >> Y;
  ll D = X + Y;
  if (D % 3) {
    cout << 0 << endl;
    return 0;
  }
  D /= 3;
  ll DD = abs(X - Y);
  ll n = (D + 1 - DD) / 2 + DD;
  if (n < 0 || n > D) {
    cout << 0 << endl;
    return 0;
  }
  cout << comb(D, n) << endl;
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <set>
#include <unordered_set>
#include <vector>

using namespace std;

#define sqr(a) ((a) * (a))
#define all(a) (a).begin(), (a).end()

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

long long fact[(int)2e6 + 5];
long long ob[(int)2e6 + 5];

long long bin_pow(long long a, long long b) {
  if (b == 0)
    return 1;

  long long ans = bin_pow(a, b / 2);
  ans = ans * ans % MOD;
  if (b % 2)
    ans = ans * a % MOD;

  return ans;
}

long long C(int n, int k) {
  // cout << n << " " << k << "\n";
  // cout << fact[n] << " " << ob[k] << " " << ob[n - k] << "\n";

  return fact[n] * ob[k] % MOD * ob[n - k] % MOD;
}

int main() {
  // freopen("input.txt", "r", stdin);
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  fact[0] = 1;
  ob[0] = 1;
  for (int i = 1; i <= (int)2e6; ++i) {
    (fact[i] = fact[i - 1] * i) %= MOD;
    ob[i] = bin_pow(fact[i], MOD - 2);
  }

  long long x, y;
  cin >> x >> y;

  if ((x + y) % 3) {
    cout << "0\n";
    return 0;
  }

  cout << C((x + y) / 3, x - ((x + y) / 3)) << "\n";
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <set>
#include <unordered_set>
#include <vector>

using namespace std;

#define sqr(a) ((a) * (a))
#define all(a) (a).begin(), (a).end()

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

long long fact[(int)2e6 + 5];
long long ob[(int)2e6 + 5];

long long bin_pow(long long a, long long b) {
  if (b == 0)
    return 1;

  long long ans = bin_pow(a, b / 2);
  ans = ans * ans % MOD;
  if (b % 2)
    ans = ans * a % MOD;

  return ans;
}

long long C(int n, int k) {
  // cout << n << " " << k << "\n";
  // cout << fact[n] << " " << ob[k] << " " << ob[n - k] << "\n";

  return fact[n] * ob[k] % MOD * ob[n - k] % MOD;
}

int main() {
  // freopen("input.txt", "r", stdin);
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  fact[0] = 1;
  ob[0] = 1;
  for (int i = 1; i <= (int)2e6; ++i) {
    (fact[i] = fact[i - 1] * i) %= MOD;
    ob[i] = bin_pow(fact[i], MOD - 2);
  }

  long long x, y;
  cin >> x >> y;

  if ((x + y) % 3 || (x + y) / 3 > x || (x + y) / 3 > y) {
    cout << "0\n";
    return 0;
  }

  cout << C((x + y) / 3, x - ((x + y) / 3)) << "\n";
}
Problem p02862 (C++)

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

const int64_t mod = 1000000007;

int64_t power(int64_t n, int64_t k) {
  if (k == 0)
    return 1;
  int64_t half = power(n, k / 2), half2 = half * half % mod;
  if (k % 2) {
    return n * half2 % mod;
  } else {
    return half2;
  }
}

int main() {
  int x0, y0, n, x, y;
  cin >> x0 >> y0;
  if ((x0 + y0) % 3) {
    cout << 0 << endl;
    return 0;
  }
  n = (x0 + y0) / 3;
  x = x0 - n;
  y = n - x;
  vector<int64_t> fact(n + 1, 1), inv(n + 1);
  for (int i = 1; i <= n; i++) {
    fact.at(i) = fact.at(i - 1) * i % mod;
    inv.at(i) = power(fact.at(i), mod - 2);
  }
  cout << fact.at(n) * inv.at(x) % mod * inv.at(y) % mod << endl;
}


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

const int64_t mod = 1000000007;

int64_t power(int64_t n, int64_t k) {
  if (k == 0)
    return 1;
  int64_t half = power(n, k / 2), half2 = half * half % mod;
  if (k % 2) {
    return n * half2 % mod;
  } else {
    return half2;
  }
}

int main() {
  int x0, y0, n, x, y;
  cin >> x0 >> y0;
  if ((x0 + y0) % 3) {
    cout << 0 << endl;
    return 0;
  }
  n = (x0 + y0) / 3;
  x = x0 - n;
  y = n - x;
  if (x < 0 || y < 0) {
    cout << 0 << endl;
    return 0;
  }
  vector<int64_t> fact(n + 1, 1), inv(n + 1, 1);
  for (int i = 1; i <= n; i++) {
    fact.at(i) = fact.at(i - 1) * i % mod;
    inv.at(i) = power(fact.at(i), mod - 2);
  }
  cout << fact.at(n) * inv.at(x) % mod * inv.at(y) % mod << endl;
}
Problem p02862 (C++)

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

using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
#define N_MAX 2000001

ll inv[N_MAX], fac[N_MAX], finv[N_MAX];

void init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < N_MAX; i++) {
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    fac[i] = fac[i - 1] * (ll)i % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

ll inv_(ll n) {
  if (n == 1)
    return 1;
  else
    return MOD - inv_(MOD % n) * (MOD / n) % MOD;
}

ll comb(ll n, ll r) {
  ll ans;
  if (n < r) {
    ans = 0;
  } else {
    ans = (fac[n] * finv[r]) % MOD;
    ans = (ans * finv[n - r]) % MOD;
    ans = (ans + MOD) % MOD;
  }
  return ans;
}

int main() {
  init();
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  ll X, Y;
  cin >> X >> Y;
  if ((X + Y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll N = (X + Y) / 3;
  ll n = X - N;
  cout << comb(N, n) << endl;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
#define N_MAX 2000001

ll inv[N_MAX], fac[N_MAX], finv[N_MAX];

void init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < N_MAX; i++) {
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    fac[i] = fac[i - 1] * (ll)i % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

ll inv_(ll n) {
  if (n == 1)
    return 1;
  else
    return MOD - inv_(MOD % n) * (MOD / n) % MOD;
}

ll comb(ll n, ll r) {
  ll ans;
  if (n < r) {
    ans = 0;
  } else {
    ans = (fac[n] * finv[r]) % MOD;
    ans = (ans * finv[n - r]) % MOD;
    ans = (ans + MOD) % MOD;
  }
  return ans;
}

int main() {
  init();
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  ll X, Y;
  cin >> X >> Y;
  if ((X + Y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll N = (X + Y) / 3;
  ll n = X - N;
  if (n < 0) {
    cout << 0 << endl;
    return 0;
  }
  cout << comb(N, n) << endl;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, s, n) for (ll i = s; i < n; i++)
#define repe(i, s, n) for (ll i = s; i <= n; i++)
#define rep_r(i, s, n) for (ll i = n - 1; i > s; i--)
#define rep_re(i, s, n) for (ll i = n - 1; i >= s; i--)
ll MOD = 1e9 + 7;

static const ll FACT_SIZE = 10000000;
ll fact[FACT_SIZE] = {};

ll Pow(ll n, ll p) {
  if (p == 0)
    return 1;
  if (p == 1)
    return n;
  ll t = Pow(n, p / 2);
  if (p & 1)
    return (t * t % MOD) * n % MOD;
  else
    return t * t % MOD;
}
void factorial(ll f) {
  fact[0] = fact[1] = 1;
  rep(i, 1, FACT_SIZE) fact[i] = fact[i - 1] * i % MOD;
}

ll nCr(ll n, ll r) {
  if (fact[0] == 0)
    factorial(FACT_SIZE);
  return ((fact[n] * (Pow(fact[r], MOD - 2) % MOD)) % MOD *
          Pow(fact[n - r], MOD - 2)) %
         MOD;
}
int main() {
  ll x, y;
  cin >> x >> y;

  if ((x + y) % 3) {
    cout << 0 << endl;
    return 0;
  }
  ll m = (2 * x - y) / 3 % MOD;
  ll n = (2 * y - x) / 3 % MOD;

  cout << nCr(m + n, m) << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, s, n) for (ll i = s; i < n; i++)
#define repe(i, s, n) for (ll i = s; i <= n; i++)
#define rep_r(i, s, n) for (ll i = n - 1; i > s; i--)
#define rep_re(i, s, n) for (ll i = n - 1; i >= s; i--)
ll MOD = 1e9 + 7;

static const ll FACT_SIZE = 10000000;
ll fact[FACT_SIZE] = {};

ll Pow(ll n, ll p) {
  if (p == 0)
    return 1;
  if (p == 1)
    return n;
  ll t = Pow(n, p / 2);
  if (p & 1)
    return (t * t % MOD) * n % MOD;
  else
    return t * t % MOD;
}
void factorial(ll f) {
  fact[0] = fact[1] = 1;
  rep(i, 1, FACT_SIZE) fact[i] = fact[i - 1] * i % MOD;
}

ll nCr(ll n, ll r) {
  if (fact[0] == 0)
    factorial(FACT_SIZE);
  return ((fact[n] * (Pow(fact[r], MOD - 2) % MOD)) % MOD *
          Pow(fact[n - r], MOD - 2)) %
         MOD;
}
int main() {
  ll x, y;
  cin >> x >> y;

  if ((x + y) % 3) {
    cout << 0 << endl;
    return 0;
  }
  ll m = (2 * x - y) / 3;
  ll n = (2 * y - x) / 3;

  if (m < 0 || n < 0) {
    cout << 0;
    return 0;
  }

  cout << nCr(m + n, m) << endl;
}