Problem p02782 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl

const int maxn = 2e6 + 5;
const ll mod = 1e9 + 7;

ll fac[maxn], ifac[maxn];

ll modpow(ll, ll);
ll inv(ll);
ll nck(ll, ll);
void build();
void testlib();

int r1, r2, c1, c2;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  build();
  testlib();

  cin >> r1 >> c1 >> r2 >> c2;

  ll ans = 0;

  for (int i = r1; i <= r2; i++) {
    for (int j = c1; j <= c2; j++) {
      ans += nck(i + j, i);
      ans %= mod;
      ans += mod;
      ans %= mod;
    }
  }

  cout << ans << endl;

  return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////

ll modpow(ll x, ll y) {
  x %= mod;
  ll res = 1;
  while (y > 0) {
    if (y % 2 == 1) {
      res *= x;
      res %= mod;
    }
    x = x * x;
    x %= mod;
    y = y / 2;
  }
  return res;
}

ll inv(ll x) {
  assert(x != 0);
  return modpow(x, mod - 2);
}

void build() {
  fac[0] = ifac[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fac[i] = fac[i - 1] * i;
    fac[i] %= mod;
    ifac[i] = inv(fac[i]);
    ifac[i] %= mod;
  }
}

ll nck(ll n, ll k) {
  assert(n >= 0 && k >= 0);
  if (k > n)
    return 0;
  return (((fac[n] * ifac[k]) % mod) * ifac[n - k]) % mod;
}

void testlib() {
  assert(modpow(1, 1) == 1);
  assert(modpow(4, 2) == 16);
  assert(modpow(2, 5) == 32);
  assert(fac[3] == 6);
  assert(nck(4, 2) == 6);
  assert(nck(10, 2) == 45);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl

const int maxn = 2e6 + 5;
const ll mod = 1e9 + 7;

ll fac[maxn], ifac[maxn];

ll modpow(ll, ll);
ll inv(ll);
ll nck(ll, ll);
void build();
void testlib();

int r1, r2, c1, c2;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  build();
  testlib();

  cin >> r1 >> c1 >> r2 >> c2;

  ll ans = 0;

  for (int i = r1; i <= r2; i++) {
    ans += nck(i + c2 + 1, i + 1);
    ans %= mod;
    ans += mod;
    ans %= mod;
    ans -= nck(i + c1, i + 1);
    ans %= mod;
    ans += mod;
    ans %= mod;
  }

  cout << ans << endl;

  return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////

ll modpow(ll x, ll y) {
  x %= mod;
  ll res = 1;
  while (y > 0) {
    if (y % 2 == 1) {
      res *= x;
      res %= mod;
    }
    x = x * x;
    x %= mod;
    y = y / 2;
  }
  return res;
}

ll inv(ll x) {
  assert(x != 0);
  return modpow(x, mod - 2);
}

void build() {
  fac[0] = ifac[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fac[i] = fac[i - 1] * i;
    fac[i] %= mod;
    ifac[i] = inv(fac[i]);
    ifac[i] %= mod;
  }
}

ll nck(ll n, ll k) {
  assert(n >= 0 && k >= 0);
  if (k > n)
    return 0;
  return (((fac[n] * ifac[k]) % mod) * ifac[n - k]) % mod;
}

void testlib() {
  assert(modpow(1, 1) == 1);
  assert(modpow(4, 2) == 16);
  assert(modpow(2, 5) == 32);
  assert(fac[3] == 6);
  assert(nck(4, 2) == 6);
  assert(nck(10, 2) == 45);
}