Problem p02950 (C++)

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

struct ModInt {
  using M = ModInt;
  long long MOD;
  long long v;
  ModInt(long long _v = 0, long long _MOD = 1e9 + 7)
      : MOD(_MOD), v(_v % MOD + MOD) {
    norm();
  }
  M &norm() {
    v = (v < MOD) ? v : v - MOD;
    return *this;
  }
  M operator+(const M &x) const { return M(v + x.v, MOD); }
  M operator-(const M &x) const { return M(v + MOD - x.v, MOD); }
  M operator*(const M &x) const { return M(v * x.v % MOD, MOD); }
  M operator/(const M &x) const { return M(v * x.inv().v, MOD); }
  M &operator+=(const M &x) { return *this = *this + x; }
  M &operator-=(const M &x) { return *this = *this - x; }
  M &operator*=(const M &x) { return *this = *this * x; }
  M &operator/=(const M &x) { return *this = *this / x; }
  friend istream &operator>>(istream &input, M &x) {
    return input >> x.v, x.norm(), input;
  }
  friend ostream &operator<<(ostream &output, const M &x) {
    return output << x.v;
  }
  M pow(long long n) const {
    M x(v, MOD), res(1, MOD);
    while (n) {
      if (n & 1)
        res *= x;
      x *= x;
      n >>= 1;
    }
    return res;
  }
  M inv() const { return this->pow(MOD - 2); }

  static vector<M> fact, finv;
  static void build(int n, long long p) {
    fact.assign(n + 1, M(1, p));
    finv.assign(n + 1, M(1, p));
    for (int i = 1; i < n + 1; i++)
      fact[i] = fact[i - 1] * M(i, p);
    for (int i = 0; i < n + 1; i++)
      finv[i] = fact[i].inv();
  }
  static M comb(int n, int k, long long p) {
    if (n < k || k < 0)
      return M(0, p);
    return fact[n] * finv[n - k] * finv[k];
  }
};
vector<ModInt> ModInt::fact = vector<ModInt>();
vector<ModInt> ModInt::finv = vector<ModInt>();
using mint = ModInt;

int main() {
  int p;
  cin >> p;
  vector<int> a(p);
  for (int i = 0; i < p; i++)
    cin >> a[i];
  mint::build(p, p);
  vector<mint> b(p, mint(0, p));
  for (int i = 0; i < p; i++) {
    if (a[i] == 0)
      continue;
    b[0] += mint(1, p);
    for (int j = 0; j < p; j++) {
      b[j] -= mint(-i, p).pow(p - 1 - j) * mint::comb(p - 1, j, p);
    }
  }
  for (auto x : b) {
    cout << x << ' ';
  }
  cout << endl;
}


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

struct ModInt {
  using M = ModInt;
  long long MOD;
  long long v;
  ModInt(long long _v = 0, long long _MOD = 1e9 + 7)
      : MOD(_MOD), v(_v % MOD + MOD) {
    norm();
  }
  M &norm() {
    v = (v < MOD) ? v : v - MOD;
    return *this;
  }
  M operator+(const M &x) const { return M(v + x.v, MOD); }
  M operator-(const M &x) const { return M(v + MOD - x.v, MOD); }
  M operator*(const M &x) const { return M(v * x.v % MOD, MOD); }
  M operator/(const M &x) const { return M(v * x.inv().v, MOD); }
  M &operator+=(const M &x) { return *this = *this + x; }
  M &operator-=(const M &x) { return *this = *this - x; }
  M &operator*=(const M &x) { return *this = *this * x; }
  M &operator/=(const M &x) { return *this = *this / x; }
  friend istream &operator>>(istream &input, M &x) {
    return input >> x.v, x.norm(), input;
  }
  friend ostream &operator<<(ostream &output, const M &x) {
    return output << x.v;
  }
  M pow(long long n) const {
    M x(v, MOD), res(1, MOD);
    while (n) {
      if (n & 1)
        res *= x;
      x *= x;
      n >>= 1;
    }
    return res;
  }
  M inv() const { return this->pow(MOD - 2); }

  static vector<M> fact, finv;
  static void build(int n, long long p) {
    fact.assign(n + 1, M(1, p));
    finv.assign(n + 1, M(1, p));
    for (int i = 1; i < n + 1; i++)
      fact[i] = fact[i - 1] * M(i, p);
    for (int i = 0; i < n + 1; i++)
      finv[i] = fact[i].inv();
  }
  static M comb(int n, int k, long long p) {
    if (n < k || k < 0)
      return M(0, p);
    return fact[n] * finv[n - k] * finv[k];
  }
};
vector<ModInt> ModInt::fact = vector<ModInt>();
vector<ModInt> ModInt::finv = vector<ModInt>();
using mint = ModInt;

int main() {
  int p;
  cin >> p;
  vector<int> a(p);
  for (int i = 0; i < p; i++)
    cin >> a[i];
  mint::build(p, p);
  vector<mint> b(p, mint(0, p));
  for (int i = 0; i < p; i++) {
    if (a[i] == 0)
      continue;
    b[0] += mint(1, p);
    mint pw = mint(1, p);
    for (int j = p - 1; j >= 0; j--) {
      b[j] -= pw * mint::comb(p - 1, j, p);
      pw *= mint(-i, p);
    }
  }
  for (auto x : b) {
    cout << x << ' ';
  }
  cout << endl;
}