Problem p02950 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int N = 2 * 1000 + 17;
const int MOD = 1000 * 1000 * 1000 + 7;

struct poly {
  int P;
  vector<long long> a;
  poly(int P) : P(P), a(vector<long long>(P, 0ll)) {}

  int len() const {
    int res = 0;

    for (int i = 0; i < P; ++i)
      if (a[i] != 0)
        res = i + 1;

    return res;
  }

  long long &operator[](int i) { return a[i]; }
  const long long operator[](int i) const { return a[i]; }
};

poly operator*(const poly &a, const poly &b) {
  auto P = a.P;
  poly res(P);
  auto alen = a.len();
  auto blen = b.len();

  for (int i = 0; i < alen; ++i)
    for (int j = 0; j < blen; ++j)
      res[(i + j) % (P - 1)] += a[i] * b[j];

  for (int i = 0; i < P; ++i)
    if (res[i] >= P)
      res[i] %= P;

  return move(res);
}

int n;
int a[N];
vector<int> vec[2];

poly binpow(poly f, int n) {
  auto res = poly(f.P);
  res[0] = 1;

  for (; n > 0; n /= 2, f = f * f)
    if (n & 1)
      res = res * f;

  return move(res);
}

bool read() {
  if (!(cin >> n))
    return false;

  for (int i = 0; i < n; ++i)
    cin >> a[i];

  return true;
}

void solve() {
  for (int i = 1; i < n; ++i)
    vec[a[i] ^ a[0]].emplace_back(i);

  vector<poly> f(2, poly(n));

  for (int k = 0; k < 2; ++k) {
    f[k][0] = 1;

    for (auto x : vec[k]) {
      poly cur(n);
      cur[0] = (n - x) % n;
      cur[1] = 1;
      f[k] = f[k] * cur;
    }
  }

  ++f[1][0];
  auto res = binpow(f[0], n - 1) * f[1];
  res[n - 1] = res[0];
  res[0] = a[0];

  for (int i = 0; i < n; ++i)
    cout << (i > 0 && a[0] ? (n - res[i]) % n : res[i]) << ' ';

  cout << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  while (read())
    solve();

  return 0;
}


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

using namespace std;

const int N = 200 * 1000 + 17;
const int MOD = 1000 * 1000 * 1000 + 7;

struct poly {
  int P;
  vector<long long> a;
  poly(int P) : P(P), a(vector<long long>(P, 0ll)) {}

  int len() const {
    int res = 0;

    for (int i = 0; i < P; ++i)
      if (a[i] != 0)
        res = i + 1;

    return res;
  }

  long long &operator[](int i) { return a[i]; }
  const long long operator[](int i) const { return a[i]; }
};

poly operator*(const poly &a, const poly &b) {
  auto P = a.P;
  poly res(P);
  auto alen = a.len();
  auto blen = b.len();

  for (int i = 0; i < alen; ++i)
    for (int j = 0; j < blen; ++j)
      res[(i + j) % (P - 1)] += a[i] * b[j];

  for (int i = 0; i < P; ++i)
    if (res[i] >= P)
      res[i] %= P;

  return move(res);
}

int n;
int a[N];
vector<int> vec[2];

poly binpow(poly f, int n) {
  auto res = poly(f.P);
  res[0] = 1;

  for (; n > 0; n /= 2, f = f * f)
    if (n & 1)
      res = res * f;

  return move(res);
}

bool read() {
  if (!(cin >> n))
    return false;

  for (int i = 0; i < n; ++i)
    cin >> a[i];

  return true;
}

void solve() {
  for (int i = 1; i < n; ++i)
    vec[a[i] ^ a[0]].emplace_back(i);

  vector<poly> f(2, poly(n));

  for (int k = 0; k < 2; ++k) {
    f[k][0] = 1;

    for (auto x : vec[k]) {
      poly cur(n);
      cur[0] = (n - x) % n;
      cur[1] = 1;
      f[k] = f[k] * cur;
    }
  }

  ++f[1][0];
  auto res = binpow(f[0], n - 1) * f[1];
  res[n - 1] = res[0];
  res[0] = a[0];

  for (int i = 0; i < n; ++i)
    cout << (i > 0 && a[0] ? (n - res[i]) % n : res[i]) << ' ';

  cout << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  while (read())
    solve();

  return 0;
}