Problem p02807 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define WHOLE(v) (v).begin(), (v).end()
#define REV_WHOLE(v) (v).rbegin(), (v).rend()
using i64 = int64_t;
using namespace std;
template <size_t I, class H, class... T> struct TupleReaderWriter {
  static tuple<H, T...> r(istream &i) {
    H v;
    i >> v;
    return tuple_cat(tuple<H>(v),
                     TupleReaderWriter<sizeof...(T) - 1, T...>::r(i));
  }
  static void w(ostream &o, tuple<H, T...> &t, string d) {
    TupleReaderWriter<I - 1, H, T...>::w(o, t, d);
    o << d << get<I>(t);
  }
};
template <class H, class... T> struct TupleReaderWriter<0, H, T...> {
  static tuple<H, T...> r(istream &i) {
    H v;
    i >> v;
    return tuple<H>(v);
  }
  static void w(ostream &o, tuple<H, T...> &t, string d) { o << get<0>(t); }
};
template <class... T> istream &operator>>(istream &i, tuple<T...> &t) {
  t = TupleReaderWriter<sizeof...(T), T...>::r(i);
  return i;
}
template <class... T> ostream &operator<<(ostream &o, tuple<T...> &t) {
  string delim = " ";
  TupleReaderWriter<sizeof...(T) - 1, T...>::w(o, t, delim);
  return o;
}
template <class T> istream &operator>>(istream &i, vector<T> &v) {
  for (auto &x : v)
    i >> x;
  return i;
}
template <class T> ostream &operator<<(ostream &o, vector<T> &v) {
  string delim = "";
  for (auto &x : v)
    o << delim << x, delim = " ";
  return o;
}
template <class T>
using rev_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> using vector2d = vector<vector<T>>;
struct fixprec {
  int x;
  fixprec(int d) : x(d) {}
};
ostream &operator<<(ostream &o, fixprec f) {
  return o << fixed << setprecision(f.x);
}
void R_YESNO(bool p) { cout << (p ? "YES" : "NO") << endl; }
void R_YesNo(bool p) { cout << (p ? "Yes" : "No") << endl; }

const i64 MOD = 1e9 + 7;

int64_t powmod(int64_t a, int64_t p) {
  int64_t v = 1;
  for (int64_t b = a; p > 0; p >>= 1, b = (b * b) % MOD) {
    if (p & 1)
      v = (v * b) % MOD;
  }
  return v;
}

int64_t invmod(int64_t x) { return powmod(x, MOD - 2); }

int main() {
  int N;
  cin >> N;
  vector<i64> x(N), d(N);
  for (i64 &a : x)
    cin >> x;
  for (int i = 0; i < N - 1; i++) {
    d[i] = (x[i + 1] - x[i]) % MOD;
  }
  i64 ans = 0, s = 0;
  for (int k = 0; k < N - 1; k++) {
    s += invmod(k + 1);
    ans = (ans + (s * d[k]) % MOD) % MOD;
  }
  for (int i = 1; i < N; i++) {
    ans = (ans * i) % MOD;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define WHOLE(v) (v).begin(), (v).end()
#define REV_WHOLE(v) (v).rbegin(), (v).rend()
using i64 = int64_t;
using namespace std;
template <size_t I, class H, class... T> struct TupleReaderWriter {
  static tuple<H, T...> r(istream &i) {
    H v;
    i >> v;
    return tuple_cat(tuple<H>(v),
                     TupleReaderWriter<sizeof...(T) - 1, T...>::r(i));
  }
  static void w(ostream &o, tuple<H, T...> &t, string d) {
    TupleReaderWriter<I - 1, H, T...>::w(o, t, d);
    o << d << get<I>(t);
  }
};
template <class H, class... T> struct TupleReaderWriter<0, H, T...> {
  static tuple<H, T...> r(istream &i) {
    H v;
    i >> v;
    return tuple<H>(v);
  }
  static void w(ostream &o, tuple<H, T...> &t, string d) { o << get<0>(t); }
};
template <class... T> istream &operator>>(istream &i, tuple<T...> &t) {
  t = TupleReaderWriter<sizeof...(T), T...>::r(i);
  return i;
}
template <class... T> ostream &operator<<(ostream &o, tuple<T...> &t) {
  string delim = " ";
  TupleReaderWriter<sizeof...(T) - 1, T...>::w(o, t, delim);
  return o;
}
template <class T> istream &operator>>(istream &i, vector<T> &v) {
  for (auto &x : v)
    i >> x;
  return i;
}
template <class T> ostream &operator<<(ostream &o, vector<T> &v) {
  string delim = "";
  for (auto &x : v)
    o << delim << x, delim = " ";
  return o;
}
template <class T>
using rev_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> using vector2d = vector<vector<T>>;
struct fixprec {
  int x;
  fixprec(int d) : x(d) {}
};
ostream &operator<<(ostream &o, fixprec f) {
  return o << fixed << setprecision(f.x);
}
void R_YESNO(bool p) { cout << (p ? "YES" : "NO") << endl; }
void R_YesNo(bool p) { cout << (p ? "Yes" : "No") << endl; }

const i64 MOD = 1e9 + 7;

int64_t powmod(int64_t a, int64_t p) {
  int64_t v = 1;
  for (int64_t b = a; p > 0; p >>= 1, b = (b * b) % MOD) {
    if (p & 1)
      v = (v * b) % MOD;
  }
  return v;
}

int64_t invmod(int64_t x) { return powmod(x, MOD - 2); }

int main() {
  int N;
  cin >> N;
  vector<i64> x(N), d(N);
  cin >> x;
  for (int i = 0; i < N - 1; i++) {
    d[i] = (x[i + 1] - x[i]) % MOD;
  }
  i64 ans = 0, s = 0;
  for (int k = 0; k < N - 1; k++) {
    s += invmod(k + 1);
    ans = (ans + (s * d[k]) % MOD) % MOD;
  }
  for (int i = 1; i < N; i++) {
    ans = (ans * i) % MOD;
  }
  cout << ans << endl;
  return 0;
}