Problem p03232 (C++)

Buggy submission (status: Runtime Error):
// problem:agc028_b
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mk make_pair
#define lob lower_bound
#define upb upper_bound
#define fst first
#define scd second

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

inline int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline ll readll() {
  ll f = 1, x = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

const int MAXN = 1e5 + 5, MOD = 1e9 + 7;
inline int mod(int x) { return x < MOD ? (x < 0 ? x + MOD : x) : x - MOD; }
inline int pow_mod(int x, int i) {
  int y = 1;
  while (i) {
    if (i & 1)
      y = (ll)y * x % MOD;
    x = (ll)x * x % MOD;
    i >>= 1;
  }
  return y;
}
int n, fac[MAXN], invf[MAXN], f[MAXN], g[MAXN];
inline int comb(int n, int k) {
  assert(n >= k);
  return (ll)fac[n] * invf[k] % MOD * invf[n - k] % MOD;
}
namespace NTT {
const ll MODs[3] = {469762049, 998244353, 1004535809};
int MOD, n, m, P, a[MAXN], b[MAXN], f[MAXN], g[MAXN], R[MAXN], r[3][MAXN];
ll ans[MAXN];
inline int pow_mod(int x, int i) {
  int y = 1;
  while (i) {
    if (i & 1)
      y = (ll)y * x % MOD;
    x = (ll)x * x % MOD;
    i >>= 1;
  }
  return y;
}
void NTT(int *a, int n, int flag) {
  for (int i = 0; i < n; ++i)
    if (i < R[i])
      swap(a[i], a[R[i]]);
  for (int i = 1; i < n; i <<= 1) {
    int T = pow_mod(3, (MOD - 1) / (i << 1));
    if (flag == -1)
      T = pow_mod(T, MOD - 2);
    for (int j = 0; j < n; j += (i << 1)) {
      int t = 1;
      for (int k = 0; k < i; ++k, t = (ll)t * T % MOD) {
        int Nx = a[j + k], Ny = (ll)t * a[i + j + k] % MOD;
        a[j + k] = ((ll)Nx + Ny) % MOD;
        a[i + j + k] = ((ll)Nx - Ny + MOD) % MOD;
      }
    }
  }
}
ll exgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll d = exgcd(b, a % b, y, x);
  y -= (a / b) * x;
  return d;
}
ll mul_mod(ll a, ll b, ll p) {
  a %= p;
  b %= p;
  ll c = (long double)a * b / p;
  ll ans = a * b - c * p;
  if (ans < 0)
    ans += p;
  if (ans >= p)
    ans -= p;
  return ans;
}
void solve() {
  // n=read()+1;m=read()+1;P=read();
  // for(int i=0;i<n;++i) a[i]=read();
  // for(int i=0;i<m;++i) b[i]=read();
  int lim = 1, ct = 0;
  while (lim <= n + m)
    lim <<= 1, ct++;
  for (int i = 0; i < lim; ++i)
    R[i] = (R[i >> 1] >> 1) | ((i & 1) << (ct - 1));
  for (int cur = 0; cur < 3; ++cur) {
    MOD = MODs[cur];
    memset(f, 0, sizeof(f));
    memset(g, 0, sizeof(g));
    for (int i = 0; i < n; ++i)
      f[i] = a[i];
    for (int i = 0; i < m; ++i)
      g[i] = b[i];
    NTT(f, lim, 1);
    NTT(g, lim, 1);
    for (int i = 0; i < lim; ++i)
      f[i] = (ll)f[i] * g[i] % MOD;
    NTT(f, lim, -1);
    int invlim = pow_mod(lim, MOD - 2);
    for (int i = 0; i < lim; ++i)
      r[cur][i] = (ll)f[i] * invlim % MOD;
  }
  const ll INV = mul_mod(pow_mod(MODs[0], MODs[2] - 2),
                         pow_mod(MODs[1], MODs[2] - 2), MODs[2]);
  for (int i = 0; i < lim; ++i) {
    ll M = MODs[0] * MODs[1];
    ll m0 = MODs[0], m1 = MODs[1], m2 = MODs[2];
    ll a = m0, b = m1, x, y;
    ll c = (r[1][i] - r[0][i] % m1 + m1) % m1;
    ll d = exgcd(a, b, x, y);
    x = mul_mod(x, c, b);
    ans[i] = (r[0][i] + mul_mod(x, m0, M)) % M;
    ll t = ((r[2][i] - ans[i]) % MODs[2] + MODs[2]) % MODs[2];
    t = mul_mod(t, INV, MODs[2]);
    ans[i] = (mul_mod(t, M, P) + ans[i]) % P;
  }
}
} // namespace NTT
int main() {
  n = read();
  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = (ll)fac[i - 1] * i % MOD;
  invf[n] = pow_mod(fac[n], MOD - 2);
  for (int i = n - 1; i >= 0; --i)
    invf[i] = (ll)invf[i + 1] * (i + 1) % MOD;
  //	int ans=0;
  //	for(int i=1;i<=n;++i){
  //		int w=read();
  //		for(int j=1;j<=n;++j){
  //			for(int k=1;k<=n-abs(i-j);++k){
  //				ans=mod(ans+(ll)w*comb(n-abs(i-j)-1,k-1)%MOD*fac[k-1]%MOD*fac[n-k]%MOD);
  //			}
  //		}
  //	}
  NTT::P = MOD;
  NTT::n = n + 1;
  NTT::m = n + 1;
  for (int i = 1; i <= n; ++i)
    NTT::a[i] = fac[n - i];
  for (int i = 0; i <= n; ++i)
    NTT::b[i] = invf[i];
  NTT::solve();
  for (int i = 1; i <= n; ++i)
    g[i] = NTT::ans[i];
  //	for(int i=1;i<=n;++i){
  //		for(int k=1;k<=i;++k)g[i]=mod(g[i]+(ll)fac[n-k]*invf[i-k]%MOD);
  //	}
  for (int i = 0; i < n; ++i)
    f[i] = mod((ll)g[n - i] * fac[n - i - 1] % MOD + f[i - 1]);
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    int w = read();
    ans = mod(ans +
              (ll)w *
                  mod(mod(f[min(i - 1, n - i)] + f[max(i - 1, n - i)]) - f[0]) %
                  MOD);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
// problem:agc028_b
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mk make_pair
#define lob lower_bound
#define upb upper_bound
#define fst first
#define scd second

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

inline int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline ll readll() {
  ll f = 1, x = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

const int MAXN = 1e5 + 5, MOD = 1e9 + 7;
inline int mod(int x) { return x < MOD ? (x < 0 ? x + MOD : x) : x - MOD; }
inline int pow_mod(int x, int i) {
  int y = 1;
  while (i) {
    if (i & 1)
      y = (ll)y * x % MOD;
    x = (ll)x * x % MOD;
    i >>= 1;
  }
  return y;
}
int n, fac[MAXN], invf[MAXN], f[MAXN], g[MAXN];
inline int comb(int n, int k) {
  assert(n >= k);
  return (ll)fac[n] * invf[k] % MOD * invf[n - k] % MOD;
}
namespace NTT {
const int MAXN = 4e5 + 5;
const ll MODs[3] = {469762049, 998244353, 1004535809};
int MOD, n, m, P, a[MAXN], b[MAXN], f[MAXN], g[MAXN], R[MAXN], r[3][MAXN];
ll ans[MAXN];
inline int pow_mod(int x, int i) {
  int y = 1;
  while (i) {
    if (i & 1)
      y = (ll)y * x % MOD;
    x = (ll)x * x % MOD;
    i >>= 1;
  }
  return y;
}
void NTT(int *a, int n, int flag) {
  for (int i = 0; i < n; ++i)
    if (i < R[i])
      swap(a[i], a[R[i]]);
  for (int i = 1; i < n; i <<= 1) {
    int T = pow_mod(3, (MOD - 1) / (i << 1));
    if (flag == -1)
      T = pow_mod(T, MOD - 2);
    for (int j = 0; j < n; j += (i << 1)) {
      int t = 1;
      for (int k = 0; k < i; ++k, t = (ll)t * T % MOD) {
        int Nx = a[j + k], Ny = (ll)t * a[i + j + k] % MOD;
        a[j + k] = ((ll)Nx + Ny) % MOD;
        a[i + j + k] = ((ll)Nx - Ny + MOD) % MOD;
      }
    }
  }
}
ll exgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll d = exgcd(b, a % b, y, x);
  y -= (a / b) * x;
  return d;
}
ll mul_mod(ll a, ll b, ll p) {
  a %= p;
  b %= p;
  ll c = (long double)a * b / p;
  ll ans = a * b - c * p;
  if (ans < 0)
    ans += p;
  if (ans >= p)
    ans -= p;
  return ans;
}
void solve() {
  // n=read()+1;m=read()+1;P=read();
  // for(int i=0;i<n;++i) a[i]=read();
  // for(int i=0;i<m;++i) b[i]=read();
  int lim = 1, ct = 0;
  while (lim <= n + m)
    lim <<= 1, ct++;
  for (int i = 0; i < lim; ++i)
    R[i] = (R[i >> 1] >> 1) | ((i & 1) << (ct - 1));
  for (int cur = 0; cur < 3; ++cur) {
    MOD = MODs[cur];
    memset(f, 0, sizeof(f));
    memset(g, 0, sizeof(g));
    for (int i = 0; i < n; ++i)
      f[i] = a[i];
    for (int i = 0; i < m; ++i)
      g[i] = b[i];
    NTT(f, lim, 1);
    NTT(g, lim, 1);
    for (int i = 0; i < lim; ++i)
      f[i] = (ll)f[i] * g[i] % MOD;
    NTT(f, lim, -1);
    int invlim = pow_mod(lim, MOD - 2);
    for (int i = 0; i < lim; ++i)
      r[cur][i] = (ll)f[i] * invlim % MOD;
  }
  const ll INV = mul_mod(pow_mod(MODs[0], MODs[2] - 2),
                         pow_mod(MODs[1], MODs[2] - 2), MODs[2]);
  for (int i = 0; i < lim; ++i) {
    ll M = MODs[0] * MODs[1];
    ll m0 = MODs[0], m1 = MODs[1], m2 = MODs[2];
    ll a = m0, b = m1, x, y;
    ll c = (r[1][i] - r[0][i] % m1 + m1) % m1;
    ll d = exgcd(a, b, x, y);
    x = mul_mod(x, c, b);
    ans[i] = (r[0][i] + mul_mod(x, m0, M)) % M;
    ll t = ((r[2][i] - ans[i]) % MODs[2] + MODs[2]) % MODs[2];
    t = mul_mod(t, INV, MODs[2]);
    ans[i] = (mul_mod(t, M, P) + ans[i]) % P;
  }
}
} // namespace NTT
int main() {
  n = read();
  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = (ll)fac[i - 1] * i % MOD;
  invf[n] = pow_mod(fac[n], MOD - 2);
  for (int i = n - 1; i >= 0; --i)
    invf[i] = (ll)invf[i + 1] * (i + 1) % MOD;
  //	int ans=0;
  //	for(int i=1;i<=n;++i){
  //		int w=read();
  //		for(int j=1;j<=n;++j){
  //			for(int k=1;k<=n-abs(i-j);++k){
  //				ans=mod(ans+(ll)w*comb(n-abs(i-j)-1,k-1)%MOD*fac[k-1]%MOD*fac[n-k]%MOD);
  //			}
  //		}
  //	}
  NTT::P = MOD;
  NTT::n = n + 1;
  NTT::m = n + 1;
  for (int i = 1; i <= n; ++i)
    NTT::a[i] = fac[n - i];
  for (int i = 0; i <= n; ++i)
    NTT::b[i] = invf[i];
  NTT::solve();
  for (int i = 1; i <= n; ++i)
    g[i] = NTT::ans[i];
  //	for(int i=1;i<=n;++i){
  //		for(int k=1;k<=i;++k)g[i]=mod(g[i]+(ll)fac[n-k]*invf[i-k]%MOD);
  //	}
  for (int i = 0; i < n; ++i)
    f[i] = mod((ll)g[n - i] * fac[n - i - 1] % MOD + f[i - 1]);
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    int w = read();
    ans = mod(ans +
              (ll)w *
                  mod(mod(f[min(i - 1, n - i)] + f[max(i - 1, n - i)]) - f[0]) %
                  MOD);
  }
  cout << ans << endl;
  return 0;
}