Problem p03365 (C++)

Buggy submission (status: Runtime Error):
/*
 * Code be weshen zyk :'(
 * I won't surrender quietly
 */
// #pragma GCC optimize ("O3")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

template <class T>
using orderd_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define S second
#define F first
#define pb push_back

using ii = pair<int, int>;
using ll = long long;
using db = long double;

const int N = 2e5 + 5, K = 12, MOD = 1e9 + 7;
const ll MX = 1e15;
ll fast(ll b, ll e) {
  if (!e)
    return 1;
  if (e & 1)
    return b * fast(b, e - 1) % MOD;
  return fast(b * b % MOD, e >> 1);
}
ll F[N], iF[N];
void init() {
  F[0] = iF[0] = 1;
  for (int i = 1; i < N; i++) {
    F[i] = F[i - 1] * i % MOD;
    iF[i] = fast(F[i], MOD - 2);
  }
}
ll C(int n, int k) {
  if (k > n || k < 0)
    return 0;
  return F[n] * iF[k] % MOD * iF[n - k] % MOD;
}
int main() {
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif
  init();
  int n;
  cin >> n;
  ll ans = 0, cur = 0;

  for (int i = 1; i < n; i++) {
    ll num = C(i - 1, n - i - 1) * F[i] % MOD * F[n - i - 1] % MOD;
    num = (num - cur + MOD) % MOD;
    cur = (cur + num) % MOD;
    ans = (ans + num * i % MOD) % MOD;
  }

  cout << ans << '\n';

  return 0;
}


Fixed submission:
/*
 * Code be weshen zyk :'(
 * I won't surrender quietly
 */
// #pragma GCC optimize ("O3")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

template <class T>
using orderd_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define S second
#define F first
#define pb push_back

using ii = pair<int, int>;
using ll = long long;
using db = long double;

const int N = 1e6 + 5, K = 12, MOD = 1e9 + 7;
const ll MX = 1e15;
ll fast(ll b, ll e) {
  if (!e)
    return 1;
  if (e & 1)
    return b * fast(b, e - 1) % MOD;
  return fast(b * b % MOD, e >> 1);
}
ll F[N], iF[N];
void init() {
  F[0] = iF[0] = 1;
  for (int i = 1; i < N; i++) {
    F[i] = F[i - 1] * i % MOD;
    iF[i] = fast(F[i], MOD - 2);
  }
}
ll C(int n, int k) {
  if (k > n || k < 0)
    return 0;
  return F[n] * iF[k] % MOD * iF[n - k] % MOD;
}
int main() {
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif
  init();
  int n;
  cin >> n;
  ll ans = 0, cur = 0;

  for (int i = 1; i < n; i++) {
    ll num = C(i - 1, n - i - 1) * F[i] % MOD * F[n - i - 1] % MOD;
    num = (num - cur + MOD) % MOD;
    cur = (cur + num) % MOD;
    ans = (ans + num * i % MOD) % MOD;
  }

  cout << ans << '\n';

  return 0;
}