Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int const nax = 3e5 + 10;
constexpr int mod = 1e9 + 7;
int r1, r2, c1, c2;
void plusle(int &a, int b) {
  if ((a += b) >= mod)
    a -= mod;
}
void minun(int &a, int b) {
  if ((a -= b) < 0)
    a += mod;
}
int add(int a, int b) { return ((a += b) >= mod) ? a - mod : a; }
int sub(int a, int b) { return ((a -= b) < 0) ? a + mod : a; }
int mul(int a, int b) { return 1LL * a * b % mod; }
int power(int a, int b) {
  int res = 1;
  for (; b > 0; b >>= 1, a = mul(a, a))
    if (b & 1)
      res = mul(res, a);
  return res;
}
const int Mx = 1e6 + 10;
int fac[Mx], invf[Mx];
void prefact() {
  fac[0] = 1;
  for (int i = 1; i < Mx; i++) {
    fac[i] = mul(fac[i - 1], i);
  }
  invf[Mx - 1] = power(fac[Mx - 1], mod - 2);
  for (int i = Mx - 2; i >= 0; i--) {
    invf[i] = mul(invf[i + 1], i + 1);
  }
}

int ncr(int N, int R) {
  if (R > N)
    return 0;
  if (R == 0 || R == N)
    return 1;
  return mul(fac[N], mul(invf[N - R], invf[R]));
}

/**
    find the number of ways to move from (0 , 0)
    to any of square (r , c ) -> r1 <= r <= r2 , c1 <= c <= c2
    Let's represent the number of ways again
    To move from (0 , 0 ) to any (r , c)
    RRRRR -> r chars and UUUUU -> c chars
    and the number of shuffle equal to (r + c)! / (r!) / (c!)
    or (r + c) choose r
    Then it's trivially ask you
    compute summation (r + c) choose (r) from r = r1 to r = r2 and c from c1 to
   c2 This can be done easily in O(rc) Of course, we can do it better. ;) Let's
   iterate over r from r1 to r2 summation (r + c) choose r from c1 to c2 by
   Pascal's theorem (r , 0 ) + (r + 1 , 1 ) + (r + 2 , 2) ... (r + 1 , 0) + (r +
   1, 1) -> (r + 2, 1) + (r + 2 ,  2) -> (r + 3, 2 ) + (r + 3, 3)
    .... (r + c + 1 , c)
*/
int main() {
  prefact();

  scanf("%d %d %d %d", &r1, &c1, &r2, &c2);
  auto sum = [&](int r, int c) -> int {
    /// (r , 0) + (r + 1 , 1) , .... (r + c, c)
    return ncr(r + c + 1, c);
  };
  int ans = 0;
  for (int r = r1; r <= r2; ++r) {
    plusle(ans, sub(sum(r, c2), sum(r, c1 - 1)));
  }
  printf("%d\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int const nax = 3e5 + 10;
constexpr int mod = 1e9 + 7;
int r1, r2, c1, c2;
void plusle(int &a, int b) {
  if ((a += b) >= mod)
    a -= mod;
}
void minun(int &a, int b) {
  if ((a -= b) < 0)
    a += mod;
}
int add(int a, int b) { return ((a += b) >= mod) ? a - mod : a; }
int sub(int a, int b) { return ((a -= b) < 0) ? a + mod : a; }
int mul(int a, int b) { return 1LL * a * b % mod; }
int power(int a, int b) {
  int res = 1;
  for (; b > 0; b >>= 1, a = mul(a, a))
    if (b & 1)
      res = mul(res, a);
  return res;
}
const int Mx = 2e6 + 10;
int fac[Mx], invf[Mx];
void prefact() {
  fac[0] = 1;
  for (int i = 1; i < Mx; i++) {
    fac[i] = mul(fac[i - 1], i);
  }
  invf[Mx - 1] = power(fac[Mx - 1], mod - 2);
  for (int i = Mx - 2; i >= 0; i--) {
    invf[i] = mul(invf[i + 1], i + 1);
  }
}

int ncr(int N, int R) {
  if (R > N)
    return 0;
  if (R == 0 || R == N)
    return 1;
  return mul(fac[N], mul(invf[N - R], invf[R]));
}

/**
    find the number of ways to move from (0 , 0)
    to any of square (r , c ) -> r1 <= r <= r2 , c1 <= c <= c2
    Let's represent the number of ways again
    To move from (0 , 0 ) to any (r , c)
    RRRRR -> r chars and UUUUU -> c chars
    and the number of shuffle equal to (r + c)! / (r!) / (c!)
    or (r + c) choose r
    Then it's trivially ask you
    compute summation (r + c) choose (r) from r = r1 to r = r2 and c from c1 to
   c2 This can be done easily in O(rc) Of course, we can do it better. ;) Let's
   iterate over r from r1 to r2 summation (r + c) choose r from c1 to c2 by
   Pascal's theorem (r , 0 ) + (r + 1 , 1 ) + (r + 2 , 2) ... (r + 1 , 0) + (r +
   1, 1) -> (r + 2, 1) + (r + 2 ,  2) -> (r + 3, 2 ) + (r + 3, 3)
    .... (r + c + 1 , c)
*/
int main() {
  prefact();

  scanf("%d %d %d %d", &r1, &c1, &r2, &c2);
  auto sum = [&](int r, int c) -> int {
    /// (r , 0) + (r + 1 , 1) , .... (r + c, c)
    return ncr(r + c + 1, c);
  };
  int ans = 0;
  for (int r = r1; r <= r2; ++r) {
    plusle(ans, sub(sum(r, c2), sum(r, c1 - 1)));
  }
  printf("%d\n", ans);
}