Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T> T gcd(T a, T b) {
  return ((a % b == 0) ? b : gcd(b, a % b));
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;
template <long long Mod = MOD> class Modint {
public:
  long long v;

  Modint(const Modint &x) { v = x.v; }
  explicit Modint(int x) {
    if (x < 0 or Mod <= x)
      x %= Mod;
    if (x < 0)
      x += Mod;
    v = x;
  }
  explicit Modint(long long x) {
    if (x < 0 or Mod <= x)
      x %= Mod;
    if (x < 0)
      x += Mod;
    v = x;
  }
  explicit Modint(unsigned x) {
    if (Mod <= x)
      x %= Mod;
    v = x;
  }
  explicit Modint(unsigned long long x) {
    if (Mod <= x)
      x %= Mod;
    v = x;
  }
  template <typename T> explicit Modint(T x) {
    if (x < 0 or Mod <= x)
      x %= Mod;
    v = x;
  }
  Modint() : v(0) {}
  long long get() const { return v; }
  Modint mpow(Modint &n) const { return mpow(n.v); }
  Modint mpow(long long n) const {
    long long i = 1, p = v;
    Modint ret(1);
    while (i <= n) {
      if (i & n)
        ret *= p;
      i = (i << 1);
      p = (p * p) % Mod;
    }
    return ret;
  }
  Modint operator-() const { return (v ? Modint(Mod - v) : Modint(0)); }
  explicit operator int() const { return v; }
  explicit operator long long() const { return v; }
  Modint &operator+=(const Modint &a) {
    v = (v + a.v);
    if (v >= Mod)
      v -= Mod;
    return *this;
  }
  Modint &operator-=(const Modint &a) {
    v = (v - a.v);
    if (v < 0)
      v += Mod;
    return *this;
  }
  Modint &operator*=(const Modint &a) {
    v = (v * a.v) % Mod;
    return *this;
  }
  Modint &operator/=(const Modint &a) {
    v = (v * a.mpow(Mod - 2).v) % Mod;
    return *this;
  }
  template <class T> Modint &operator+=(const T &a) {
    v = (v + Modint(a).v);
    if (v >= Mod)
      v -= Mod;
    return *this;
  }
  template <class T> Modint &operator-=(const T &a) {
    v = (v - Modint(a).v);
    if (v < 0)
      v += Mod;
    return *this;
  }
  template <class T> Modint &operator*=(const T &a) {
    v = (v * Modint(a).v) % Mod;
    return *this;
  }
  template <class T> Modint &operator/=(const T &a) {
    v = (v * Modint(a).mpow(Mod - 2).v) % Mod;
    return *this;
  }
  friend Modint operator+(const Modint &a, const Modint &b) {
    return Modint(a) += b;
  }
  friend Modint operator-(const Modint &a, const Modint &b) {
    return Modint(a) -= b;
  }
  friend Modint operator*(const Modint &a, const Modint &b) {
    return Modint(a) *= b;
  }
  friend Modint operator/(const Modint &a, const Modint &b) {
    return Modint(a) /= b;
  }
  template <class T, class U> friend Modint operator+(const T &a, const U &b) {
    return Modint(a) += Modint(b);
  }
  template <class T, class U> friend Modint operator-(const T &a, const U &b) {
    return Modint(a) -= Modint(b);
  }
  template <class T, class U> friend Modint operator*(const T &a, const U &b) {
    return Modint(a) *= Modint(b);
  }
  template <class T, class U> friend Modint operator/(const T &a, const U &b) {
    return Modint(a) /= Modint(b);
  }
};

template <long long M> ostream &operator<<(ostream &os, const Modint<M> m) {
  return os << m.v;
}

template <long long Mod = MOD> class Factorial {
private:
  int n;

public:
  vector<long long> fac, inv;

public:
  Factorial(long long n) : n(n), fac(n + 1), inv(n + 1) {
    fac[0] = 1;
    for (int i = 0; i < n; ++i)
      fac[i + 1] = fac[i] * (i + 1) % Mod;
    inv[n] = modpow(fac[n], Mod - 2);
    for (int i = n - 1; i >= 0; --i)
      inv[i] = inv[i + 1] * (i + 1) % Mod;
  };
  long long modpow(long long a, long long n, long long mod = Mod) {
    long long i = 1, ret = 1, p = a;
    while (i <= n) {
      if (i & n) {
        ret = (ret * p) % mod;
      }
      i = (i << 1);
      p = (p * p) % mod;
    }
    return ret;
  }
  long long C(int a, int b) {
    return (fac[a] * inv[b] % Mod) * inv[a - b] % Mod;
  }
  long long P(int a, int b) { return (fac[a]) * inv[a - b] % Mod; }
  long long H(int a, int b) { // The number of multisets of length b on a
                              // symbols
    return C(a + b - 1, b);
  }
};

class Solver2 {
public:
  int r1, r2, c1, c2;
  Factorial<> fac;
  Solver2(LL r1, LL r2, LL c1, LL c2)
      : r1(r1), r2(r2), c1(c1), c2(c2), fac(222222){};

  void solve() {
    Modint<> ans(0);
    ans += calc(r2, c2);
    ans -= calc(r2, c1 - 1);
    ans -= calc(r1 - 1, c2);
    ans += calc(r1 - 1, c1 - 1);
    cout << ans << endl;
  }

  Modint<> calc(int r, int c) {
    auto ret = sumRows(r + c);
    DBG(ret)
    for (int i = 0; i < c; ++i) {
      // from (r,0) ~ (r,c-1)
      ret -= Modint<>(fac.C(r + i, r)) * sumRows(c - 1 - i);
    }
    for (int i = 0; i < r; ++i) {
      // from (0,c) ~ (r-1,c)
      ret -= Modint<>(fac.C(c + i, c)) * sumRows(r - 1 - i);
    }
    DBG(r, c, ret)
    return ret;
  }

  Modint<> sumRows(int x) { return Modint<>(2).mpow(x + 1) - 1; }
};
} // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(12);
  long long r1, r2, c1, c2;
  std::cin >> r1 >> c1 >> r2 >> c2;

  Problem::Solver2 sol(r1, r2, c1, c2);
  sol.solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T> T gcd(T a, T b) {
  return ((a % b == 0) ? b : gcd(b, a % b));
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;
template <long long Mod = MOD> class Modint {
public:
  long long v;

  Modint(const Modint &x) { v = x.v; }
  explicit Modint(int x) {
    if (x < 0 or Mod <= x)
      x %= Mod;
    if (x < 0)
      x += Mod;
    v = x;
  }
  explicit Modint(long long x) {
    if (x < 0 or Mod <= x)
      x %= Mod;
    if (x < 0)
      x += Mod;
    v = x;
  }
  explicit Modint(unsigned x) {
    if (Mod <= x)
      x %= Mod;
    v = x;
  }
  explicit Modint(unsigned long long x) {
    if (Mod <= x)
      x %= Mod;
    v = x;
  }
  template <typename T> explicit Modint(T x) {
    if (x < 0 or Mod <= x)
      x %= Mod;
    v = x;
  }
  Modint() : v(0) {}
  long long get() const { return v; }
  Modint mpow(Modint &n) const { return mpow(n.v); }
  Modint mpow(long long n) const {
    long long i = 1, p = v;
    Modint ret(1);
    while (i <= n) {
      if (i & n)
        ret *= p;
      i = (i << 1);
      p = (p * p) % Mod;
    }
    return ret;
  }
  Modint operator-() const { return (v ? Modint(Mod - v) : Modint(0)); }
  explicit operator int() const { return v; }
  explicit operator long long() const { return v; }
  Modint &operator+=(const Modint &a) {
    v = (v + a.v);
    if (v >= Mod)
      v -= Mod;
    return *this;
  }
  Modint &operator-=(const Modint &a) {
    v = (v - a.v);
    if (v < 0)
      v += Mod;
    return *this;
  }
  Modint &operator*=(const Modint &a) {
    v = (v * a.v) % Mod;
    return *this;
  }
  Modint &operator/=(const Modint &a) {
    v = (v * a.mpow(Mod - 2).v) % Mod;
    return *this;
  }
  template <class T> Modint &operator+=(const T &a) {
    v = (v + Modint(a).v);
    if (v >= Mod)
      v -= Mod;
    return *this;
  }
  template <class T> Modint &operator-=(const T &a) {
    v = (v - Modint(a).v);
    if (v < 0)
      v += Mod;
    return *this;
  }
  template <class T> Modint &operator*=(const T &a) {
    v = (v * Modint(a).v) % Mod;
    return *this;
  }
  template <class T> Modint &operator/=(const T &a) {
    v = (v * Modint(a).mpow(Mod - 2).v) % Mod;
    return *this;
  }
  friend Modint operator+(const Modint &a, const Modint &b) {
    return Modint(a) += b;
  }
  friend Modint operator-(const Modint &a, const Modint &b) {
    return Modint(a) -= b;
  }
  friend Modint operator*(const Modint &a, const Modint &b) {
    return Modint(a) *= b;
  }
  friend Modint operator/(const Modint &a, const Modint &b) {
    return Modint(a) /= b;
  }
  template <class T, class U> friend Modint operator+(const T &a, const U &b) {
    return Modint(a) += Modint(b);
  }
  template <class T, class U> friend Modint operator-(const T &a, const U &b) {
    return Modint(a) -= Modint(b);
  }
  template <class T, class U> friend Modint operator*(const T &a, const U &b) {
    return Modint(a) *= Modint(b);
  }
  template <class T, class U> friend Modint operator/(const T &a, const U &b) {
    return Modint(a) /= Modint(b);
  }
};

template <long long M> ostream &operator<<(ostream &os, const Modint<M> m) {
  return os << m.v;
}

template <long long Mod = MOD> class Factorial {
private:
  int n;

public:
  vector<long long> fac, inv;

public:
  Factorial(long long n) : n(n), fac(n + 1), inv(n + 1) {
    fac[0] = 1;
    for (int i = 0; i < n; ++i)
      fac[i + 1] = fac[i] * (i + 1) % Mod;
    inv[n] = modpow(fac[n], Mod - 2);
    for (int i = n - 1; i >= 0; --i)
      inv[i] = inv[i + 1] * (i + 1) % Mod;
  };
  long long modpow(long long a, long long n, long long mod = Mod) {
    long long i = 1, ret = 1, p = a;
    while (i <= n) {
      if (i & n) {
        ret = (ret * p) % mod;
      }
      i = (i << 1);
      p = (p * p) % mod;
    }
    return ret;
  }
  long long C(int a, int b) {
    return (fac[a] * inv[b] % Mod) * inv[a - b] % Mod;
  }
  long long P(int a, int b) { return (fac[a]) * inv[a - b] % Mod; }
  long long H(int a, int b) { // The number of multisets of length b on a
                              // symbols
    return C(a + b - 1, b);
  }
};

class Solver2 {
public:
  int r1, r2, c1, c2;
  Factorial<> fac;
  Solver2(LL r1, LL r2, LL c1, LL c2)
      : r1(r1), r2(r2), c1(c1), c2(c2), fac(2222222){};

  void solve() {
    Modint<> ans(0);
    ans += calc(r2, c2);
    ans -= calc(r2, c1 - 1);
    ans -= calc(r1 - 1, c2);
    ans += calc(r1 - 1, c1 - 1);
    cout << ans << endl;
  }

  Modint<> calc(int r, int c) {
    auto ret = sumRows(r + c);
    DBG(ret)
    for (int i = 0; i < c; ++i) {
      // from (r,0) ~ (r,c-1)
      ret -= Modint<>(fac.C(r + i, r)) * sumRows(c - 1 - i);
    }
    for (int i = 0; i < r; ++i) {
      // from (0,c) ~ (r-1,c)
      ret -= Modint<>(fac.C(c + i, c)) * sumRows(r - 1 - i);
    }
    DBG(r, c, ret)
    return ret;
  }

  Modint<> sumRows(int x) { return Modint<>(2).mpow(x + 1) - 1; }
};
} // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(12);
  long long r1, r2, c1, c2;
  std::cin >> r1 >> c1 >> r2 >> c2;

  Problem::Solver2 sol(r1, r2, c1, c2);
  sol.solve();
  return 0;
}