Problem p03655 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define F(i, l, r) for (int i = (l), _end_ = (int)(r); i <= _end_; ++i)
#define f(i, r, l) for (int i = (r), _end_ = (int)(l); i >= _end_; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define file(a) freopen(a ".in", "r", stdin), freopen(a ".out", "w", stdout)
using namespace std;

bool chkmin(int &a, int b) { return b < a ? a = b, 1 : 0; }
bool chkmax(int &a, int b) { return b > a ? a = b, 1 : 0; }

inline int read() {
  int x = 0, fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fh = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ '0');
  return x * fh;
}
int n;
int xx[6], yy[6];
int inv[2000005], c[2000005];
const int mod = 1e9 + 7;
int ans;
long long qpow(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1)
      res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
int getans(int x, int y) {
  return 1ll * c[x + y] * inv[x] % mod * inv[y] % mod;
}
int calc(int x, int y) {
  int res = 0;
  res = (res + getans(x - xx[2], y - yy[2])) % mod;
  res = (res + getans(x - xx[1] + 1, y - yy[1] + 1)) % mod;
  res = (res - getans(x - xx[1] + 1, y - yy[2]) + mod) % mod;
  res = (res - getans(x - xx[2], y - yy[1] + 1) + mod) % mod;
  return res;
}
int calc2(int x, int y) {
  int res = 0;
  res = (res + getans(xx[5] - x, yy[5] - y)) % mod;
  res = (res + getans(xx[6] - x + 1, yy[6] - y + 1)) % mod;
  res = (res - getans(xx[6] - x + 1, yy[5] - y) + mod) % mod;
  res = (res - getans(xx[5] - x, yy[6] - y + 1) + mod) % mod;
  return res;
}
int main() {
#ifndef ONLINE_JUDGE
  file("agc018e");
#endif
  F(i, 1, 6) xx[i] = read();
  F(i, 1, 6) yy[i] = read();
  c[0] = 1;
  F(i, 1, 2000000) c[i] = 1ll * c[i - 1] * i % mod;
  inv[2000000] = qpow(c[2000000], mod - 2);
  f(i, 1999999, 0) inv[i] = 1ll * (i + 1) * inv[i + 1] % mod;
  ans = 0;
  F(i, xx[3], xx[4]) {
    ans = (ans + 1ll * (mod - i - yy[3]) * calc(i, yy[3] - 1) % mod *
                     calc2(i, yy[3]) % mod) %
          mod;
    ans = (ans + 1ll * (i + yy[4] + 1) * calc(i, yy[4]) % mod *
                     calc2(i, yy[4] + 1) % mod) %
          mod;
  }
  F(i, yy[3], yy[4]) {
    ans = (ans + 1ll * (mod - i - xx[3]) * calc(xx[3] - 1, i) % mod *
                     calc2(xx[3], i) % mod) %
          mod;
    ans = (ans + 1ll * (i + xx[4] + 1) * calc(xx[4], i) % mod *
                     calc2(xx[4] + 1, i) % mod) %
          mod;
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define F(i, l, r) for (int i = (l), _end_ = (int)(r); i <= _end_; ++i)
#define f(i, r, l) for (int i = (r), _end_ = (int)(l); i >= _end_; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define file(a) freopen(a ".in", "r", stdin), freopen(a ".out", "w", stdout)
using namespace std;

bool chkmin(int &a, int b) { return b < a ? a = b, 1 : 0; }
bool chkmax(int &a, int b) { return b > a ? a = b, 1 : 0; }

inline int read() {
  int x = 0, fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fh = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ '0');
  return x * fh;
}
int n;
int xx[6], yy[6];
int inv[2000005], c[2000005];
const int mod = 1e9 + 7;
int ans;
long long qpow(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1)
      res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
int getans(int x, int y) {
  return 1ll * c[x + y] * inv[x] % mod * inv[y] % mod;
}
int calc(int x, int y) {
  int res = 0;
  res = (res + getans(x - xx[2], y - yy[2])) % mod;
  res = (res + getans(x - xx[1] + 1, y - yy[1] + 1)) % mod;
  res = (res - getans(x - xx[1] + 1, y - yy[2]) + mod) % mod;
  res = (res - getans(x - xx[2], y - yy[1] + 1) + mod) % mod;
  return res;
}
int calc2(int x, int y) {
  int res = 0;
  res = (res + getans(xx[5] - x, yy[5] - y)) % mod;
  res = (res + getans(xx[6] - x + 1, yy[6] - y + 1)) % mod;
  res = (res - getans(xx[6] - x + 1, yy[5] - y) + mod) % mod;
  res = (res - getans(xx[5] - x, yy[6] - y + 1) + mod) % mod;
  return res;
}
int main() {
  F(i, 1, 6) xx[i] = read();
  F(i, 1, 6) yy[i] = read();
  c[0] = 1;
  F(i, 1, 2000000) c[i] = 1ll * c[i - 1] * i % mod;
  inv[2000000] = qpow(c[2000000], mod - 2);
  f(i, 1999999, 0) inv[i] = 1ll * (i + 1) * inv[i + 1] % mod;
  ans = 0;
  F(i, xx[3], xx[4]) {
    ans = (ans + 1ll * (mod - i - yy[3]) * calc(i, yy[3] - 1) % mod *
                     calc2(i, yy[3]) % mod) %
          mod;
    ans = (ans + 1ll * (i + yy[4] + 1) * calc(i, yy[4]) % mod *
                     calc2(i, yy[4] + 1) % mod) %
          mod;
  }
  F(i, yy[3], yy[4]) {
    ans = (ans + 1ll * (mod - i - xx[3]) * calc(xx[3] - 1, i) % mod *
                     calc2(xx[3], i) % mod) %
          mod;
    ans = (ans + 1ll * (i + xx[4] + 1) * calc(xx[4], i) % mod *
                     calc2(xx[4] + 1, i) % mod) %
          mod;
  }
  printf("%d\n", ans);
  return 0;
}