Problem p03232 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define fst first
#define snd second
#define SZ(u) ((int)(u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline T read() {
  register T sum(0), fg(1);
  register char ch(getchar());
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fg = -1;
  for (; isdigit(ch); ch = getchar())
    sum = sum * 10 - '0' + ch;
  return sum * fg;
}

typedef long long LL;
typedef pair<int, int> pii;

const int MOD = (int)1e9 + 7;
const int MAXN = (int)1e5;

// Template-Libs

namespace MATH {
int inv[MAXN + 5];

inline void init() {
  inv[1] = 1;
  for (int i = 2; i <= MAXN; ++i)
    inv[i] = (LL)MOD / i * -inv[MOD % i] % MOD;
}
} // namespace MATH

using MATH::inv;

// END

int n;

int a[MAXN + 5];

inline void input() {
  n = read<int>();
  for (int i = 1; i <= n; ++i)
    a[i] = read<int>();
}

inline void solve() {
  static int prefix[MAXN + 5];
  prefix[0] = 0;
  for (int i = 1; i <= n; ++i)
    prefix[i] = (prefix[i - 1] + inv[i]) % MOD;

  int ans = 0;
  for (int i = 1; i <= n; ++i)
    (ans += (LL)(prefix[i] + prefix[n - i + 1] - 1) % MOD * a[i] % MOD) %= MOD;
  for (int i = 1; i <= n; ++i)
    ans = (LL)ans * i % MOD;
  printf("%d\n", (ans + MOD) % MOD);
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("std.in", "r", stdin);
  freopen("std.out", "w", stdout);
#endif

  MATH::init();
  input();
  solve();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define fst first
#define snd second
#define SZ(u) ((int)(u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline T read() {
  register T sum(0), fg(1);
  register char ch(getchar());
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fg = -1;
  for (; isdigit(ch); ch = getchar())
    sum = sum * 10 - '0' + ch;
  return sum * fg;
}

typedef long long LL;
typedef pair<int, int> pii;

const int MOD = (int)1e9 + 7;
const int MAXN = (int)1e5;

// Template-Libs

namespace MATH {
int inv[MAXN + 5];

inline void init() {
  inv[1] = 1;
  for (int i = 2; i <= MAXN; ++i)
    inv[i] = (LL)MOD / i * -inv[MOD % i] % MOD;
}
} // namespace MATH

using MATH::inv;

// END

int n;

int a[MAXN + 5];

inline void input() {
  n = read<int>();
  for (int i = 1; i <= n; ++i)
    a[i] = read<int>();
}

inline void solve() {
  static int prefix[MAXN + 5];
  prefix[0] = 0;
  for (int i = 1; i <= n; ++i)
    prefix[i] = (prefix[i - 1] + inv[i]) % MOD;

  int ans = 0;
  for (int i = 1; i <= n; ++i)
    (ans += (LL)(prefix[i] + prefix[n - i + 1] - 1) % MOD * a[i] % MOD) %= MOD;
  for (int i = 1; i <= n; ++i)
    ans = (LL)ans * i % MOD;
  printf("%d\n", (ans + MOD) % MOD);
}

int main() {
  MATH::init();
  input();
  solve();

  return 0;
}