Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define ll long long
#define dd double
#define MOD 1000000007
#define nl cout << endl
#define rt ll Q[100005] = {0}
#define mp make_pair
#define test()                                                                 \
  ull t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define s(ar, n) sort(ar, ar + n)
#define rs(ar, n) sort(ar, ar + n, greater<ll>())
#define oa(a, n)                                                               \
  for (ll i = 0; i < n; i++)                                                   \
    cout << a[i] << " ";                                                       \
  nl
#define cn(a, n)                                                               \
  for (ll i = 0; i < n; i++)                                                   \
    cin >> a[i];
#define ov(a, n)                                                               \
  for (ll i = 0; i < n; i++)                                                   \
    cout << a[i] << " ";                                                       \
  nl
#define maxa(ar, N) *max_element(ar, ar + N)
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll modInverse(ll n, ll p) { return power(n, p - 2, p); }
ll nCrModPFermat(ll n, ll r, ll p) {
  if (r < 0)
    return 0;
  if (r == 0)
    return 1ll;
  ll 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;
}
int main() {
  fastio();
  ll N, M;
  cin >> N >> M;
  ll j = N + M;
  if (j % 3 == 0) {
    j /= 3;
    // cout<<j<<" "<<M-j<<" "<<MOD<<endl;
    ll kj = nCrModPFermat(j, M - j, MOD);
    cout << kj;
  } else
    cout << 0;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define ll long long
#define dd double
#define MOD 1000000007
#define nl cout << endl
#define rt ll Q[100005] = {0}
#define mp make_pair
#define test()                                                                 \
  ull t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define s(ar, n) sort(ar, ar + n)
#define rs(ar, n) sort(ar, ar + n, greater<ll>())
#define oa(a, n)                                                               \
  for (ll i = 0; i < n; i++)                                                   \
    cout << a[i] << " ";                                                       \
  nl
#define cn(a, n)                                                               \
  for (ll i = 0; i < n; i++)                                                   \
    cin >> a[i];
#define ov(a, n)                                                               \
  for (ll i = 0; i < n; i++)                                                   \
    cout << a[i] << " ";                                                       \
  nl
#define maxa(ar, N) *max_element(ar, ar + N)
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll modInverse(ll n, ll p) { return power(n, p - 2, p); }
ll nCrModPFermat(ll n, ll r, ll p) {
  if (r < 0 || r > n)
    return 0;
  if (r == 0)
    return 1ll;
  ll 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;
}
int main() {
  fastio();
  ll N, M;
  cin >> N >> M;
  ll j = N + M;
  if (j % 3 == 0) {
    j /= 3;
    // cout<<j<<" "<<M-j<<" "<<MOD<<endl;
    ll kj = nCrModPFermat(j, M - j, MOD);
    cout << kj;
  } else
    cout << 0;
  return 0;
}