Problem p02807 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3fLL
// #define MOD 998244353
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<pint> vpint;
int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1};
const int SIZE = 10000;
// ↑templete
ll mod_pow(ll x, ll n, ll mod) {
  if (n == 0)
    return 1;
  ll res = mod_pow((x * x) % mod, n / 2, mod);
  if (n & 1)
    res = (res * x) % mod;
  return res;
}
signed main() {
  ll N;
  cin >> N;
  vll A, D;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (A.size())
      D.pb(a - A.back());
    A.pb(a);
  }

  ll C[SIZE] = {1, 1};
  for (int i = 2; i < N; i++) {
    C[i] = C[i - 1] + mod_pow(i, MOD - 2, MOD);
    C[i] %= MOD;
  }

  ll ans = 0;
  for (int i = 0; i < N - 1; i++) {
    ans += D[i] * C[i + 1];
    ans %= MOD;
  }

  for (int i = 1; i <= N - 1; i++) {
    ans *= i;
    ans %= MOD;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3fLL
// #define MOD 998244353
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<pint> vpint;
int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1};
const int SIZE = 100050;
// ↑templete
ll mod_pow(ll x, ll n, ll mod) {
  if (n == 0)
    return 1;
  ll res = mod_pow((x * x) % mod, n / 2, mod);
  if (n & 1)
    res = (res * x) % mod;
  return res;
}
signed main() {
  ll N;
  cin >> N;
  vll A, D;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (A.size())
      D.pb(a - A.back());
    A.pb(a);
  }

  ll C[SIZE] = {1, 1};
  for (int i = 2; i < N; i++) {
    C[i] = C[i - 1] + mod_pow(i, MOD - 2, MOD);
    C[i] %= MOD;
  }

  ll ans = 0;
  for (int i = 0; i < N - 1; i++) {
    ans += D[i] * C[i + 1];
    ans %= MOD;
  }

  for (int i = 1; i <= N - 1; i++) {
    ans *= i;
    ans %= MOD;
  }
  cout << ans << endl;

  return 0;
}