Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

#define PI 3.1415926535897932384626433832795

using ll = long long;

class mod_ll {
public:
  ll m_v;
  static const ll m_m = 1000000007; // prime value ex. 1000000009
  mod_ll(ll v = 0) : m_v(v) {}
  mod_ll operator=(ll rhs) {
    m_v = rhs % m_m;
    return *this;
  }
  ll operator()() { return m_v; }
  mod_ll operator=(mod_ll rhs) {
    m_v = rhs.m_v;
    return *this;
  }
  mod_ll operator+=(ll rhs) {
    m_v = (m_v + rhs) % m_m;
    return *this;
  }
  mod_ll operator+=(mod_ll rhs) {
    m_v = (m_v + rhs.m_v) % m_m;
    return *this;
  }
  mod_ll operator+(mod_ll rhs) {
    auto ret = *this;
    return ret += rhs;
  }
  mod_ll operator-=(mod_ll rhs) {
    m_v = (m_v - rhs.m_v + m_m) % m_m;
    return *this;
  }
  mod_ll operator-(mod_ll rhs) {
    auto ret = *this;
    return ret -= rhs;
  }
  mod_ll operator*=(ll rhs) {
    m_v = (m_v * rhs) % m_m;
    return *this;
  }
  mod_ll operator*=(mod_ll rhs) {
    m_v = (m_v * rhs.m_v) % m_m;
    return *this;
  }
  mod_ll operator*(mod_ll rhs) {
    mod_ll ret = *this;
    return ret *= rhs;
  }
  mod_ll operator^(ll rhs) {
    mod_ll t = *this;
    mod_ll ret(1LL);

    for (; rhs > 0; rhs >>= 1) {
      if (rhs & 1)
        ret *= t;
      t *= t;
    }
    return ret;
  }
  mod_ll operator/=(mod_ll rhs) { return *this *= (rhs ^ (m_m - 2)); }
  mod_ll operator/=(ll rhs) {
    mod_ll t(rhs);
    return *this /= t;
  }
  mod_ll operator/(mod_ll rhs) {
    mod_ll ret = *this;
    return ret /= rhs;
  }
};

mod_ll mod_fact(int n) {
  static vector<mod_ll> memo(1);

  if (memo.size() <= n) {
    if (memo.size() == 1) {
      memo[0] = 1;
    }
    for (int i = memo.size(); i <= n; i++) {
      memo.push_back(memo.back() * i);
    }
  }
  return memo[n];
}

mod_ll mod_nCr(int n, int r) {
  mod_ll bunsi = mod_fact(n);
  mod_ll bunbo = mod_fact(n - r) * mod_fact(r);
  return bunsi / bunbo;
}

mod_ll mod_nPr(int n, int r) {
  mod_ll ret(1);

  for (int i = 0; i < r; i++) {
    ret *= (n - i);
  }
  return ret;
}

mod_ll g(int r, int c) {
  return mod_fact(r + c + 2) / (mod_fact(r + 1) * mod_fact(c + 1)) - 1;
}

int main(void) {
  int r1, c1, r2, c2;
  while (scanf("%d%d%d%d", &r1, &c1, &r2, &c2) == 4) {
    vector<vector<mod_ll>> dp(c2 + 1);
    for (auto &m : dp)
      m.resize(r2 + 1);

    for (int x = 0; x <= r2; x++)
      dp[0][x] = x + 1;
    for (int y = 1; y <= c2; y++)
      dp[y][0] = y + 1;

    for (int y = 1; y <= c2; y++) {
      for (int x = 1; x <= r2; x++) {
        dp[y][x] = dp[y - 1][x] + dp[y][x - 1] + 1;
      }
    }

    //		mod_ll ans=dp[c2][r2]-dp[c2][r1-1]-dp[c1-1][r2]+dp[c1-1][r1-1];
    mod_ll ans = g(c2, r2) - g(c2, r1 - 1) - g(c1 - 1, r2) + g(c1 - 1, r1 - 1);
    printf("%lld\n", ans());
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

#define PI 3.1415926535897932384626433832795

using ll = long long;

class mod_ll {
public:
  ll m_v;
  static const ll m_m = 1000000007; // prime value ex. 1000000009
  mod_ll(ll v = 0) : m_v(v) {}
  mod_ll operator=(ll rhs) {
    m_v = rhs % m_m;
    return *this;
  }
  ll operator()() { return m_v; }
  mod_ll operator=(mod_ll rhs) {
    m_v = rhs.m_v;
    return *this;
  }
  mod_ll operator+=(ll rhs) {
    m_v = (m_v + rhs) % m_m;
    return *this;
  }
  mod_ll operator+=(mod_ll rhs) {
    m_v = (m_v + rhs.m_v) % m_m;
    return *this;
  }
  mod_ll operator+(mod_ll rhs) {
    auto ret = *this;
    return ret += rhs;
  }
  mod_ll operator-=(mod_ll rhs) {
    m_v = (m_v - rhs.m_v + m_m) % m_m;
    return *this;
  }
  mod_ll operator-(mod_ll rhs) {
    auto ret = *this;
    return ret -= rhs;
  }
  mod_ll operator*=(ll rhs) {
    m_v = (m_v * rhs) % m_m;
    return *this;
  }
  mod_ll operator*=(mod_ll rhs) {
    m_v = (m_v * rhs.m_v) % m_m;
    return *this;
  }
  mod_ll operator*(mod_ll rhs) {
    mod_ll ret = *this;
    return ret *= rhs;
  }
  mod_ll operator^(ll rhs) {
    mod_ll t = *this;
    mod_ll ret(1LL);

    for (; rhs > 0; rhs >>= 1) {
      if (rhs & 1)
        ret *= t;
      t *= t;
    }
    return ret;
  }
  mod_ll operator/=(mod_ll rhs) { return *this *= (rhs ^ (m_m - 2)); }
  mod_ll operator/=(ll rhs) {
    mod_ll t(rhs);
    return *this /= t;
  }
  mod_ll operator/(mod_ll rhs) {
    mod_ll ret = *this;
    return ret /= rhs;
  }
};

mod_ll mod_fact(int n) {
  static vector<mod_ll> memo(1);

  if (memo.size() <= n) {
    if (memo.size() == 1) {
      memo[0] = 1;
    }
    for (int i = memo.size(); i <= n; i++) {
      memo.push_back(memo.back() * i);
    }
  }
  return memo[n];
}

mod_ll mod_nCr(int n, int r) {
  mod_ll bunsi = mod_fact(n);
  mod_ll bunbo = mod_fact(n - r) * mod_fact(r);
  return bunsi / bunbo;
}

mod_ll mod_nPr(int n, int r) {
  mod_ll ret(1);

  for (int i = 0; i < r; i++) {
    ret *= (n - i);
  }
  return ret;
}

mod_ll g(int r, int c) {
  return mod_fact(r + c + 2) / (mod_fact(r + 1) * mod_fact(c + 1)) - 1;
}

int main(void) {
  int r1, c1, r2, c2;
  while (scanf("%d%d%d%d", &r1, &c1, &r2, &c2) == 4) {
    mod_ll ans = g(c2, r2) - g(c2, r1 - 1) - g(c1 - 1, r2) + g(c1 - 1, r1 - 1);
    printf("%lld\n", ans());
  }
  return 0;
}