Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define rev(i, s, e) for (i64(i) = (s); (i)-- > (e);)
#define all(x) x.begin(), x.end()

template <class T>
static inline std::vector<T> ndvec(size_t &&n, T val) noexcept {
  return std::vector<T>(n, std::forward<T>(val));
}

template <class... Tail>
static inline auto ndvec(size_t &&n, Tail &&...tail) noexcept {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(
      n, ndvec(std::forward<Tail>(tail)...));
}

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

template <i64 M> struct modint {
  i64 a;
  constexpr modint(const i64 x = 0) noexcept : a((x % M + M) % M) {}
  constexpr i64 value() const noexcept { return a; }
  constexpr modint pow(i64 r) const noexcept {
    modint ans(1);
    modint aa = *this;
    while (r) {
      if (r & 1) {
        ans *= aa;
      }
      aa *= aa;
      r >>= 1;
    }
    return ans;
  }
  constexpr modint &operator+=(const modint r) noexcept {
    a += r.a;
    if (a >= M)
      a -= M;
    return *this;
  }
  constexpr modint &operator=(const i64 r) {
    a = (r % M + M) % M;
    return *this;
  }
  constexpr modint &operator-=(const modint r) noexcept {
    a -= r.a;
    if (a < 0)
      a += M;
    return *this;
  }
  constexpr modint &operator*=(const modint r) noexcept {
    a = a * r.a % M;
    return *this;
  }
  constexpr modint &operator/=(modint r) noexcept {
    i64 ex = M - 2;
    while (ex) {
      if (ex & 1) {
        *this *= r;
      }
      r *= r;
      ex >>= 1;
    }
    return *this;
  }

  constexpr modint operator+(const modint r) const {
    return modint(*this) += r;
  }
  constexpr modint operator-(const modint r) const {
    return modint(*this) -= r;
  }
  constexpr modint operator*(const modint r) const {
    return modint(*this) *= r;
  }
  constexpr modint operator/(const modint r) const {
    return modint(*this) /= r;
  }
};

template <const i64 M>
std::ostream &operator<<(std::ostream &os, const modint<M> &m) {
  os << m.value();
  return os;
}

using fp = modint<(i64)(1e9 + 7)>;

#include <vector>
using i64 = long long;

template <class T>
void build_factorial(std::vector<T> &fact, std::vector<T> &finv,
                     std::vector<T> &inv, std::vector<T> &bin) {
  std::size_t N = fact.size();
  fact[0] = T(1);
  bin[0] = T(1);
  for (int i = 1; i < N; i++) {
    fact[i] = fact[i - 1] * T(i);
    bin[i] = bin[i - 1] * T(2);
  }
  finv[N - 1] = T(1) / fact[N - 1];
  for (int i = N - 1; i-- > 0;) {
    finv[i] = finv[i + 1] * T(i + 1);
  }
  for (int i = 0; i < N; i++) {
    inv[i] = fact[i - 1] * finv[i];
  }
}

i64 R1, C1, R2, C2;

i64 NMAX = 1e6 + 1;

int main() {
  cin >> R1 >> C1 >> R2 >> C2;
  vector<fp> fact(NMAX);
  vector<fp> finv(NMAX);
  vector<fp> inv(NMAX);
  vector<fp> bin(NMAX);
  build_factorial(fact, finv, inv, bin);
  auto f = [&](i64 i, i64 j) {
    if (i == 0 && j == 0)
      return fp(1);
    fp ans = fp(0);
    fp nowi = fp(0);
    fp nowj = fp(0);
    for (i64 n = 0; n <= i + j; n++) {
      ans += bin[n];
      if (n >= i + 1) {
        nowi = nowi * fp(2) + fact[n - 1] * finv[n - i - 1] * finv[i];
        if (n == i + 1)
          nowi = fp(1);
        ans -= nowi;
      }
      if (n >= j + 1) {
        nowj = nowj * fp(2) + fact[n - 1] * finv[n - j - 1] * finv[j];
        if (n == j + 1)
          nowj = fp(1);
        ans -= nowj;
      }
    }
    return ans;
  };
  R1--;
  C1--;
  cout << (f(R2, C2) + f(R1, C1) - f(R1, C2) - f(R2, C1)).value() << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define rev(i, s, e) for (i64(i) = (s); (i)-- > (e);)
#define all(x) x.begin(), x.end()

template <class T>
static inline std::vector<T> ndvec(size_t &&n, T val) noexcept {
  return std::vector<T>(n, std::forward<T>(val));
}

template <class... Tail>
static inline auto ndvec(size_t &&n, Tail &&...tail) noexcept {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(
      n, ndvec(std::forward<Tail>(tail)...));
}

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

template <i64 M> struct modint {
  i64 a;
  constexpr modint(const i64 x = 0) noexcept : a((x % M + M) % M) {}
  constexpr i64 value() const noexcept { return a; }
  constexpr modint pow(i64 r) const noexcept {
    modint ans(1);
    modint aa = *this;
    while (r) {
      if (r & 1) {
        ans *= aa;
      }
      aa *= aa;
      r >>= 1;
    }
    return ans;
  }
  constexpr modint &operator+=(const modint r) noexcept {
    a += r.a;
    if (a >= M)
      a -= M;
    return *this;
  }
  constexpr modint &operator=(const i64 r) {
    a = (r % M + M) % M;
    return *this;
  }
  constexpr modint &operator-=(const modint r) noexcept {
    a -= r.a;
    if (a < 0)
      a += M;
    return *this;
  }
  constexpr modint &operator*=(const modint r) noexcept {
    a = a * r.a % M;
    return *this;
  }
  constexpr modint &operator/=(modint r) noexcept {
    i64 ex = M - 2;
    while (ex) {
      if (ex & 1) {
        *this *= r;
      }
      r *= r;
      ex >>= 1;
    }
    return *this;
  }

  constexpr modint operator+(const modint r) const {
    return modint(*this) += r;
  }
  constexpr modint operator-(const modint r) const {
    return modint(*this) -= r;
  }
  constexpr modint operator*(const modint r) const {
    return modint(*this) *= r;
  }
  constexpr modint operator/(const modint r) const {
    return modint(*this) /= r;
  }
};

template <const i64 M>
std::ostream &operator<<(std::ostream &os, const modint<M> &m) {
  os << m.value();
  return os;
}

using fp = modint<(i64)(1e9 + 7)>;

#include <vector>
using i64 = long long;

template <class T>
void build_factorial(std::vector<T> &fact, std::vector<T> &finv,
                     std::vector<T> &inv, std::vector<T> &bin) {
  std::size_t N = fact.size();
  fact[0] = T(1);
  bin[0] = T(1);
  for (int i = 1; i < N; i++) {
    fact[i] = fact[i - 1] * T(i);
    bin[i] = bin[i - 1] * T(2);
  }
  finv[N - 1] = T(1) / fact[N - 1];
  for (int i = N - 1; i-- > 0;) {
    finv[i] = finv[i + 1] * T(i + 1);
  }
  for (int i = 0; i < N; i++) {
    inv[i] = fact[i - 1] * finv[i];
  }
}

i64 R1, C1, R2, C2;

i64 NMAX = 2e6 + 1000;

int main() {
  cin >> R1 >> C1 >> R2 >> C2;
  vector<fp> fact(NMAX);
  vector<fp> finv(NMAX);
  vector<fp> inv(NMAX);
  vector<fp> bin(NMAX);
  build_factorial(fact, finv, inv, bin);
  auto f = [&](i64 i, i64 j) {
    if (i == 0 && j == 0)
      return fp(1);
    fp ans = fp(0);
    fp nowi = fp(0);
    fp nowj = fp(0);
    for (i64 n = 0; n <= i + j; n++) {
      ans += bin[n];
      if (n >= i + 1) {
        nowi = nowi * fp(2) + fact[n - 1] * finv[n - i - 1] * finv[i];
        if (n == i + 1)
          nowi = fp(1);
        ans -= nowi;
      }
      if (n >= j + 1) {
        nowj = nowj * fp(2) + fact[n - 1] * finv[n - j - 1] * finv[j];
        if (n == j + 1)
          nowj = fp(1);
        ans -= nowj;
      }
    }
    return ans;
  };
  R1--;
  C1--;
  cout << (f(R2, C2) + f(R1, C1) - f(R1, C2) - f(R2, C1)).value() << endl;
}