Problem p02807 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define debug(x) cerr << #x << " " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
const int INF = 0x3f3f3f3f, N = 1e5 + 5;
const ll LINF = 1e18 + 5;
constexpr int mod = 1e9 + 7, two = (mod + 1) / 2;
int n, x[N], d[N];
ll sum[N];
ll fac[N], inv[N];
ll powmod(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
void init() {
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % mod;
  inv[n] = powmod(fac[n], mod - 2);
  for (int i = n - 1; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % mod;
}
ll comp(ll a, ll b) {
  if (a < b || b < 0)
    return 0;
  return fac[a] * inv[b] % mod * inv[a - b] % mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> x[i];
  for (int i = 1; i < n; i++)
    d[i] = x[i + 1] - x[i];
  for (int i = 1; i < n; i++)
    sum[i] = (sum[i - 1] + d[i]) % mod;
  init();
  ll res = 0;
  for (int i = 1; i < n - 1; i++) {
    for (int j = 1; j + i < n; j++) {
      ll cur = d[j + i] * comp(n - 1, i + 1) % mod * fac[n - i - 2] % mod *
               fac[i] % mod;
      // debug(cur);
      res = (res + cur) % mod;
    }
  }
  res = (res + fac[n - 1] * sum[n - 1] % mod) % mod;
  res = (res + mod) % mod;
  cout << res;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define debug(x) cerr << #x << " " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
const int INF = 0x3f3f3f3f, N = 1e5 + 5;
const ll LINF = 1e18 + 5;
constexpr int mod = 1e9 + 7, two = (mod + 1) / 2;
int n, x[N], d[N];
ll sum[N];
ll fac[N], inv[N];
ll powmod(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
void init() {
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % mod;
  inv[n] = powmod(fac[n], mod - 2);
  for (int i = n - 1; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % mod;
}
ll comp(ll a, ll b) {
  if (a < b || b < 0)
    return 0;
  return fac[a] * inv[b] % mod * inv[a - b] % mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> x[i];
  for (int i = 1; i < n; i++)
    d[i] = x[i + 1] - x[i];
  for (int i = 1; i < n; i++)
    sum[i] = (sum[i - 1] + d[i]) % mod;
  init();
  ll res = 0;
  for (int i = 1; i < n - 1; i++) {
    // 		for(int j=1; j+i<n; j++)
    // 		{
    // 			ll cur = d[j+i]*comp(n-1,
    // i+1)%mod*fac[n-i-2]%mod*fac[i]%mod;
    // 			//debug(cur);
    //			res = (res+cur)%mod;
    // 		}
    ll cur = comp(n - 1, i + 1) % mod * fac[n - i - 2] % mod * fac[i] % mod;
    cur = cur * (sum[n - 1] - sum[i]) % mod;
    res = (res + cur) % mod;
  }
  res = (res + fac[n - 1] * sum[n - 1] % mod) % mod;
  res = (res + mod) % mod;
  cout << res;
  return 0;
}