Problem p02807 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
#define int long long

#define ff first
#define ss second
// #define endl "\n"
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
#define PI 3.1415926535897932384626433832795
int MOD = 1e9 + 7;
int powmod(int a, int l, int md) {
  int res = 1;
  while (l) {
    if (l & 1)
      res = res * a % md;
    l /= 2;
    a = a * a % md;
  }
  return res;
}
int binpow(int a, int l) {
  int res = 1;
  while (l) {
    if (l & 1)
      res = res * a;
    l /= 2;
    a = a * a;
  }
  return res;
}
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> ii;
typedef vector<ii> vii;
#define pb push_back
int __set(int b, int i) { return b | (1 << i); } // set ith bit
int __unset(int b, int i) { return b & (~(1UL << i)); }
int __check(int b, int i) { return b & (1 << i); } // returns 0 if ith bit is 0
int mulmod(int a, int b, int md) { return ((a % md) * (b % md)) % md; }
int addmod(int a, int b, int md) { return (a + b) % md; }
int submod(int a, int b, int md) { return (((a - b) % md) + md) % md; }
int divmod(int a, int b, int md) {
  return mulmod(a, powmod(b, md - 2, md), md);
} // if md is prime;
const ll inf = 0xFFFFFFFFFFFFFFFL;                 // very large number
priority_queue<int, vector<int>, greater<int>> pq; // for min priority_queue
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
clock_t time_p = clock();
void time() {
  time_p = clock() - time_p;
  cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int getRand(int l, int r) {
  uniform_int_distribution<int> uid(l, r);
  return uid(rng);
}
int md = 1e9 + 7;
void manacher(vi &d1, vi &d2, int n, string &s) {
  for (int i = 0, l = 0, r = -1; i < n; i++) {
    int k = (i > r) ? 1 : min(d1[l + r - i], r - i + 1);
    while (0 <= i - k && i + k < n && s[i - k] == s[i + k])
      k++;
    d1[i] = k--;
    if (i + k > r) {
      l = i - k;
      r = i + k;
    }
  }
  for (int i = 0, l = 0, r = -1; i < n; i++) {
    int k = (i > r) ? 0 : min(d2[l + r - i + 1], r - i + 1);
    while (0 <= i - k - 1 && i + k < n && s[i - k - 1] == s[i + k])
      k++;
    d2[i] = k--;
    if (i + k > r) {
      l = i - k - 1;
      r = i + k;
    }
  }
}

#define arr (int)(4e5 + 10)

int f[arr];
int rf[arr];

int C(int l, int k, int md) {
  if (k < 0 || k > l) {
    return 0;
  }
  return f[l] * rf[k] % md * rf[l - k] % md;
}

void precfac() {
  f[0] = 1;
  for (int i = 1; i < arr; i++) {
    f[i] = f[i - 1] * i % MOD;
  }
  rf[arr - 1] = powmod(f[arr - 1], MOD - 2, MOD);
  for (int i = arr - 2; i >= 0; i--) {
    rf[i] = rf[i + 1] * (i + 1) % MOD;
  }
}

signed main(void) {
  IOS;
  precfac();
  int n;
  cin >> n;
  vi x(n);
  int ans = 0;
  for (int i = 0; i < n; i++)
    cin >> x[i];
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (j == n - 1) {
        // int tp =
        // ((((x[j]-x[i])*(C(n-1,j-i,md)*(f[j-i-1]))%MOD)%MOD)*f[n-1-(j-i)])%MOD;
        int tp = ((((x[j] - x[i]) * C(n - 1, j - i, md)) % MOD) *
                  ((f[j - i - 1] * f[n - (j - i) - 1]) % MOD)) %
                 MOD;
        ans = (ans + tp) % MOD;
      } else {
        int tp = ((((x[j] - x[i]) * C(n - 1, j - i + 1, md)) % MOD) *
                  ((f[j - i - 1] * f[n - (j - i + 1) - 1]) % MOD)) %
                 MOD;
        // int tp =
        // ((((x[j]-x[i])*(C(n-1,j-i+1,md)*(f[j-i-1]))%MOD)%MOD)*f[n-1-(j-i+1)])%MOD;
        ans = (ans + tp) % MOD;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define int long long

#define ff first
#define ss second
// #define endl "\n"
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
#define PI 3.1415926535897932384626433832795
int MOD = 1e9 + 7;
int powmod(int a, int l, int md) {
  int res = 1;
  while (l) {
    if (l & 1)
      res = res * a % md;
    l /= 2;
    a = a * a % md;
  }
  return res;
}
int binpow(int a, int l) {
  int res = 1;
  while (l) {
    if (l & 1)
      res = res * a;
    l /= 2;
    a = a * a;
  }
  return res;
}
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> ii;
typedef vector<ii> vii;
#define pb push_back
int __set(int b, int i) { return b | (1 << i); } // set ith bit
int __unset(int b, int i) { return b & (~(1UL << i)); }
int __check(int b, int i) { return b & (1 << i); } // returns 0 if ith bit is 0
int mulmod(int a, int b, int md) { return ((a % md) * (b % md)) % md; }
int addmod(int a, int b, int md) { return (a + b) % md; }
int submod(int a, int b, int md) { return (((a - b) % md) + md) % md; }
int divmod(int a, int b, int md) {
  return mulmod(a, powmod(b, md - 2, md), md);
} // if md is prime;
const ll inf = 0xFFFFFFFFFFFFFFFL;                 // very large number
priority_queue<int, vector<int>, greater<int>> pq; // for min priority_queue
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
clock_t time_p = clock();
void time() {
  time_p = clock() - time_p;
  cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int getRand(int l, int r) {
  uniform_int_distribution<int> uid(l, r);
  return uid(rng);
}
int md = 1e9 + 7;
void manacher(vi &d1, vi &d2, int n, string &s) {
  for (int i = 0, l = 0, r = -1; i < n; i++) {
    int k = (i > r) ? 1 : min(d1[l + r - i], r - i + 1);
    while (0 <= i - k && i + k < n && s[i - k] == s[i + k])
      k++;
    d1[i] = k--;
    if (i + k > r) {
      l = i - k;
      r = i + k;
    }
  }
  for (int i = 0, l = 0, r = -1; i < n; i++) {
    int k = (i > r) ? 0 : min(d2[l + r - i + 1], r - i + 1);
    while (0 <= i - k - 1 && i + k < n && s[i - k - 1] == s[i + k])
      k++;
    d2[i] = k--;
    if (i + k > r) {
      l = i - k - 1;
      r = i + k;
    }
  }
}

#define arr (int)(4e5 + 10)

int f[arr];
int rf[arr];

int C(int l, int k, int md) {
  if (k < 0 || k > l) {
    return 0;
  }
  return f[l] * rf[k] % md * rf[l - k] % md;
}

void precfac() {
  f[0] = 1;
  for (int i = 1; i < arr; i++) {
    f[i] = f[i - 1] * i % MOD;
  }
  rf[arr - 1] = powmod(f[arr - 1], MOD - 2, MOD);
  for (int i = arr - 2; i >= 0; i--) {
    rf[i] = rf[i + 1] * (i + 1) % MOD;
  }
}

signed main(void) {
  IOS;
  precfac();
  int n;
  cin >> n;
  vi x(n);
  int ans = 0;
  for (int i = 0; i < n; i++)
    cin >> x[i];
  for (int i = 0; i < n - 1; i++) {
    int j = n - 1;
    int tp = ((((x[j] - x[i]) * C(n - 1, j - i, md)) % MOD) *
              ((f[j - i - 1] * f[n - (j - i) - 1]) % MOD)) %
             MOD;
    ans = (ans + tp) % MOD;
  }
  // cout<<ans<<endl;
  int pre[n - 1];
  int suf[n - 1];
  suf[n - 2] = x[n - 2];
  pre[0] = x[0];
  for (int i = 1; i < n - 1; i++)
    pre[i] = pre[i - 1] + x[i];
  for (int i = n - 3; i >= 0; i--)
    suf[i] = suf[i + 1] + x[i];
  for (int p = 1; p < n - 1; p++) {
    int i = 0, j = p;
    int p1 = (suf[n - 1 - p] - pre[p - 1]) % MOD;
    int p2 = (((C(n - 1, j - i + 1, md) * (f[j - i - 1])) % MOD) *
              f[n - 1 - (j - i + 1)]) %
             MOD;
    int tp = (p1 * p2) % MOD;
    ans = (ans + tp) % MOD;
  }
  cout << ans << endl;
  return 0;
}