Problem p03232 (C++)

Buggy submission (status: Runtime Error):
//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define shandom_ruffle random_shuffle

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
const int nax = 1007;
const ll mod = 1000 * 1000 * 1000 + 7;

int n;

ll dp[nax];

ll wyn;

ll sil[nax];
//~ ll kom[nax][nax];

ll dziel(ll a, ll b) {
  a %= mod;
  b %= mod;
  ll wyk = mod - 2;
  while (wyk) {
    if (wyk & 1)
      a = (a * b) % mod;
    b = (b * b) % mod;
    wyk >>= 1;
  }
  return a;
}

ll kom(int a, int b) {
  if (b > a || b < 0)
    return 0;
  return dziel(sil[a], sil[b] * sil[a - b]);
}

int main() {
  sil[0] = 1;
  for (int i = 1; i < nax; i++)
    sil[i] = (sil[i - 1] * i) % mod;
  scanf("%d", &n);
  dp[1] = 1;
  ll s = 1;
  for (int i = 2; i <= n; i++) {
    dp[i] = (sil[i] + sil[i - 1] * s) % mod;
    s = (s + dziel(dp[i], sil[i])) % mod;
  }
  debug() << range(dp + 1, dp + 1 + n);
  for (int i = 1; i <= n; i++) {
    dp[i] = (dp[i] + mod - sil[i]) % mod;
  }
  debug() << range(dp + 1, dp + 1 + n);
  for (int i = 1; i <= n; i++) {
    ll x;
    scanf("%lld", &x);
    ll mno = dp[i] * sil[n - i];
    mno %= mod;
    mno *= kom(n, i);
    mno %= mod;
    wyn = (wyn + x * mno) % mod;
    mno = dp[n - i + 1] * sil[i - 1];
    mno %= mod;
    mno *= kom(n, i - 1);
    mno %= mod;
    wyn = (wyn + x * mno) % mod;
    wyn = (wyn + x * sil[n]) % mod;
  }
  wyn %= mod;
  wyn += mod;
  wyn %= mod;
  printf("%lld\n", wyn);
  return 0;
}


Fixed submission:
//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define shandom_ruffle random_shuffle

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
const int nax = 1000 * 1007;
const ll mod = 1000 * 1000 * 1000 + 7;

int n;

ll dp[nax];

ll wyn;

ll sil[nax];
//~ ll kom[nax][nax];

ll dziel(ll a, ll b) {
  a %= mod;
  b %= mod;
  ll wyk = mod - 2;
  while (wyk) {
    if (wyk & 1)
      a = (a * b) % mod;
    b = (b * b) % mod;
    wyk >>= 1;
  }
  return a;
}

ll kom(int a, int b) {
  if (b > a || b < 0)
    return 0;
  return dziel(sil[a], sil[b] * sil[a - b]);
}

int main() {
  sil[0] = 1;
  for (int i = 1; i < nax; i++)
    sil[i] = (sil[i - 1] * i) % mod;
  scanf("%d", &n);
  dp[1] = 1;
  ll s = 1;
  for (int i = 2; i <= n; i++) {
    dp[i] = (sil[i] + sil[i - 1] * s) % mod;
    s = (s + dziel(dp[i], sil[i])) % mod;
  }
  debug() << range(dp + 1, dp + 1 + n);
  for (int i = 1; i <= n; i++) {
    dp[i] = (dp[i] + mod - sil[i]) % mod;
  }
  debug() << range(dp + 1, dp + 1 + n);
  for (int i = 1; i <= n; i++) {
    ll x;
    scanf("%lld", &x);
    ll mno = dp[i] * sil[n - i];
    mno %= mod;
    mno *= kom(n, i);
    mno %= mod;
    wyn = (wyn + x * mno) % mod;
    mno = dp[n - i + 1] * sil[i - 1];
    mno %= mod;
    mno *= kom(n, i - 1);
    mno %= mod;
    wyn = (wyn + x * mno) % mod;
    wyn = (wyn + x * sil[n]) % mod;
  }
  wyn %= mod;
  wyn += mod;
  wyn %= mod;
  printf("%lld\n", wyn);
  return 0;
}