Problem p02807 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define FORD(i, a, b) for (int i = a; i > (int)(b); i--)
#define PPC(x) __builtin_popcount(x)
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define ft first
#define sd second
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
using namespace std;

const int maxN = 1 << 11, mod = 1000000007;

template <class T1, class T2> inline void addMod(T1 &a, T2 b) {
  a = (a + b) % mod;
}
template <class T1, class T2> inline void multMod(T1 &a, T2 b) {
  a = a * b % mod;
}

long long qpow(long long a, long long b) {
  long long res0 = 1;
  for (; b != 0; b >>= 1) {
    if (b & 1)
      multMod(res0, a);
    multMod(a, a);
  }
  return res0;
}

long long fact[maxN], tcaf[maxN];

long long cf(int n, int k) {
  bool static initd = false;
  if (!initd) {
    fact[0] = 1;
    FOR(i, 1, maxN)
    fact[i] = fact[i - 1] * i % mod;
    tcaf[maxN - 1] = qpow(fact[maxN - 1], mod - 2);
    for (int i = maxN - 1; i > 0; i--)
      tcaf[i - 1] = tcaf[i] * i % mod;
    initd = true;
  }
  if (k < 0 or k > n)
    return 0;
  long long res0 = fact[n];
  multMod(res0, tcaf[k]);
  multMod(res0, tcaf[n - k]);
  return res0;
}

long long f(int n, int d) { return cf(n + 1, d + 1); }

int x[maxN];
long long pref[maxN];

int main() {
  int n;
  scanf("%d", &n);
  cf(n, 0);

  FOR(i, 0, n)
  scanf("%d", x + i);

  FOR(i, 1, n + 1)
  pref[i] = (pref[i - 1] + qpow(i, mod - 2)) % mod;

  long long res = 0;
  FOR(i, 0, n - 1)
  addMod(res, pref[i + 1] * (x[i + 1] - x[i]));

  multMod(res, fact[n - 1]);
  printf("%lld\n", res);
  return 0;
}


Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define FORD(i, a, b) for (int i = a; i > (int)(b); i--)
#define PPC(x) __builtin_popcount(x)
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define ft first
#define sd second
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
using namespace std;

const int maxN = 1 << 20, mod = 1000000007;

template <class T1, class T2> inline void addMod(T1 &a, T2 b) {
  a = (a + b) % mod;
}
template <class T1, class T2> inline void multMod(T1 &a, T2 b) {
  a = a * b % mod;
}

long long qpow(long long a, long long b) {
  long long res0 = 1;
  for (; b != 0; b >>= 1) {
    if (b & 1)
      multMod(res0, a);
    multMod(a, a);
  }
  return res0;
}

long long fact[maxN], tcaf[maxN];

long long cf(int n, int k) {
  bool static initd = false;
  if (!initd) {
    fact[0] = 1;
    FOR(i, 1, maxN)
    fact[i] = fact[i - 1] * i % mod;
    tcaf[maxN - 1] = qpow(fact[maxN - 1], mod - 2);
    for (int i = maxN - 1; i > 0; i--)
      tcaf[i - 1] = tcaf[i] * i % mod;
    initd = true;
  }
  if (k < 0 or k > n)
    return 0;
  long long res0 = fact[n];
  multMod(res0, tcaf[k]);
  multMod(res0, tcaf[n - k]);
  return res0;
}

long long f(int n, int d) { return cf(n + 1, d + 1); }

int x[maxN];
long long pref[maxN];

int main() {
  int n;
  scanf("%d", &n);
  cf(n, 0);

  FOR(i, 0, n)
  scanf("%d", x + i);

  FOR(i, 1, n + 1)
  pref[i] = (pref[i - 1] + qpow(i, mod - 2)) % mod;

  long long res = 0;
  FOR(i, 0, n - 1)
  addMod(res, pref[i + 1] * (x[i + 1] - x[i]));

  multMod(res, fact[n - 1]);
  printf("%lld\n", res);
  return 0;
}