Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr ll INF = 9e18;
constexpr int inf = 1e9;
const double INFD = 1e100;
const ll mod = 1000000007;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
};

// intやllをstringに変換
template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
};
// ----------------------------------------------------------------------------

ll Pow(ll x, ll p) {
  if (!p)
    return 1;
  if (p % 2) {
    return (x * Pow(x, p - 1)) % mod;
  }
  ll t = Pow(x, p >> 1);
  return (t * t) % mod;
}

struct combination {
  vector<ll> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    fact[0] = 1;
    for (ll i = 1; i <= n; i++)
      fact[i] = (fact[i - 1] * i) % mod;
    ifact[n] = Pow(fact[n], mod - 2);
    for (ll i = n; i >= 1; i--)
      ifact[i - 1] = (ifact[i] * i) % mod;
  }
  ll operator()(ll n, ll k) {
    return (((fact[n] * ifact[k]) % mod) * ifact[n - k]) % mod;
  }
};

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


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr ll INF = 9e18;
constexpr int inf = 1e9;
const double INFD = 1e100;
const ll mod = 1000000007;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
};

// intやllをstringに変換
template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
};
// ----------------------------------------------------------------------------

ll Pow(ll x, ll p) {
  if (!p)
    return 1;
  if (p % 2) {
    return (x * Pow(x, p - 1)) % mod;
  }
  ll t = Pow(x, p >> 1);
  return (t * t) % mod;
}

struct combination {
  vector<ll> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    fact[0] = 1;
    for (ll i = 1; i <= n; i++)
      fact[i] = (fact[i - 1] * i) % mod;
    ifact[n] = Pow(fact[n], mod - 2);
    for (ll i = n; i >= 1; i--)
      ifact[i - 1] = (ifact[i] * i) % mod;
  }
  ll operator()(ll n, ll k) {
    return (((fact[n] * ifact[k]) % mod) * ifact[n - k]) % mod;
  }
};

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