Problem p02950 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
ll mod;
vector<ll> inv, fact, invfact;
void mod_build(int n = 101010) {
  fact.resize(n + 1);
  inv.resize(n + 1);
  invfact.resize(n + 1);
  fact[0] = inv[0] = invfact[0] = 1;
  inv[1] = 1;
  rep(i, n) {
    fact[i + 1] = fact[i] * (i + 1) % mod;
    if (i > 0)
      inv[i + 1] = mod - inv[mod % (i + 1)] * (mod / (i + 1)) % mod;
    invfact[i + 1] = invfact[i] * inv[i + 1] % mod;
  }
}
ll perm(int n, int k) {
  if (n < 0 || k < 0 || k > n)
    return 0;
  return fact[n] * invfact[n - k] % mod;
}
ll comb(int n, int k) {
  if (n < 0 || k < 0 || k > n)
    return 0;
  return (fact[n] * invfact[n - k] % mod) * invfact[k] % mod;
}
ll powmod(ll n, ll k) {
  k %= mod - 1;
  if (k < 0)
    k += mod - 1;
  ll ret = 1;
  while (k) {
    if (k & 1)
      ret = ret * n % mod;
    n = n * n % mod;
    k >>= 1;
  }
  return ret;
}

int main() {
  int n;
  cin >> n;
  mod = n;
  mod_build(n);
  vector<ll> ans(n);
  ll x;
  cin >> x;
  ans[n - 1] += n - x;
  ans[0] += x;
  REP(i, 1, n) {
    int x;
    cin >> x;
    ll res = inv[i];
    REP(j, 1, n) {
      if (i == j)
        continue;
      else
        res *= inv[(i - j + n) % n];
      res %= n;
    }
    rep(j, n - 1) ans[n - 1 - j] += powmod(i, j) * res * x % mod;
  }
  rep(i, n) ans[i] %= mod;
  rep(i, n) cout << ans[i] << " ";
  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
ll mod;
vector<ll> inv, fact, invfact;
void mod_build(int n = 101010) {
  fact.resize(n + 1);
  inv.resize(n + 1);
  invfact.resize(n + 1);
  fact[0] = inv[0] = invfact[0] = 1;
  inv[1] = 1;
  rep(i, n) {
    fact[i + 1] = fact[i] * (i + 1) % mod;
    if (i > 0)
      inv[i + 1] = mod - inv[mod % (i + 1)] * (mod / (i + 1)) % mod;
    invfact[i + 1] = invfact[i] * inv[i + 1] % mod;
  }
}
ll perm(int n, int k) {
  if (n < 0 || k < 0 || k > n)
    return 0;
  return fact[n] * invfact[n - k] % mod;
}
ll comb(int n, int k) {
  if (n < 0 || k < 0 || k > n)
    return 0;
  return (fact[n] * invfact[n - k] % mod) * invfact[k] % mod;
}
ll powmod(ll n, ll k) {
  k %= mod - 1;
  if (k < 0)
    k += mod - 1;
  ll ret = 1;
  while (k) {
    if (k & 1)
      ret = ret * n % mod;
    n = n * n % mod;
    k >>= 1;
  }
  return ret;
}

int main() {
  int n;
  cin >> n;
  mod = n;
  mod_build(n);
  vector<ll> ans(n);
  ll x;
  cin >> x;
  ans[n - 1] += n - x;
  ans[0] += x;
  REP(i, 1, n) {
    int x;
    cin >> x;
    ll res = inv[i];
    REP(j, 1, n) {
      if (i == j)
        continue;
      else
        res *= inv[(i - j + n) % n];
      res %= n;
    }
    ll p = 1;
    rep(j, n - 1) {
      ans[n - 1 - j] += p * res * x % mod;
      p = p * i % mod;
    }
  }
  rep(i, n) ans[i] %= mod;
  rep(i, n) cout << ans[i] << " ";
  cout << endl;
  return 0;
}