Problem p02782 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#define dumpv(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); i++)
#define mins(x, y) (x = min(x, y))
#define maxs(x, y) (x = max(x, y))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using P = pair<int, int>;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

struct mint {
  ll v;
  mint() : v(0) {}
  mint(ll v) : v((v % MOD + MOD) % MOD) {}
  operator ll() const { return v; }
  mint pow(ll t) const;
  mint inv() const;
};
mint &operator+=(mint &a, mint b) { return a = a.v + b.v; }
mint &operator-=(mint &a, mint b) { return a = a.v - b.v; }
mint &operator*=(mint &a, mint b) { return a = a.v * b.v; }
mint operator+(mint a, mint b) { return a += b; }
mint operator-(mint a, mint b) { return a -= b; }
mint operator*(mint a, mint b) { return a *= b; }
mint operator-(mint a) { return (mint)0 - a; }
mint &operator/=(mint &a, mint b) { return a *= b.inv(); }
mint operator/(mint a, mint b) { return a /= b; }
mint mint::pow(ll t) const {
  if (!t)
    return 1;
  mint a = pow(t >> 1);
  a *= a;
  if (t & 1)
    a *= *this;
  return a;
}
mint mint::inv() const { return pow(MOD - 2); }
bool operator==(mint a, mint b) { return a.v == b.v; }
bool operator!=(mint a, mint b) { return a.v != b.v; }
istream &operator>>(istream &os, mint &a) { return (os >> a.v); }
ostream &operator<<(ostream &os, const mint &a) { return (os << a.v); }

mint mfact(int n) { // n! (mod MOD)
  static vector<mint> fact(1, 1);
  int pn = fact.size();
  if (pn < n + 1) {
    fact.resize(n + 1);
    for (int i = pn; i <= n; i++)
      fact[i] = fact[i - 1] * (mint)i;
  }
  return fact[n];
}
mint nPk(int n, int k) {
  if (n < 0 || k < 0 || n < k)
    return 0;
  if (k == 0)
    return 1;
  return mfact(n) * mfact(n - k).inv();
}
mint nCk(int n, int k) {
  if (n < 0 || k < 0 || n < k)
    return 0;
  if (k == 0)
    return 1;
  return mfact(n) * mfact(k).inv() * mfact(n - k).inv();
}

mint f(int r, int c) {
  mint res = 0;
  for (int i = 1; i <= r; i++) {
    res += nCk(i + c + 1, c) - (mint)1;
  }
  return res;
}

void solve() {
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  if (abs(r2 - r1) > abs(c2 - c1)) {
    swap(r1, c1);
    swap(r2, c2);
  }
  dump(r1, r2, c1, c2);
  mint ans = 0;
  ans = f(r2, c2) - f(r1 - 1, c2) - f(r2, c1 - 1) + f(r1 - 1, c1 - 1);
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  // freopen("temp.1", "r", stdin);
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#define dumpv(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); i++)
#define mins(x, y) (x = min(x, y))
#define maxs(x, y) (x = max(x, y))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using P = pair<int, int>;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

struct mint {
  ll v;
  mint() : v(0) {}
  mint(ll v) : v((v % MOD + MOD) % MOD) {}
  operator ll() const { return v; }
  mint pow(ll t) const;
  mint inv() const;
};
mint &operator+=(mint &a, mint b) { return a = a.v + b.v; }
mint &operator-=(mint &a, mint b) { return a = a.v - b.v; }
mint &operator*=(mint &a, mint b) { return a = a.v * b.v; }
mint operator+(mint a, mint b) { return a += b; }
mint operator-(mint a, mint b) { return a -= b; }
mint operator*(mint a, mint b) { return a *= b; }
mint operator-(mint a) { return (mint)0 - a; }
mint &operator/=(mint &a, mint b) { return a *= b.inv(); }
mint operator/(mint a, mint b) { return a /= b; }
mint mint::pow(ll t) const {
  if (!t)
    return 1;
  mint a = pow(t >> 1);
  a *= a;
  if (t & 1)
    a *= *this;
  return a;
}
mint mint::inv() const { return pow(MOD - 2); }
bool operator==(mint a, mint b) { return a.v == b.v; }
bool operator!=(mint a, mint b) { return a.v != b.v; }
istream &operator>>(istream &os, mint &a) { return (os >> a.v); }
ostream &operator<<(ostream &os, const mint &a) { return (os << a.v); }

mint mfact(int n) { // n! (mod MOD)
  static vector<mint> fact(1, 1);
  int pn = fact.size();
  if (pn < n + 1) {
    fact.resize(n + 1);
    for (int i = pn; i <= n; i++)
      fact[i] = fact[i - 1] * (mint)i;
  }
  return fact[n];
}
mint nPk(int n, int k) {
  if (n < 0 || k < 0 || n < k)
    return 0;
  if (k == 0)
    return 1;
  return mfact(n) * mfact(n - k).inv();
}
mint nCk(int n, int k) {
  if (n < 0 || k < 0 || n < k)
    return 0;
  if (k == 0)
    return 1;
  return mfact(n) * mfact(k).inv() * mfact(n - k).inv();
}

mint f(int r, int c) {
  mint res = 0;
  // for (int i = 1; i <= r; i++) {
  //   res += nCk(i + c + 1, c) - (mint)1;
  // }
  res = nCk(r + 1 + c + 1, r + 1) - nCk(c + 1, 1) - (mint)1;
  return res;
}

void solve() {
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  if (abs(r2 - r1) > abs(c2 - c1)) {
    swap(r1, c1);
    swap(r2, c2);
  }
  dump(r1, r2, c1, c2);
  mint ans = 0;
  ans = f(r2, c2) - f(r1 - 1, c2) - f(r2, c1 - 1) + f(r1 - 1, c1 - 1);
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  // freopen("temp.1", "r", stdin);
  solve();
  return 0;
}