Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const bool b) { return (b ? "true" : "false"); }
string to_string(const char *s) { return to_string((string)s); }
string to_string(const char c) {
  string s = "'";
  s += c;
  s += "'";
  return s;
}
template <class T, class U> string to_string(const pair<T, U> &p) {
  string s = "(";
  s += to_string(p.first);
  s += ", ";
  s += to_string(p.second);
  s += ")";
  return s;
}
template <class T> string to_string(const T &a) {
  bool first = true;
  string s = "{";
  for (const auto &x : a) {
    if (!first) {
      s += ", ";
    }
    first = false;
    s += to_string(x);
  }
  s += "}";
  return s;
}
void debug_out() { cerr << '\n'; }
template <class Head, class... Tail> void debug_out(Head head, Tail... tail) {
  cerr << ' ' << to_string(head);
  debug_out(tail...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "] = ", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
using ll = long long;
using ld = long double;
const int mod = 1e9 + 7;
const int inf = 1e9 + 100;
const ll inf64 = 7e18l;
int mult(int x, int y) { return 1ll * x * y % mod; }
int add(int x, int y) {
  x += y;
  if (x >= mod) {
    return x - mod;
  }
  return x;
}
int sub(int x, int y) {
  x -= y;
  if (x < 0) {
    return x + mod;
  }
  return x;
}
void add_self(int &x, int y) { x = add(x, y); }
int bin_pow(int x, int y) {
  if (y == 0) {
    return 1;
  }
  if (y % 2 == 0) {
    return bin_pow(mult(x, x), y / 2);
  }
  return mult(x, bin_pow(x, y - 1));
}
int inv(int x) { return bin_pow(x, mod - 2); }
const int maxn = 1e6 + 5;
vector<int> fact(maxn), inv_fact(maxn);
int C(int x, int y) {
  assert(x >= y);
  return mult(fact[x], mult(inv_fact[y], inv_fact[x - y]));
}
int solve_function(int k, int n) { return C(k + n + 1, k + 1); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);
  fact[0] = 1;
  for (int i = 1; i < maxn; ++i) {
    fact[i] = mult(fact[i - 1], i);
  }
  for (int i = 0; i < maxn; ++i) {
    inv_fact[i] = inv(fact[i]);
  }
  int r1, r2, c1, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  int answer = 0;
  for (int i = r1; i <= r2; ++i) {
    int x2 = solve_function(i, c2);
    int x1 = solve_function(i, c1 - 1);
    add_self(answer, sub(x2, x1));
  }
  cout << answer << '\n';
  //~ int answer2 = 0;
  //~ for (int i = r1; i <= r2; ++i) {
  //~ for (int j = c1; j <= c2; ++j) {
  //~ add_self(answer2, C(i + j, i));
  //~ }
  //~ }
  //~ cout << answer2 << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const bool b) { return (b ? "true" : "false"); }
string to_string(const char *s) { return to_string((string)s); }
string to_string(const char c) {
  string s = "'";
  s += c;
  s += "'";
  return s;
}
template <class T, class U> string to_string(const pair<T, U> &p) {
  string s = "(";
  s += to_string(p.first);
  s += ", ";
  s += to_string(p.second);
  s += ")";
  return s;
}
template <class T> string to_string(const T &a) {
  bool first = true;
  string s = "{";
  for (const auto &x : a) {
    if (!first) {
      s += ", ";
    }
    first = false;
    s += to_string(x);
  }
  s += "}";
  return s;
}
void debug_out() { cerr << '\n'; }
template <class Head, class... Tail> void debug_out(Head head, Tail... tail) {
  cerr << ' ' << to_string(head);
  debug_out(tail...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "] = ", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
using ll = long long;
using ld = long double;
const int mod = 1e9 + 7;
const int inf = 1e9 + 100;
const ll inf64 = 7e18l;
int mult(int x, int y) { return 1ll * x * y % mod; }
int add(int x, int y) {
  x += y;
  if (x >= mod) {
    return x - mod;
  }
  return x;
}
int sub(int x, int y) {
  x -= y;
  if (x < 0) {
    return x + mod;
  }
  return x;
}
void add_self(int &x, int y) { x = add(x, y); }
int bin_pow(int x, int y) {
  if (y == 0) {
    return 1;
  }
  if (y % 2 == 0) {
    return bin_pow(mult(x, x), y / 2);
  }
  return mult(x, bin_pow(x, y - 1));
}
int inv(int x) { return bin_pow(x, mod - 2); }
const int maxn = 4e6 + 5;
vector<int> fact(maxn), inv_fact(maxn);
int C(int x, int y) {
  assert(x >= y);
  return mult(fact[x], mult(inv_fact[y], inv_fact[x - y]));
}
int solve_function(int k, int n) { return C(k + n + 1, k + 1); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);
  fact[0] = 1;
  for (int i = 1; i < maxn; ++i) {
    fact[i] = mult(fact[i - 1], i);
  }
  for (int i = 0; i < maxn; ++i) {
    inv_fact[i] = inv(fact[i]);
  }
  int r1, r2, c1, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  int answer = 0;
  for (int i = r1; i <= r2; ++i) {
    int x2 = solve_function(i, c2);
    int x1 = solve_function(i, c1 - 1);
    add_self(answer, sub(x2, x1));
  }
  cout << answer << '\n';
  //~ int answer2 = 0;
  //~ for (int i = r1; i <= r2; ++i) {
  //~ for (int j = c1; j <= c2; ++j) {
  //~ add_self(answer2, C(i + j, i));
  //~ }
  //~ }
  //~ cout << answer2 << '\n';
  return 0;
}