Problem p02862 (C++)

Buggy submission (status: Runtime Error):
// ~maqsat~
/*
 */

// #pragma GCC target ("avx2")
// #pragma GCC optimization ("Ofast")
// #pragma GCC optimization ("unroll-loops")

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define F first
#define S second
#define in insert
#define pb push_back
#define eb emplace_back
#define sz(x) int(x.size())
#define all(x) x.begin(), x.end()
#define count1 __builtin_popcountl
#define debug(x) cerr << (#x) << " = " << (x) << "\n"
#define ACCELERATE ios_base::sync_with_stdio(false), cin.tie(nullptr)
#define fre(f)                                                                 \
  if (fopen(f ".in", "r"))                                                     \
  freopen(f ".in", "r", stdin), freopen(f ".out", "w", stdout)

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using ull = unsigned long long;
// using big = __int128_t; // -10^38...10^38
using db = double;
using ld = long double;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;

template <class T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class T> inline void cmin(T &a, T b) { a = min(a, b); }
template <class T> inline void cmax(T &a, T b) { a = max(a, b); }

const int oo = 0x3f3f3f3f;
const ll N = 4e6 + 345;
const ll MOD = 1e9 + 7;
const ll INF = 1e10 + 9;
const db EPS = 1e-9;
const db PI = acos(-1); // 3.14159265358979323846
const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
const int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};

#define int ll

ll f[N];

inline ll binpow(ll a, ll n) {
  ll res = 1;
  while (n) {
    if (n & 1ll)
      res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1ll;
  }
  return res;
}

signed main() {
  fre("");
  ACCELERATE;

  f[0] = f[1] = 1;
  for (int i = 2; i <= N - 100; i++)
    f[i] = f[i - 1] * i % MOD;

  ll x, y;
  cin >> x >> y;

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

  ll n = (x + y) / 3;
  ll k = (x - y + n) / 2;

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

  cout << f[n] * binpow(f[k], MOD - 2) % MOD * binpow(f[n - k], MOD - 2) % MOD;

  return 0;
}
/*

*/


Fixed submission:
// ~maqsat~
/*
 */

// #pragma GCC target ("avx2")
// #pragma GCC optimization ("Ofast")
// #pragma GCC optimization ("unroll-loops")

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define F first
#define S second
#define in insert
#define pb push_back
#define eb emplace_back
#define sz(x) int(x.size())
#define all(x) x.begin(), x.end()
#define count1 __builtin_popcountl
#define debug(x) cerr << (#x) << " = " << (x) << "\n"
#define ACCELERATE ios_base::sync_with_stdio(false), cin.tie(nullptr)
#define fre(f)                                                                 \
  if (fopen(f ".in", "r"))                                                     \
  freopen(f ".in", "r", stdin), freopen(f ".out", "w", stdout)

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using ull = unsigned long long;
// using big = __int128_t; // -10^38...10^38
using db = double;
using ld = long double;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;

template <class T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class T> inline void cmin(T &a, T b) { a = min(a, b); }
template <class T> inline void cmax(T &a, T b) { a = max(a, b); }

const int oo = 0x3f3f3f3f;
const ll N = 4e6 + 345;
const ll MOD = 1e9 + 7;
const ll INF = 1e10 + 9;
const db EPS = 1e-9;
const db PI = acos(-1); // 3.14159265358979323846
const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
const int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};

#define int ll

ll f[N];

inline ll binpow(ll a, ll n) {
  ll res = 1;
  while (n) {
    if (n & 1ll)
      res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1ll;
  }
  return res;
}

signed main() {
  fre("");
  ACCELERATE;

  f[0] = f[1] = 1;
  for (int i = 2; i <= N - 100; i++)
    f[i] = f[i - 1] * i % MOD;

  ll x, y;
  cin >> x >> y;

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

  ll n = (x + y) / 3;
  ll k = (x - y + n) / 2;

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

  cout << f[n] * binpow(f[k], MOD - 2) % MOD * binpow(f[n - k], MOD - 2) % MOD;

  return 0;
}
/*

*/