Problem p03655 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

#define bomb exit(1)
#define INF 1061109567
#define LINF 4557430888798830399ll
#define pprint(x) print(x), putchar(' ')
#define fprint(x) print(x), putchar('\n')
#define EE(x)                                                                  \
  ;                                                                            \
  struct edge {                                                                \
    int nxt, to;                                                               \
    ll w;                                                                      \
  } e[M << 1];                                                                 \
  int head[N], ecnt = 1;                                                       \
  void add(int u, int v, ll w = 0) {                                           \
    e[++ecnt].w = w, e[ecnt].to = v, e[ecnt].nxt = head[u];                    \
    head[u] = ecnt;                                                            \
  }                                                                            \
  void add_edge(int u, int v, ll w = 0) { add(u, v, w), add(v, u, w *x); }
#define ll long long
int mod = 1000000007;
ll Mod(ll x) { return x >= mod ? x - mod : x; }
#define eps 0.0000000001
#define sqr(x) ((x) * (x))
#define iter iterator
#define fi first
#define se second
// #define getchar() (SS == TT && (TT = (SS = BB) + fread(BB,1,1 << 15,stdin),TT
// == SS) ? EOF : *SS++) char BB[1 << 15],*SS = BB,*TT = BB;
ll read() {
  ll x = 0;
  int f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + (ch ^ 48);
  return x * f;
}
void print(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}

const int N = 1000010;
ll qpow(ll a, int b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod, b >>= 1;
  }
  return ans;
}
ll fac[N], ifac[N];
ll C(int n, int m) { return fac[n] * ifac[m] % mod * ifac[n - m] % mod; }
ll G(int x1, int y1, int x2, int y2) {
  return C(abs(x2 - x1) + abs(y2 - y1), abs(x2 - x1));
}
ll X[10], Y[10], ans;
ll calc(int x1, int y1, int x2, int y2, int f1, int f2) {
  ll res = 0;
  for (int x = X[3]; x <= X[4]; x++) {
    res = (res +
           G(x1, y1, x, Y[4]) * G(x, Y[4] + 1, x2, y2) % mod * (x + Y[4] + 1)) %
          mod;
    res = Mod(res -
              G(x1, y1, x, Y[3] - 1) * G(x, Y[3], x2, y2) % mod * (x + Y[3]) %
                  mod +
              mod);
  }
  for (int y = Y[3]; y <= Y[4]; y++) {
    res = (res +
           G(x1, y1, X[4], y) * G(X[4] + 1, y, x2, y2) % mod * (X[4] + y + 1)) %
          mod;
    res = Mod(res -
              G(x1, y1, X[3] - 1, y) * G(X[3], y, x2, y2) % mod * (X[3] + y) %
                  mod +
              mod);
  }
  return res * f1 * f2;
}
struct point {
  ll x, y, f;
} p[20];
int main() {
  for (int i = 1; i <= 6; i++)
    X[i] = read();
  for (int i = 1; i <= 6; i++)
    Y[i] = read();
  fac[0] = 1;
  for (int i = 1; i < N; i++)
    fac[i] = fac[i - 1] * i % mod;
  ifac[N - 1] = qpow(fac[N - 1], mod - 2);
  for (int i = N - 1; i; i--)
    ifac[i - 1] = ifac[i] * i % mod;

  p[1] = {X[1] - 1, Y[1] - 1, 1};
  p[4] = {X[2], Y[2], 1};
  p[2] = {X[2], Y[1] - 1, -1};
  p[3] = {X[1] - 1, Y[2], -1};

  p[5] = {X[6] + 1, Y[6] + 1, 1};
  p[8] = {X[5], Y[5], 1};
  p[6] = {X[6] + 1, Y[5], -1}, p[7] = {X[5], Y[6] + 1, -1};

  for (int i = 1; i <= 4; i++)
    for (int j = 5; j <= 8; j++)
      ans = (ans + calc(p[i].x, p[i].y, p[j].x, p[j].y, p[i].f, p[j].f) + mod) %
            mod;
  fprint(ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

#define bomb exit(1)
#define INF 1061109567
#define LINF 4557430888798830399ll
#define pprint(x) print(x), putchar(' ')
#define fprint(x) print(x), putchar('\n')
#define EE(x)                                                                  \
  ;                                                                            \
  struct edge {                                                                \
    int nxt, to;                                                               \
    ll w;                                                                      \
  } e[M << 1];                                                                 \
  int head[N], ecnt = 1;                                                       \
  void add(int u, int v, ll w = 0) {                                           \
    e[++ecnt].w = w, e[ecnt].to = v, e[ecnt].nxt = head[u];                    \
    head[u] = ecnt;                                                            \
  }                                                                            \
  void add_edge(int u, int v, ll w = 0) { add(u, v, w), add(v, u, w *x); }
#define ll long long
int mod = 1000000007;
ll Mod(ll x) { return x >= mod ? x - mod : x; }
#define eps 0.0000000001
#define sqr(x) ((x) * (x))
#define iter iterator
#define fi first
#define se second
// #define getchar() (SS == TT && (TT = (SS = BB) + fread(BB,1,1 << 15,stdin),TT
// == SS) ? EOF : *SS++) char BB[1 << 15],*SS = BB,*TT = BB;
ll read() {
  ll x = 0;
  int f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + (ch ^ 48);
  return x * f;
}
void print(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}

const int N = 2000010;
ll qpow(ll a, int b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod, b >>= 1;
  }
  return ans;
}
ll fac[N], ifac[N];
ll C(int n, int m) { return fac[n] * ifac[m] % mod * ifac[n - m] % mod; }
ll G(int x1, int y1, int x2, int y2) {
  return C(abs(x2 - x1) + abs(y2 - y1), abs(x2 - x1));
}
ll X[10], Y[10], ans;
ll calc(int x1, int y1, int x2, int y2, int f1, int f2) {
  ll res = 0;
  for (int x = X[3]; x <= X[4]; x++) {
    res = (res +
           G(x1, y1, x, Y[4]) * G(x, Y[4] + 1, x2, y2) % mod * (x + Y[4] + 1)) %
          mod;
    res = Mod(res -
              G(x1, y1, x, Y[3] - 1) * G(x, Y[3], x2, y2) % mod * (x + Y[3]) %
                  mod +
              mod);
  }
  for (int y = Y[3]; y <= Y[4]; y++) {
    res = (res +
           G(x1, y1, X[4], y) * G(X[4] + 1, y, x2, y2) % mod * (X[4] + y + 1)) %
          mod;
    res = Mod(res -
              G(x1, y1, X[3] - 1, y) * G(X[3], y, x2, y2) % mod * (X[3] + y) %
                  mod +
              mod);
  }
  return res * f1 * f2;
}
struct point {
  ll x, y, f;
} p[20];
int main() {
  for (int i = 1; i <= 6; i++)
    X[i] = read();
  for (int i = 1; i <= 6; i++)
    Y[i] = read();
  fac[0] = 1;
  for (int i = 1; i < N; i++)
    fac[i] = fac[i - 1] * i % mod;
  ifac[N - 1] = qpow(fac[N - 1], mod - 2);
  for (int i = N - 1; i; i--)
    ifac[i - 1] = ifac[i] * i % mod;

  p[1] = {X[1] - 1, Y[1] - 1, 1};
  p[4] = {X[2], Y[2], 1};
  p[2] = {X[2], Y[1] - 1, -1};
  p[3] = {X[1] - 1, Y[2], -1};

  p[5] = {X[6] + 1, Y[6] + 1, 1};
  p[8] = {X[5], Y[5], 1};
  p[6] = {X[6] + 1, Y[5], -1}, p[7] = {X[5], Y[6] + 1, -1};

  for (int i = 1; i <= 4; i++)
    for (int j = 5; j <= 8; j++)
      ans = (ans + calc(p[i].x, p[i].y, p[j].x, p[j].y, p[i].f, p[j].f) + mod) %
            mod;
  fprint(ans);
  return 0;
}