Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1000000 + 10;
const int mod = 1e9 + 7;
ll qpow(ll n, ll k) {
  ll res = 1;
  while (k) {
    if (k & 1)
      res = res * n % mod;
    n = n * n % mod;
    k >>= 1;
  }
  return res;
}
ll fac[N], inv[N];
ll C(int n, int k) { return fac[n] * inv[k] % mod * inv[n - k] % mod; }
ll f(int x, int y) {
  int n = 0, k = 0;
  ll S = 1, ans = S;
  for (int i = 1; i <= x; i++) {
    S = (S * 2 - C(n, k) + mod) % mod, n++;
    S = (S + C(n, k + 1)) % mod, k++;
    //        cout<<n<<' '<<k<<' '<<S<<endl;
    ans += S;
  }
  for (int i = 1; i <= y; i++) {
    S = (S * 2 - C(n, k) + mod) % mod, n++;
    //        cout<<n<<' '<<k<<' '<<S<<endl;
    ans += S;
  }
  n = 0, k = 0, S = 1, ans -= S;
  for (int i = 1; i <= y; i++) {
    S = (S * 2 - C(n, k) + mod) % mod, n++;
    //        cout<<n<<' '<<k<<' '<<S<<endl;
    ans -= S;
  }
  for (int i = 1; i <= x; i++) {
    S = (S * 2 - C(n, k) + mod) % mod, n++;
    S = (S + C(n, k + 1)) % mod, k++;
    //        cout<<n<<' '<<k<<' '<<S<<endl;
    ans -= S;
  }
  for (int i = 0; i <= y; i++)
    ans += C(i, 0);
  for (int i = 1; i <= x; i++)
    ans += C(i + y, i);
  //    cout<<x<<' '<<y<<' '<<ans<<endl;
  return (ans % mod + mod) % mod;
}
int main() {
  fac[0] = 1;
  for (int i = 1; i < N; i++)
    fac[i] = fac[i - 1] * i % mod;
  inv[N - 1] = qpow(fac[N - 1], mod - 2);
  for (int i = N - 2; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % mod;
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  ll ans = f(x2, y2) - f(x2, y1 - 1) - f(x1 - 1, y2) + f(x1 - 1, y1 - 1);
  ans = (ans % mod + mod) % mod;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2000000 + 10;
const int mod = 1e9 + 7;
ll qpow(ll n, ll k) {
  ll res = 1;
  while (k) {
    if (k & 1)
      res = res * n % mod;
    n = n * n % mod;
    k >>= 1;
  }
  return res;
}
ll fac[N], inv[N];
ll C(int n, int k) { return fac[n] * inv[k] % mod * inv[n - k] % mod; }
ll f(int x, int y) {
  int n = 0, k = 0;
  ll S = 1, ans = S;
  for (int i = 1; i <= x; i++) {
    S = (S * 2 - C(n, k) + mod) % mod, n++;
    S = (S + C(n, k + 1)) % mod, k++;
    //        cout<<n<<' '<<k<<' '<<S<<endl;
    ans += S;
  }
  for (int i = 1; i <= y; i++) {
    S = (S * 2 - C(n, k) + mod) % mod, n++;
    //        cout<<n<<' '<<k<<' '<<S<<endl;
    ans += S;
  }
  n = 0, k = 0, S = 1, ans -= S;
  for (int i = 1; i <= y; i++) {
    S = (S * 2 - C(n, k) + mod) % mod, n++;
    //        cout<<n<<' '<<k<<' '<<S<<endl;
    ans -= S;
  }
  for (int i = 1; i <= x; i++) {
    S = (S * 2 - C(n, k) + mod) % mod, n++;
    S = (S + C(n, k + 1)) % mod, k++;
    //        cout<<n<<' '<<k<<' '<<S<<endl;
    ans -= S;
  }
  for (int i = 0; i <= y; i++)
    ans += C(i, 0);
  for (int i = 1; i <= x; i++)
    ans += C(i + y, i);
  //    cout<<x<<' '<<y<<' '<<ans<<endl;
  return (ans % mod + mod) % mod;
}
int main() {
  fac[0] = 1;
  for (int i = 1; i < N; i++)
    fac[i] = fac[i - 1] * i % mod;
  inv[N - 1] = qpow(fac[N - 1], mod - 2);
  for (int i = N - 2; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % mod;
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  ll ans = f(x2, y2) - f(x2, y1 - 1) - f(x1 - 1, y2) + f(x1 - 1, y1 - 1);
  ans = (ans % mod + mod) % mod;
  cout << ans << endl;
}