Problem p02950 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;

long long MOD;
struct mint {

  long long v;

  mint() : v(0) {}
  mint(long long x) {
    x = x % MOD;
    if (x < 0) {
      x += MOD;
    }
    v = x;
  }
  mint &operator+=(mint a) {
    v += a.v;
    if (v >= MOD) {
      v -= MOD;
    }
    return *this;
  }
  mint &operator*=(mint a) {
    v *= a.v;
    v %= MOD;
    return *this;
  }
  mint &operator-=(mint a) {
    v -= a.v;
    if (v < 0) {
      v += MOD;
    }
    return *this;
  }
  mint &operator/=(mint a) { return (*this) *= a.inv(); }

  mint operator*(mint a) const { return mint(v) *= a; }

  mint operator+(mint a) const { return mint(v) += a; }

  mint operator-(mint a) const { return mint(v) -= a; }

  mint operator/(mint a) const { return mint(v) /= a; }

  mint pow(long long k) const {
    mint res(1), tmp(v);
    while (k) {
      if (k & 1)
        res *= tmp;
      tmp *= tmp;
      k >>= 1;
    }
    return res;
  }
  mint inv() { return pow(MOD - 2); }

  static mint comb(long long n, int k) {
    mint res(1);
    for (int i = 0; i < k; ++i) {
      res *= mint(n - i);
      res /= mint(i + 1);
    }
    return res;
  }

  static mint factorial(long long n) {
    mint res(1);
    for (int i = n; i > 1; --i) {
      res *= mint(i);
    }
    return res;
  }

  bool operator<(const mint &a) const { return v < a.v; };
};

mint operator*(long long l, mint r) { return r * l; }

void calc_factorials(long long n, vector<mint> &out) {
  out.resize(n + 1);
  out.at(0) = 1;
  out.at(1) = 1;
  for (long long i = 2; i <= n; ++i) {
    out.at(i) = out.at(i - 1) * i;
  }
}

int main() {
  cin >> MOD;
  vector<int> a(MOD);
  for (int i = 0; i < MOD; ++i) {
    cin >> a.at(i);
  }

  vector<mint> facts, invs;
  calc_factorials(MOD - 1, facts);
  invs.resize(facts.size());
  for (int i = 0; i < facts.size(); ++i) {
    invs.at(i) = facts.at(i).inv();
  }

  vector<mint> b(MOD);
  for (int i = 0; i < MOD; ++i) {
    if (a.at(i) == 0) {
      continue;
    }
    b.at(0) += 1;
    for (int k = 0; k < MOD; ++k) {
      mint tmp = mint(i).pow(MOD - 1 - k);
      tmp *= facts.at(MOD - 1);
      tmp *= invs.at(k);
      tmp *= invs.at(MOD - 1 - k);
      b.at(k) -= mint(-i).pow(MOD - 1 - k) * facts.at(MOD - 1) * invs.at(k) *
                 invs.at(MOD - 1 - k);
    }
  }

  for (int i = 0; i < b.size(); ++i) {
    cout << b.at(i).v;
    if (i != b.size() - 1) {
      cout << ' ';
    }
  }
  cout << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;

long long MOD;
struct mint {

  long long v;

  mint() : v(0) {}
  mint(long long x) {
    x = x % MOD;
    if (x < 0) {
      x += MOD;
    }
    v = x;
  }
  mint &operator+=(mint a) {
    v += a.v;
    if (v >= MOD) {
      v -= MOD;
    }
    return *this;
  }
  mint &operator*=(mint a) {
    v *= a.v;
    v %= MOD;
    return *this;
  }
  mint &operator-=(mint a) {
    v -= a.v;
    if (v < 0) {
      v += MOD;
    }
    return *this;
  }
  mint &operator/=(mint a) { return (*this) *= a.inv(); }

  mint operator*(mint a) const { return mint(v) *= a; }

  mint operator+(mint a) const { return mint(v) += a; }

  mint operator-(mint a) const { return mint(v) -= a; }

  mint operator/(mint a) const { return mint(v) /= a; }

  mint pow(long long k) const {
    mint res(1), tmp(v);
    while (k) {
      if (k & 1)
        res *= tmp;
      tmp *= tmp;
      k >>= 1;
    }
    return res;
  }
  mint inv() { return pow(MOD - 2); }

  static mint comb(long long n, int k) {
    mint res(1);
    for (int i = 0; i < k; ++i) {
      res *= mint(n - i);
      res /= mint(i + 1);
    }
    return res;
  }

  static mint factorial(long long n) {
    mint res(1);
    for (int i = n; i > 1; --i) {
      res *= mint(i);
    }
    return res;
  }

  bool operator<(const mint &a) const { return v < a.v; };
};

mint operator*(long long l, mint r) { return r * l; }

void calc_factorials(long long n, vector<mint> &out) {
  out.resize(n + 1);
  out.at(0) = 1;
  out.at(1) = 1;
  for (long long i = 2; i <= n; ++i) {
    out.at(i) = out.at(i - 1) * i;
  }
}

int main() {
  cin >> MOD;
  vector<int> a(MOD);
  for (int i = 0; i < MOD; ++i) {
    cin >> a.at(i);
  }

  vector<mint> facts, invs;
  calc_factorials(MOD - 1, facts);
  invs.resize(facts.size());
  for (int i = 0; i < facts.size(); ++i) {
    invs.at(i) = facts.at(i).inv();
  }

  vector<mint> b(MOD);
  for (int i = 0; i < MOD; ++i) {
    if (a.at(i) == 0) {
      continue;
    }
    b.at(0) += 1;
    mint tmp = 1;
    for (int k = MOD - 1; k >= 0; --k) {
      b.at(k) -= tmp * facts.at(MOD - 1) * invs.at(k) * invs.at(MOD - 1 - k);
      tmp *= -i;
    }
  }

  for (int i = 0; i < b.size(); ++i) {
    cout << b.at(i).v;
    if (i != b.size() - 1) {
      cout << ' ';
    }
  }
  cout << endl;
  return 0;
}