Problem p02609 (C++)

Buggy submission (status: Runtime Error):
// In the name of God //

#include <bits/stdc++.h>
#define ll long long
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
using namespace std;
const ll mod = 998244353;

int power(ll x, ll y, ll p) {
  if (y == 0) {
    return 1 % p;
  }
  ll res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
             // equal to p

  if (x == 0)
    return 0; // In case x is divisible by p;

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

void solve() {
  int n;
  cin >> n;
  string t;
  cin >> t;
  vector<int> a(n);
  int p = 0;
  for (int i = 0; i < n; i++) {
    a[i] = t[i] - '0';
    if (a[i])
      p++;
  }
  if (p == 0) {
    for (int i = 0; i < n; i++) {
      cout << 1 << "\n";
    }
    return;
  }
  // if(p == 1) {
  //     for (int i = 0; i < n; i++) {
  //         if(a[i]) {
  //             cout << 0 << "\n";
  //         } else {

  //         }
  //     }
  //     return;
  // }
  // cout << p << "\n";
  // cout << power(2, 0, 2);
  ll s1 = 0, s2 = 0;
  vector<ll> m1(n), m2(n);
  for (int i = 0; i < n; i++) {
    if (p > 1)
      m1[i] = power(2, n - 1 - i, p - 1);
    m2[i] = power(2, n - 1 - i, p + 1);
    // cout << m1[i] << " " << m2[i] << "\n";
    if (a[i]) {
      s1 = ((s1 % (p - 1)) + (m1[i] % (p - 1)) % (p - 1));
      s2 = ((s2 % (p + 1)) + (m2[i] % (p + 1)) % (p + 1));
    }
  }
  // cout << s1 << " " << s2 << "\n";
  vector<int> ans(n, 1);
  for (int i = 0; i < n; i++) {
    if (a[i]) {
      if (p == 1) {
        ans[i] = 0;
        continue;
      }
      int x = (s1 - m1[i] + p - 1) % (p - 1);
      // cout << x << " ";
      while (x) {
        // cout << x << " ";
        x %= __builtin_popcount(x);
        ans[i]++;
      }
    } else {
      int x = (s2 + m2[i] + p + 1) % (p + 1);
      // cout << x << " ";
      while (x) {
        // cout << x << " ";
        x %= __builtin_popcount(x);
        ans[i]++;
      }
    }
    // cout << "\n";
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i] << "\n";
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tt = 1;
  // cin >> tt;
  for (int tc = 1; tc <= tt; ++tc) {
    solve();
  }
  // cout << 15*modInverse(6, mod)%mod << " " << 39*modInverse(10, mod)%mod;
}

Fixed submission:
// In the name of God //

#include <bits/stdc++.h>
#define ll long long
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
using namespace std;
const ll mod = 998244353;

int power(ll x, ll y, ll p) {
  if (y == 0) {
    return 1 % p;
  }
  ll res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
             // equal to p

  if (x == 0)
    return 0; // In case x is divisible by p;

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

void solve() {
  int n;
  cin >> n;
  string t;
  cin >> t;
  vector<int> a(n);
  int p = 0;
  for (int i = 0; i < n; i++) {
    a[i] = t[i] - '0';
    if (a[i])
      p++;
  }
  if (p == 0) {
    for (int i = 0; i < n; i++) {
      cout << 1 << "\n";
    }
    return;
  }
  // if(p == 1) {
  //     for (int i = 0; i < n; i++) {
  //         if(a[i]) {
  //             cout << 0 << "\n";
  //         } else {

  //         }
  //     }
  //     return;
  // }
  // cout << p << "\n";
  // cout << power(2, 0, 2);
  ll s1 = 0, s2 = 0;
  vector<ll> m1(n), m2(n);
  for (int i = 0; i < n; i++) {
    if (p > 1)
      m1[i] = power(2, n - 1 - i, p - 1);
    m2[i] = power(2, n - 1 - i, p + 1);
    // cout << m1[i] << " " << m2[i] << "\n";
    if (a[i]) {
      if (p > 1)
        s1 = ((s1 % (p - 1)) + (m1[i] % (p - 1)) % (p - 1));
      s2 = ((s2 % (p + 1)) + (m2[i] % (p + 1)) % (p + 1));
    }
  }
  // cout << s1 << " " << s2 << "\n";
  vector<int> ans(n, 1);
  for (int i = 0; i < n; i++) {
    if (a[i]) {
      if (p == 1) {
        ans[i] = 0;
        continue;
      }
      int x = (s1 - m1[i] + p - 1) % (p - 1);
      // cout << x << " ";
      while (x) {
        // cout << x << " ";
        x %= __builtin_popcount(x);
        ans[i]++;
      }
    } else {
      int x = (s2 + m2[i] + p + 1) % (p + 1);
      // cout << x << " ";
      while (x) {
        // cout << x << " ";
        x %= __builtin_popcount(x);
        ans[i]++;
      }
    }
    // cout << "\n";
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i] << "\n";
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tt = 1;
  // cin >> tt;
  for (int tc = 1; tc <= tt; ++tc) {
    solve();
  }
  // cout << 15*modInverse(6, mod)%mod << " " << 39*modInverse(10, mod)%mod;
}