Problem p02807 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
using ll = long long;
using ull = unsigned long long;

const int INF = 2100100100;
const int MOD = 1e9 + 7;

// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <int mod> struct ModInt {
  int val;
  ModInt() : val(0) {}
  ModInt(long long x) : val(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}
  ModInt &operator+=(const ModInt &p) {
    if ((val += p.val) >= mod) {
      val -= mod;
    }
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((val += mod - p.val) >= mod) {
      val -= mod;
    }
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    val = (int)(1LL * val * p.val % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt operator-() const { return ModInt(-val); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return val == p.val; }
  bool operator!=(const ModInt &p) const { return val != p.val; }
  ModInt inverse() const {
    int a = val, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
  ModInt pow(long long n) const {
    ModInt ret(1), mul(val);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.val;
  }
  friend istream &operator>>(istream &is, ModInt &a) {
    long long t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
  static int get_mod() { return mod; }
};
using modint = ModInt<MOD>;

int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<int> x(N);
  vector<modint> a(N), c(N), fib(N);
  REP(i, N) { cin >> x.at(i); }
  REP(i, N - 1) { a[i + 1] = modint(x[i + 1] - x[i]); }

  fib[0] = modint(1);
  REP(i, N) { fib[i + 1] = fib[i] * modint(i + 1); }

  c[0] = modint(0);
  REP(i, N - 1) { c[i + 1] = c[i] + modint(1) / modint(i + 1); }

  modint ans = modint(0);
  REP(i, N - 1) { ans += a[i + 1] * c[i + 1]; }

  cout << fib[N - 1] * ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
using ll = long long;
using ull = unsigned long long;

const int INF = 2100100100;
const int MOD = 1e9 + 7;

// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <int mod> struct ModInt {
  int val;
  ModInt() : val(0) {}
  ModInt(long long x) : val(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}
  ModInt &operator+=(const ModInt &p) {
    if ((val += p.val) >= mod) {
      val -= mod;
    }
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((val += mod - p.val) >= mod) {
      val -= mod;
    }
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    val = (int)(1LL * val * p.val % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt operator-() const { return ModInt(-val); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return val == p.val; }
  bool operator!=(const ModInt &p) const { return val != p.val; }
  ModInt inverse() const {
    int a = val, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
  ModInt pow(long long n) const {
    ModInt ret(1), mul(val);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.val;
  }
  friend istream &operator>>(istream &is, ModInt &a) {
    long long t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
  static int get_mod() { return mod; }
};
using modint = ModInt<MOD>;

int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<int> x(N);
  vector<modint> a(N), c(N), fib(N);
  REP(i, N) { cin >> x.at(i); }
  REP(i, N - 1) { a[i + 1] = modint(x[i + 1] - x[i]); }

  fib[0] = modint(1);
  REP(i, N - 1) { fib[i + 1] = fib[i] * modint(i + 1); }

  c[0] = modint(0);
  REP(i, N - 1) { c[i + 1] = c[i] + modint(1) / modint(i + 1); }

  modint ans = modint(0);
  REP(i, N - 1) { ans += a[i + 1] * c[i + 1]; }

  cout << fib[N - 1] * ans << endl;

  return 0;
}