Problem p03655 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define mem(a, b) memset(a, b, sizeof(a))
#define For(i, a, b) for (int i = a, i##E = b; i <= i##E; ++i)
#define rFor(i, a, b) for (int i = a, i##E = b; i >= i##E; --i)
typedef long long LL;
using namespace std;
const int N = 2000010;
const LL mod = 1e9 + 7;
template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline void read(T &x) {
  x = 0;
  int _f(0);
  char ch = getchar();
  while (!isdigit(ch))
    _f |= (ch == '-'), ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  x = _f ? -x : x;
}
inline void file() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}
int x[7], y[7];
LL fac[N], ifac[N], ans, ret;
inline LL qpow(LL a, LL b) {
  LL ret = 1;
  for (; b; b >>= 1, a = a * a % mod)
    if (b & 1)
      ret = ret * a % mod;
  return ret;
}
inline void init(int n) {
  fac[0] = ifac[0] = 1;
  For(i, 1, n) fac[i] = fac[i - 1] * i % mod;
  ifac[n] = qpow(fac[n], mod - 2);
  rFor(i, n, 2) ifac[i - 1] = ifac[i] * i % mod;
}
inline LL C(int a, int b) { return fac[a + b] * ifac[a] % mod * ifac[b] % mod; }
inline LL C(int a, int b, int c, int d) {
  // printf("%d %d %d %d:\n",a,b,c,d);
  // printf("%lld\n",(C(c+1,d+1)+mod-C(a,d+1)+mod-C(c+1,b)+C(a,b))%mod);
  assert(a <= c);
  assert(b <= d);
  return (C(c + 1, d + 1) + mod - C(a, d + 1) + mod - C(c + 1, b) + C(a, b)) %
         mod;
}
int main() {
  file();
  For(i, 1, 6) read(x[i]);
  For(i, 1, 6) read(y[i]);
  init(2000005);
  For(i, x[3], x[4]) {
    int X = i, Y = y[3] - 1;
    ret = (X + Y + 1) * C(X - x[2], Y - y[2], X - x[1], Y - y[1]) % mod;
    Y++;
    ret = ret * C(x[5] - X, y[5] - Y, x[6] - X, y[6] - Y) % mod;
    ans = (ans + mod - ret) % mod;
  }
  For(i, y[3], y[4]) {
    int X = x[3] - 1, Y = i;
    ret = (X + 1 + Y) * C(X - x[2], Y - y[2], X - x[1], Y - y[1]) % mod;
    X++;
    ret = ret * C(x[5] - X, y[5] - Y, x[6] - X, y[6] - Y) % mod;
    ans = (ans + mod - ret) % mod;
  }
  For(i, x[3], x[4]) {
    int X = i, Y = y[4];
    ans = (ans + (X + Y + 1) * C(X - x[2], Y - y[2], X - x[1], Y - y[1]) % mod *
                     C(x[5] - X, y[5] - Y - 1, x[6] - X, y[6] - Y - 1) % mod) %
          mod;
  }
  For(i, y[3], y[4]) {
    int X = x[4], Y = i;
    ans = (ans + (X + Y + 1) * C(X - x[2], Y - y[2], X - x[1], Y - y[1]) % mod *
                     C(x[5] - X - 1, y[5] - Y, x[6] - X - 1, y[6] - Y) % mod) %
          mod;
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mem(a, b) memset(a, b, sizeof(a))
#define For(i, a, b) for (int i = a, i##E = b; i <= i##E; ++i)
#define rFor(i, a, b) for (int i = a, i##E = b; i >= i##E; --i)
typedef long long LL;
using namespace std;
const int N = 2000010;
const LL mod = 1e9 + 7;
template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline void read(T &x) {
  x = 0;
  int _f(0);
  char ch = getchar();
  while (!isdigit(ch))
    _f |= (ch == '-'), ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  x = _f ? -x : x;
}
inline void file() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}
int x[7], y[7];
LL fac[N], ifac[N], ans, ret;
inline LL qpow(LL a, LL b) {
  LL ret = 1;
  for (; b; b >>= 1, a = a * a % mod)
    if (b & 1)
      ret = ret * a % mod;
  return ret;
}
inline void init(int n) {
  fac[0] = ifac[0] = 1;
  For(i, 1, n) fac[i] = fac[i - 1] * i % mod;
  ifac[n] = qpow(fac[n], mod - 2);
  rFor(i, n, 2) ifac[i - 1] = ifac[i] * i % mod;
}
inline LL C(int a, int b) { return fac[a + b] * ifac[a] % mod * ifac[b] % mod; }
inline LL C(int a, int b, int c, int d) {
  // printf("%d %d %d %d:\n",a,b,c,d);
  // printf("%lld\n",(C(c+1,d+1)+mod-C(a,d+1)+mod-C(c+1,b)+C(a,b))%mod);
  assert(a <= c);
  assert(b <= d);
  return (C(c + 1, d + 1) + mod - C(a, d + 1) + mod - C(c + 1, b) + C(a, b)) %
         mod;
}
int main() {
  For(i, 1, 6) read(x[i]);
  For(i, 1, 6) read(y[i]);
  init(2000005);
  For(i, x[3], x[4]) {
    int X = i, Y = y[3] - 1;
    ret = (X + Y + 1) * C(X - x[2], Y - y[2], X - x[1], Y - y[1]) % mod;
    Y++;
    ret = ret * C(x[5] - X, y[5] - Y, x[6] - X, y[6] - Y) % mod;
    ans = (ans + mod - ret) % mod;
  }
  For(i, y[3], y[4]) {
    int X = x[3] - 1, Y = i;
    ret = (X + 1 + Y) * C(X - x[2], Y - y[2], X - x[1], Y - y[1]) % mod;
    X++;
    ret = ret * C(x[5] - X, y[5] - Y, x[6] - X, y[6] - Y) % mod;
    ans = (ans + mod - ret) % mod;
  }
  For(i, x[3], x[4]) {
    int X = i, Y = y[4];
    ans = (ans + (X + Y + 1) * C(X - x[2], Y - y[2], X - x[1], Y - y[1]) % mod *
                     C(x[5] - X, y[5] - Y - 1, x[6] - X, y[6] - Y - 1) % mod) %
          mod;
  }
  For(i, y[3], y[4]) {
    int X = x[4], Y = i;
    ans = (ans + (X + Y + 1) * C(X - x[2], Y - y[2], X - x[1], Y - y[1]) % mod *
                     C(x[5] - X - 1, y[5] - Y, x[6] - X - 1, y[6] - Y) % mod) %
          mod;
  }
  printf("%lld\n", ans);
  return 0;
}