Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdbool>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

typedef long long ll;

#define rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const long long INF = 1000000000000000;
const ll inf = -1e18;
typedef pair<ll, ll> P;
ll ma = 1000000000 + 7;
ll mx = 1000003;
ll h, w, m;
string s;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
ll gcd(ll x, ll y) {
  if (x % y == 0)
    return y;
  return gcd(y, x % y);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
void comb(vector<vector<ll>> &v) {
  for (ll i = 0; i < v.size(); i++) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (ll k = 1; k < v.size(); k++) {
    for (int j = 1; j < k; j++) {
      v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]) % ma;
    }
  }
}

ll GetDigit(ll num) { return log10(num) + 1; }
ll Combination(int n, int r) {
  if (n - r < r)
    r = n - r;
  if (r == 0)
    return 1;
  if (r == 1)
    return n;

  vector<ll> numerator(r);
  vector<ll> denominator(r);

  for (int k = 0; k < r; k++) {
    numerator[k] = n - r + k + 1;
    denominator[k] = k + 1;
  }

  for (int p = 2; p <= r; p++) {
    int pivot = denominator[p - 1];
    if (pivot > 1) {
      int offset = (n - r) % p;
      for (int k = p - 1; k < r; k += p) {
        numerator[k - offset] /= pivot;
        denominator[k] /= pivot;
      }
    }
  }

  ll result = 1;
  for (int k = 0; k < r; k++) {
    if (numerator[k] > 1)
      result *= numerator[k];
    result %= ma;
  }

  return result;
}
int main() {
  ll x, y;
  cin >> x >> y;
  ll a, b;
  a = 0;
  b = 0;
  if ((-x + 2 * y) % 3 == 0 && (2 * x - y) % 3 == 0) {
    a = (-x + 2 * y) / 3;
    b = (2 * x - y) / 3;
  } else {
    cout << 0 << endl;
    return 0;
  }

  cout << Combination(a + b, min(a, b)) << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdbool>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

typedef long long ll;

#define rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const long long INF = 1000000000000000;
const ll inf = -1e18;
typedef pair<ll, ll> P;
ll ma = 1000000000 + 7;
ll mx = 1000003;
ll h, w, m;
string s;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
ll gcd(ll x, ll y) {
  if (x % y == 0)
    return y;
  return gcd(y, x % y);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
void comb(vector<vector<ll>> &v) {
  for (ll i = 0; i < v.size(); i++) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (ll k = 1; k < v.size(); k++) {
    for (int j = 1; j < k; j++) {
      v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]) % ma;
    }
  }
}

ll GetDigit(ll num) { return log10(num) + 1; }
ll Combination(int n, int r) {
  if (n - r < r)
    r = n - r;
  if (r == 0)
    return 1;
  if (r == 1)
    return n;

  vector<ll> numerator(r);
  vector<ll> denominator(r);

  for (int k = 0; k < r; k++) {
    numerator[k] = n - r + k + 1;
    denominator[k] = k + 1;
  }

  for (int p = 2; p <= r; p++) {
    int pivot = denominator[p - 1];
    if (pivot > 1) {
      int offset = (n - r) % p;
      for (int k = p - 1; k < r; k += p) {
        numerator[k - offset] /= pivot;
        denominator[k] /= pivot;
      }
    }
  }

  ll result = 1;
  for (int k = 0; k < r; k++) {
    if (numerator[k] > 1)
      result *= numerator[k];
    result %= ma;
  }

  return result;
}
int main() {
  ll x, y;
  cin >> x >> y;
  ll a, b;
  a = 0;
  b = 0;
  if ((-x + 2 * y) % 3 == 0 && (2 * x - y) % 3 == 0 && (-x + 2 * y) >= 0 &&
      (2 * x - y) >= 0) {
    a = (-x + 2 * y) / 3;
    b = (2 * x - y) / 3;
  } else {
    cout << 0 << endl;
    return 0;
  }

  cout << Combination(a + b, min(a, b)) << endl;
}