Problem p02862 (C++)

Buggy submission (status: Runtime Error):

// Best practice

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less

#define int long long
#define pb emplace_back
#define pf emplace_front
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define x first
#define y second
#define endl '\n'
#define tezz                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define MOD 1000000007
using namespace std;
using namespace __gnu_pbds;

typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

#define fbo find_by_order
#define ook order_of_key

int power(int x, int y, int p) {
  int res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
             // equal to p

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

// Returns n^(-1) mod p
int modInverse(int n, int p) { return power(n, p - 2, p); }
int ncr(int n, int r, int p) {
  // Base case
  if (r == 0)
    return 1;

  // Fill factorial array so that we
  // can find all factorial of r, n
  // and n-r
  int fac[n + 1];
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % p;

  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}

signed main() {
  tezz

      int x,
      y;
  cin >> x >> y;
  int t = (x + y);
  if (t % 3 != 0) {
    cout << 0;
    return 0;
  }
  t /= 3;
  int tt = x - t;
  //   cout << tt << " " << t << endl;
  if (tt < 0 || t < 0) {
    cout << 0;
    return 0;
  }
  if (tt == 0 || tt == t) {
    cout << 1;
    return 0;
  }
  if (tt == 1 || tt == t - 1) {
    cout << t;
    return 0;
  }

  cout << ncr(t, tt, MOD);

  return 0;
}


Fixed submission:

// Best practice

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less

#define int long long
#define pb emplace_back
#define pf emplace_front
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define x first
#define y second
#define endl '\n'
#define tezz                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define MOD 1000000007
using namespace std;
using namespace __gnu_pbds;

typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

#define fbo find_by_order
#define ook order_of_key

int power(int x, int y, int p) {
  int res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
             // equal to p

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

// Returns n^(-1) mod p
int modInverse(int n, int p) { return power(n, p - 2, p); }
int ncr(int n, int r, int p) {
  // Base case
  if (r == 0)
    return 1;

  // Fill factorial array so that we
  // can find all factorial of r, n
  // and n-r
  int fac[n + 1];
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % p;

  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}

signed main() {
  tezz

      int x,
      y;
  cin >> x >> y;
  int t = (x + y);
  if (t % 3 != 0) {
    cout << 0;
    return 0;
  }
  t /= 3;
  int tt = x - t;
  //   cout << tt << " " << t << endl;
  if (tt < 0 || t < 0 || tt > t) {
    cout << 0;
    return 0;
  }
  if (tt == 0 || tt == t) {
    cout << 1;
    return 0;
  }
  if (tt == 1 || tt == t - 1) {
    cout << t;
    return 0;
  }

  cout << ncr(t, tt, MOD);

  return 0;
}