Problem p02807 (C++)

Buggy submission (status: Runtime Error):
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>

#include <algorithm>
#include <cmath>
#include <functional>

#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define TEST                                                                   \
  { IS_TEST = true; }
#define fi first
#define se second
#define pb(x)                                                                  \
  { push_back(x); }

using namespace std;
using ll = int_fast64_t;
using v_b = vector<bool>;
using v_ll = vector<ll>;
using str = string;
using v_str = vector<string>;
using p_ll = pair<ll, ll>;
using vv_b = vector<v_b>;
using vv_ll = vector<v_ll>;
using vp_ll = vector<p_ll>;
using vvv_ll = vector<vv_ll>;
using vvp_ll = vector<vp_ll>;

using ld = long double;
using v_ld = vector<ld>;
using vv_ld = vector<v_ld>;

bool IS_TEST = false;
ll ll_min64 = 1LL << 63;
ll ll_max64 = ~ll_min64;
ll ll_min32 = 1LL << 31;
ll ll_max32 = ~ll_min32;

ll MOD = 1000000007;

/*displaying functions for debug*/
template <class T> void show2(const T &x) { cout << x; }
template <class T1, class T2> void show2(const pair<T1, T2> &x) {
  cout << "{" << show2(x.first) << "," << show2(x.second) << "}";
}
template <class T> void show(const T &x) {
  if (!IS_TEST)
    return;
  show2(x);
  cout << endl;
}
template <class T> void v_show(const T &v, ll n = -1) {
  if (!IS_TEST)
    return;
  auto itr = v.begin();
  ll m = n;
  while (itr != v.end() && m != 0) {
    show2(*itr);
    cout << " ";
    itr++;
    m--;
  }
  cout << endl;
}
template <class T> void vv_show(const T &v, ll n = -1) {
  if (!IS_TEST)
    return;
  cout << "--------------------------------\n";
  auto itr = v.begin();
  ll m = n;
  while (itr != v.end() && m != 0) {
    v_show(*itr, n);
    itr++;
    m--;
  }
  cout << "--------------------------------" << endl;
}
/*--------------------------------*/

/*loading integers*/
void load(ll &x1) { cin >> x1; }
void load(ll &x1, ll &x2) { cin >> x1 >> x2; }
void load(ll &x1, ll &x2, ll &x3) { cin >> x1 >> x2 >> x3; }
void load(ll &x1, ll &x2, ll &x3, ll &x4) { cin >> x1 >> x2 >> x3 >> x4; }
void v_load(ll n, v_ll &v1, ll head = 0, ll tail = 0, ll init = 0) {
  ll m = n + head + tail;
  v1.assign(m, init);
  for (ll i = 0; i < n; i++) {
    cin >> v1[i + head];
  }
}
void v_load(ll n, v_ll &v1, v_ll &v2, ll head = 0, ll tail = 0, ll init = 0) {
  ll m = n + head + tail;
  v1.assign(m, init);
  v2.assign(m, init);
  for (ll i = 0; i < n; i++) {
    cin >> v1[i + head] >> v2[i + head];
  }
}
void v_load(ll n, v_ll &v1, v_ll &v2, v_ll &v3, ll head = 0, ll tail = 0,
            ll init = 0) {
  ll m = n + head + tail;
  v1.assign(m, init);
  v2.assign(m, init);
  v3.assign(m, init);
  for (ll i = 0; i < n; i++) {
    cin >> v1[i + head] >> v2[i + head] >> v3[i + head];
  }
}
void v_load(ll n, v_ll &v1, v_ll &v2, v_ll &v3, v_ll &v4, ll head = 0,
            ll tail = 0, ll init = 0) {
  ll m = n + head + tail;
  v1.assign(m, init);
  v2.assign(m, init);
  v3.assign(m, init);
  v4.assign(m, init);
  for (ll i = 0; i < n; i++) {
    cin >> v1[i + head] >> v2[i + head] >> v3[i + head] >> v4[i + head];
  }
}
/*--------------------------------*/

v_ll ll_mg(ll x1 = ll_max64, ll x2 = ll_max64, ll x3 = ll_max64,
           ll x4 = ll_max64) {
  v_ll x{x1, x2, x3, x4};
  sort(x.begin(), x.end());
  return x;
}

template <class T> void ch_max(T &x, const T &y) {
  if (x >= y)
    return;
  x = y;
}
template <class T> void ch_min(T &x, const T &y) {
  if (x <= y)
    return;
  x = y;
}
template <class T, class S> void ch_max(T &x, S &xx, const T &y, const S &yy) {
  if (x >= y)
    return;
  x = y;
  xx = yy;
}
template <class T, class S> void ch_min(T &x, S &xx, const T &y, const S &yy) {
  if (x <= y)
    return;
  x = y;
  xx = yy;
}

template <class T> void quit(T x) {
  cout << x << endl;
  exit(0);
}

ll rem(ll x, ll y) {
  ll z = x % y;
  return z >= 0 ? z : z + y;
}

/*modint*/
class ModInt {
public:
  static ll modulus;
  ll val;
  ModInt(ll n = 0) {
    val = n % modulus;
    if (val < 0)
      val += modulus;
  }
  ModInt operator+(const ModInt &rhs) const { return val + rhs.val; }
  ModInt operator-(const ModInt &rhs) const { return val - rhs.val; }
  ModInt operator*(const ModInt &rhs) const { return val * rhs.val; }
  ModInt &operator+=(const ModInt &rhs) {
    val += rhs.val;
    if (val >= modulus)
      val -= modulus;
    return *this;
  }
  ModInt &operator-=(const ModInt &rhs) {
    val -= rhs.val;
    if (val < modulus)
      val += modulus;
    return *this;
  }
  ModInt &operator*=(const ModInt &rhs) {
    val *= rhs.val;
    val %= modulus;
    return *this;
  }
  ModInt pow(ll a) const {
    a %= (modulus - 1);
    if (a < 0)
      a += (modulus - 1);
    ModInt ret(1);
    ModInt vv(val);
    while (a != 0) {
      if (a & 1)
        ret *= vv;
      a >>= 1;
      vv *= vv;
    }
    return ret;
  }
  ModInt inv() const { return pow(-1); }
  ModInt operator/(const ModInt &rhs) const { return *this * rhs.pow(-1); }
  ModInt &operator/=(const ModInt &rhs) {
    *this *= rhs.pow(-1);
    return *this;
  }
};
istream &operator>>(istream &istr, ModInt &rhs) {
  istr >> (rhs.val);
  return istr;
};
ostream &operator<<(ostream &ostr, const ModInt &rhs) {
  ostr << (rhs.val);
  return ostr;
};
vector<ModInt> mod_nck(ll n) {
  vector<ModInt> v(n + 1, 1);
  for (ll i = 1; i <= n; i++)
    v[i] = v[i - 1] * (n + 1 - i) / i;
  return v;
}
vector<ModInt> mod_npk(ll n) {
  vector<ModInt> v(n + 1, 1);
  for (ll i = 1; i <= n; i++)
    v[i] = v[i - 1] * (n + 1 - i);
  return v;
}
vector<ModInt> mod_fact(ll n) {
  vector<ModInt> v(n + 1, 1);
  for (ll i = 1; i <= n; i++)
    v[i] = v[i - 1] * i;
  return v;
}
vector<ModInt> mod_nck_2(ll n, ll k) {
  vector<ModInt> v(n + 1, 1);
  for (ll i = k + 1; i <= n; i++)
    v[i] = v[i - 1] * i / (i - k);
  return v;
}

ll ModInt::modulus = 1000000007;

ll N;
v_ll X, Y;
int main() {
  load(N);
  v_load(N, X);
  Y.assign(N - 1, 0);
  for (ll i = 0; i < N; i++) {
    Y[i] = ModInt(i + 1).inv().val;
  }
  for (ll i = 1; i < N; i++) {
    Y[i] = (Y[i - 1] + Y[i]) % MOD;
  }
  ModInt ans(0);
  for (ll i = 0; i + 1 < N; i++) {
    ans += (X[i + 1] - X[i]) * Y[i];
  }
  for (ll i = 1; i < N; i++) {
    ans *= i;
  }
  cout << ans.val << endl;
}


Fixed submission:
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>

#include <algorithm>
#include <cmath>
#include <functional>

#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define TEST                                                                   \
  { IS_TEST = true; }
#define fi first
#define se second
#define pb(x)                                                                  \
  { push_back(x); }

using namespace std;
using ll = int_fast64_t;
using v_b = vector<bool>;
using v_ll = vector<ll>;
using str = string;
using v_str = vector<string>;
using p_ll = pair<ll, ll>;
using vv_b = vector<v_b>;
using vv_ll = vector<v_ll>;
using vp_ll = vector<p_ll>;
using vvv_ll = vector<vv_ll>;
using vvp_ll = vector<vp_ll>;

using ld = long double;
using v_ld = vector<ld>;
using vv_ld = vector<v_ld>;

bool IS_TEST = false;
ll ll_min64 = 1LL << 63;
ll ll_max64 = ~ll_min64;
ll ll_min32 = 1LL << 31;
ll ll_max32 = ~ll_min32;

ll MOD = 1000000007;

/*displaying functions for debug*/
template <class T> void show2(const T &x) { cout << x; }
template <class T1, class T2> void show2(const pair<T1, T2> &x) {
  cout << "{" << show2(x.first) << "," << show2(x.second) << "}";
}
template <class T> void show(const T &x) {
  if (!IS_TEST)
    return;
  show2(x);
  cout << endl;
}
template <class T> void v_show(const T &v, ll n = -1) {
  if (!IS_TEST)
    return;
  auto itr = v.begin();
  ll m = n;
  while (itr != v.end() && m != 0) {
    show2(*itr);
    cout << " ";
    itr++;
    m--;
  }
  cout << endl;
}
template <class T> void vv_show(const T &v, ll n = -1) {
  if (!IS_TEST)
    return;
  cout << "--------------------------------\n";
  auto itr = v.begin();
  ll m = n;
  while (itr != v.end() && m != 0) {
    v_show(*itr, n);
    itr++;
    m--;
  }
  cout << "--------------------------------" << endl;
}
/*--------------------------------*/

/*loading integers*/
void load(ll &x1) { cin >> x1; }
void load(ll &x1, ll &x2) { cin >> x1 >> x2; }
void load(ll &x1, ll &x2, ll &x3) { cin >> x1 >> x2 >> x3; }
void load(ll &x1, ll &x2, ll &x3, ll &x4) { cin >> x1 >> x2 >> x3 >> x4; }
void v_load(ll n, v_ll &v1, ll head = 0, ll tail = 0, ll init = 0) {
  ll m = n + head + tail;
  v1.assign(m, init);
  for (ll i = 0; i < n; i++) {
    cin >> v1[i + head];
  }
}
void v_load(ll n, v_ll &v1, v_ll &v2, ll head = 0, ll tail = 0, ll init = 0) {
  ll m = n + head + tail;
  v1.assign(m, init);
  v2.assign(m, init);
  for (ll i = 0; i < n; i++) {
    cin >> v1[i + head] >> v2[i + head];
  }
}
void v_load(ll n, v_ll &v1, v_ll &v2, v_ll &v3, ll head = 0, ll tail = 0,
            ll init = 0) {
  ll m = n + head + tail;
  v1.assign(m, init);
  v2.assign(m, init);
  v3.assign(m, init);
  for (ll i = 0; i < n; i++) {
    cin >> v1[i + head] >> v2[i + head] >> v3[i + head];
  }
}
void v_load(ll n, v_ll &v1, v_ll &v2, v_ll &v3, v_ll &v4, ll head = 0,
            ll tail = 0, ll init = 0) {
  ll m = n + head + tail;
  v1.assign(m, init);
  v2.assign(m, init);
  v3.assign(m, init);
  v4.assign(m, init);
  for (ll i = 0; i < n; i++) {
    cin >> v1[i + head] >> v2[i + head] >> v3[i + head] >> v4[i + head];
  }
}
/*--------------------------------*/

v_ll ll_mg(ll x1 = ll_max64, ll x2 = ll_max64, ll x3 = ll_max64,
           ll x4 = ll_max64) {
  v_ll x{x1, x2, x3, x4};
  sort(x.begin(), x.end());
  return x;
}

template <class T> void ch_max(T &x, const T &y) {
  if (x >= y)
    return;
  x = y;
}
template <class T> void ch_min(T &x, const T &y) {
  if (x <= y)
    return;
  x = y;
}
template <class T, class S> void ch_max(T &x, S &xx, const T &y, const S &yy) {
  if (x >= y)
    return;
  x = y;
  xx = yy;
}
template <class T, class S> void ch_min(T &x, S &xx, const T &y, const S &yy) {
  if (x <= y)
    return;
  x = y;
  xx = yy;
}

template <class T> void quit(T x) {
  cout << x << endl;
  exit(0);
}

ll rem(ll x, ll y) {
  ll z = x % y;
  return z >= 0 ? z : z + y;
}

/*modint*/
class ModInt {
public:
  static ll modulus;
  ll val;
  ModInt(ll n = 0) {
    val = n % modulus;
    if (val < 0)
      val += modulus;
  }
  ModInt operator+(const ModInt &rhs) const { return val + rhs.val; }
  ModInt operator-(const ModInt &rhs) const { return val - rhs.val; }
  ModInt operator*(const ModInt &rhs) const { return val * rhs.val; }
  ModInt &operator+=(const ModInt &rhs) {
    val += rhs.val;
    if (val >= modulus)
      val -= modulus;
    return *this;
  }
  ModInt &operator-=(const ModInt &rhs) {
    val -= rhs.val;
    if (val < modulus)
      val += modulus;
    return *this;
  }
  ModInt &operator*=(const ModInt &rhs) {
    val *= rhs.val;
    val %= modulus;
    return *this;
  }
  ModInt pow(ll a) const {
    a %= (modulus - 1);
    if (a < 0)
      a += (modulus - 1);
    ModInt ret(1);
    ModInt vv(val);
    while (a != 0) {
      if (a & 1)
        ret *= vv;
      a >>= 1;
      vv *= vv;
    }
    return ret;
  }
  ModInt inv() const { return pow(-1); }
  ModInt operator/(const ModInt &rhs) const { return *this * rhs.pow(-1); }
  ModInt &operator/=(const ModInt &rhs) {
    *this *= rhs.pow(-1);
    return *this;
  }
};
istream &operator>>(istream &istr, ModInt &rhs) {
  istr >> (rhs.val);
  return istr;
};
ostream &operator<<(ostream &ostr, const ModInt &rhs) {
  ostr << (rhs.val);
  return ostr;
};
vector<ModInt> mod_nck(ll n) {
  vector<ModInt> v(n + 1, 1);
  for (ll i = 1; i <= n; i++)
    v[i] = v[i - 1] * (n + 1 - i) / i;
  return v;
}
vector<ModInt> mod_npk(ll n) {
  vector<ModInt> v(n + 1, 1);
  for (ll i = 1; i <= n; i++)
    v[i] = v[i - 1] * (n + 1 - i);
  return v;
}
vector<ModInt> mod_fact(ll n) {
  vector<ModInt> v(n + 1, 1);
  for (ll i = 1; i <= n; i++)
    v[i] = v[i - 1] * i;
  return v;
}
vector<ModInt> mod_nck_2(ll n, ll k) {
  vector<ModInt> v(n + 1, 1);
  for (ll i = k + 1; i <= n; i++)
    v[i] = v[i - 1] * i / (i - k);
  return v;
}

ll ModInt::modulus = 1000000007;

ll N;
v_ll X, Y;
int main() {
  load(N);
  v_load(N, X);
  Y.assign(N + 100, 0);
  for (ll i = 0; i < N; i++) {
    Y[i] = ModInt(i + 1).inv().val;
  }
  for (ll i = 1; i < N; i++) {
    Y[i] = (Y[i - 1] + Y[i]) % MOD;
  }
  ModInt ans(0);
  for (ll i = 0; i + 1 < N; i++) {
    ans += (X[i + 1] - X[i]) * Y[i];
  }
  for (ll i = 1; i < N; i++) {
    ans *= i;
  }
  cout << ans.val << endl;
}