Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using lint = int64_t;
using uint = uint32_t;
using ulint = uint64_t;

template <class T> using vector2d = vector<vector<T>>;

template <class T> bool UpdateMax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

template <class T> bool UpdateMin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

template <class T> void OutVec(const vector<T> &vec) {
  for (int i = 0; i < vec.size() - 1; ++i) {
    cout << vec[i] << " ";
  }
  cout << vec.back() << endl;
}

template <class T> void OutVec2d(const vector2d<T> &vec) {
  for (auto v : vec) {
    OutVec(v);
  }
}

class ModCombination {
  std::vector<int64_t> fac, finv, inv;
  int64_t max, mod;

public:
  ModCombination(int64_t max_, int64_t mod_)
      : max(max_), mod(mod_), fac(max_ + 1), finv(max_ + 1), inv(max_ + 1) {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < max; i++) {
      fac[i] = fac[i - 1] * i % mod;
      inv[i] = mod - inv[mod % i] * (mod / i) % mod;
      finv[i] = finv[i - 1] * inv[i] % mod;
    }
  }
  int64_t GetCombination(int n, int k) {
    if (n < k)
      return 0;
    if (n < 0 || k < 0)
      return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
  }
};

int64_t EuclidMod(int64_t v, int64_t m) {
  if (0 <= v && v < m) {
    return v;
  } else if (-m <= v && v < 0) {
    return v + m;
  } else {
    int64_t result = v % m;
    if (result < 0) {
      result += m;
    }
    return result;
  }
}

int64_t ModInv(int64_t a, int64_t m) {
  int64_t b = m, u = 1, v = 0;
  while (b) {
    int64_t t = a / b;
    a -= t * b;
    std::swap(a, b);
    u -= t * v;
    std::swap(u, v);
  }
  EuclidMod(u, m);
  return u;
}

// return base^exponent (MOD. mod).
int64_t RepeatedPowMod(int64_t base, int64_t exponent, int64_t mod) {
  if (exponent == 0)
    return 1;
  else if (exponent % 2 == 0) {
    int64_t root = RepeatedPowMod(base, exponent / 2, mod);
    return (root * root) % mod;
  } else {
    return (base * RepeatedPowMod(base, exponent - 1, mod)) % mod;
  }
}

template <int64_t mod> class ModInt {
  int64_t value_;

public:
  explicit ModInt(const int64_t x = 0) : value_(x % mod){};

  ModInt &operator=(const ModInt &another) {
    value_ = another.value_;
    return *this;
  }

  ModInt &operator^=(const ModInt &another) {
    value_ = RepeatedPowMod(value_, another.value_, mod);
    return *this;
  }

  ModInt &operator+=(const ModInt &another) {
    value_ += another.value_;
    value_ = EuclidMod(value_, mod);
    return *this;
  }

  ModInt &operator-=(const ModInt &another) {
    value_ -= another.value_;
    value_ = EuclidMod(value_, mod);
    return *this;
  }

  ModInt &operator*=(const ModInt &another) {
    value_ *= another.value_;
    value_ = EuclidMod(value_, mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &another) {
    value_ = value_ * ModInv(another.value_, mod);
    value_ = EuclidMod(value_, mod);
    return *this;
  }

  ModInt &operator++() {
    value_ += 1;
    value_ = EuclidMod(value_, mod);
    return *this;
  }

  ModInt operator++(int) {
    ModInt tmp = *this;
    this->value_ += 1;
    this->value_ = EuclidMod(this->value_, mod);
    return tmp;
  }

  ModInt &operator--() {
    value_ -= 1;
    value_ = EuclidMod(value_, mod);
    return *this;
  }

  ModInt operator--(int) {
    ModInt tmp = *this;
    this->value_ -= 1;
    this->value_ = EuclidMod(this->value_, mod);
    return tmp;
  }

  int64_t GetValue() const { return value_; }
};

template <int64_t mod>
ModInt<mod> operator+(const ModInt<mod> &left, const ModInt<mod> &right) {
  return ModInt<mod>(left) += right;
}

template <int64_t mod>
ModInt<mod> operator-(const ModInt<mod> &left, const ModInt<mod> &right) {
  return ModInt<mod>(left) -= right;
}

template <int64_t mod>
ModInt<mod> operator/(const ModInt<mod> &left, const ModInt<mod> &right) {
  return ModInt<mod>(left) /= right;
}

template <int64_t mod>
ModInt<mod> operator*(const ModInt<mod> &left, const ModInt<mod> &right) {
  return ModInt<mod>(left) *= right;
}

template <int64_t mod>
ModInt<mod> operator^(const ModInt<mod> &left, const ModInt<mod> &right) {
  return ModInt<mod>(left) ^= right;
}

template <int64_t mod>
std::ostream &operator<<(std::ostream &stream, const ModInt<mod> &mod_int) {
  stream << mod_int.GetValue();
  return stream;
}

int main() {
  cout << std::fixed << std::setprecision(16);
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  const int64_t MOD = 1000000007;
  ModCombination cmb(1000001, MOD);
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  ModInt<MOD> result = ModInt<MOD>(cmb.GetCombination(r2 + c2 + 2, r2 + 1) - 1);
  result -= ModInt<MOD>(cmb.GetCombination(r1 + c2 + 1, r1) - 1);
  result -= ModInt<MOD>(cmb.GetCombination(r2 + c1 + 1, c1) - 1);
  result += ModInt<MOD>(cmb.GetCombination(r1 + c1, r1) - 1);
  cout << result << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using lint = int64_t;
using uint = uint32_t;
using ulint = uint64_t;

template <class T> using vector2d = vector<vector<T>>;

template <class T> bool UpdateMax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

template <class T> bool UpdateMin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

template <class T> void OutVec(const vector<T> &vec) {
  for (int i = 0; i < vec.size() - 1; ++i) {
    cout << vec[i] << " ";
  }
  cout << vec.back() << endl;
}

template <class T> void OutVec2d(const vector2d<T> &vec) {
  for (auto v : vec) {
    OutVec(v);
  }
}

class ModCombination {
  std::vector<int64_t> fac, finv, inv;
  int64_t max, mod;

public:
  ModCombination(int64_t max_, int64_t mod_)
      : max(max_), mod(mod_), fac(max_ + 1), finv(max_ + 1), inv(max_ + 1) {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < max; i++) {
      fac[i] = fac[i - 1] * i % mod;
      inv[i] = mod - inv[mod % i] * (mod / i) % mod;
      finv[i] = finv[i - 1] * inv[i] % mod;
    }
  }
  int64_t GetCombination(int n, int k) {
    if (n < k)
      return 0;
    if (n < 0 || k < 0)
      return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
  }
};

int64_t EuclidMod(int64_t v, int64_t m) {
  if (0 <= v && v < m) {
    return v;
  } else if (-m <= v && v < 0) {
    return v + m;
  } else {
    int64_t result = v % m;
    if (result < 0) {
      result += m;
    }
    return result;
  }
}

int64_t ModInv(int64_t a, int64_t m) {
  int64_t b = m, u = 1, v = 0;
  while (b) {
    int64_t t = a / b;
    a -= t * b;
    std::swap(a, b);
    u -= t * v;
    std::swap(u, v);
  }
  EuclidMod(u, m);
  return u;
}

// return base^exponent (MOD. mod).
int64_t RepeatedPowMod(int64_t base, int64_t exponent, int64_t mod) {
  if (exponent == 0)
    return 1;
  else if (exponent % 2 == 0) {
    int64_t root = RepeatedPowMod(base, exponent / 2, mod);
    return (root * root) % mod;
  } else {
    return (base * RepeatedPowMod(base, exponent - 1, mod)) % mod;
  }
}

template <int64_t mod> class ModInt {
  int64_t value_;

public:
  explicit ModInt(const int64_t x = 0) : value_(x % mod){};

  ModInt &operator=(const ModInt &another) {
    value_ = another.value_;
    return *this;
  }

  ModInt &operator^=(const ModInt &another) {
    value_ = RepeatedPowMod(value_, another.value_, mod);
    return *this;
  }

  ModInt &operator+=(const ModInt &another) {
    value_ += another.value_;
    value_ = EuclidMod(value_, mod);
    return *this;
  }

  ModInt &operator-=(const ModInt &another) {
    value_ -= another.value_;
    value_ = EuclidMod(value_, mod);
    return *this;
  }

  ModInt &operator*=(const ModInt &another) {
    value_ *= another.value_;
    value_ = EuclidMod(value_, mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &another) {
    value_ = value_ * ModInv(another.value_, mod);
    value_ = EuclidMod(value_, mod);
    return *this;
  }

  ModInt &operator++() {
    value_ += 1;
    value_ = EuclidMod(value_, mod);
    return *this;
  }

  ModInt operator++(int) {
    ModInt tmp = *this;
    this->value_ += 1;
    this->value_ = EuclidMod(this->value_, mod);
    return tmp;
  }

  ModInt &operator--() {
    value_ -= 1;
    value_ = EuclidMod(value_, mod);
    return *this;
  }

  ModInt operator--(int) {
    ModInt tmp = *this;
    this->value_ -= 1;
    this->value_ = EuclidMod(this->value_, mod);
    return tmp;
  }

  int64_t GetValue() const { return value_; }
};

template <int64_t mod>
ModInt<mod> operator+(const ModInt<mod> &left, const ModInt<mod> &right) {
  return ModInt<mod>(left) += right;
}

template <int64_t mod>
ModInt<mod> operator-(const ModInt<mod> &left, const ModInt<mod> &right) {
  return ModInt<mod>(left) -= right;
}

template <int64_t mod>
ModInt<mod> operator/(const ModInt<mod> &left, const ModInt<mod> &right) {
  return ModInt<mod>(left) /= right;
}

template <int64_t mod>
ModInt<mod> operator*(const ModInt<mod> &left, const ModInt<mod> &right) {
  return ModInt<mod>(left) *= right;
}

template <int64_t mod>
ModInt<mod> operator^(const ModInt<mod> &left, const ModInt<mod> &right) {
  return ModInt<mod>(left) ^= right;
}

template <int64_t mod>
std::ostream &operator<<(std::ostream &stream, const ModInt<mod> &mod_int) {
  stream << mod_int.GetValue();
  return stream;
}

int main() {
  cout << std::fixed << std::setprecision(16);
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  const int64_t MOD = 1000000007;
  ModCombination cmb(2000010, MOD);
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  ModInt<MOD> result = ModInt<MOD>(cmb.GetCombination(r2 + c2 + 2, r2 + 1) - 1);
  result -= ModInt<MOD>(cmb.GetCombination(r1 + c2 + 1, r1) - 1);
  result -= ModInt<MOD>(cmb.GetCombination(r2 + c1 + 1, c1) - 1);
  result += ModInt<MOD>(cmb.GetCombination(r1 + c1, r1) - 1);
  cout << result << endl;

  return 0;
}