Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (ll i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
mint fact(int n) {
  if (n == 1 || n == 0)
    return 1;
  mint res = n;
  return res * fact(n - 1);
}

int main() {
  ll n;
  string x;
  cin >> n >> x;
  ll k = 0; /// popcount
  rep(i, n) if (x[i] == '1') k++;
  mod = k + 1;
  vector<mint> m(2);
  rep(a, 2) {
    rep(i, n) {
      m[a] *= 2;
      if (x[i] == '1')
        m[a] += 1;
    }
    mod = k - 1;
    if (k - 1 == 0)
      break;
  }
  rep(i, n) {
    ll y;
    if (x[i] == '1') {
      if (k - 1 == 0) {
        cout << 0 << '\\n';
        continue;
      }
      mod = k - 1;
      mint now = m[1];
      now -= mint(2).pow(n - 1 - i);
      y = now.x;
    } else {
      mod = k + 1;
      mint now = m[0];
      now += mint(2).pow(n - 1 - i);
      y = now.x;
    }
    ll ans = 1;
    while (y > 0) {
      ans++;
      ll cnt = 0;
      rep(j, 32) if (y >> j & 1) cnt++;
      y %= cnt;
    }
    cout << ans << '\\n';
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (ll i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
mint fact(int n) {
  if (n == 1 || n == 0)
    return 1;
  mint res = n;
  return res * fact(n - 1);
}

int main() {
  ll n;
  string x;
  cin >> n >> x;
  ll k = 0; /// popcount
  rep(i, n) if (x[i] == '1') k++;
  mod = k + 1;
  vector<mint> m(2);
  rep(a, 2) {
    rep(i, n) {
      m[a] *= 2;
      if (x[i] == '1')
        m[a] += 1;
    }
    mod = k - 1;
  }
  rep(i, n) {
    ll y;
    if (x[i] == '1') {
      mod = k - 1;
      mint now = m[1];
      now -= mint(2).pow(n - 1 - i);
      y = now.x;
    } else {
      mod = k + 1;
      mint now = m[0];
      now += mint(2).pow(n - 1 - i);
      y = now.x;
    }
    ll ans = 1;
    while (y > 0) {
      ans++;
      ll cnt = 0;
      rep(j, 32) if (y >> j & 1) cnt++;
      y %= cnt;
    }
    cout << ans << '\\n';
  }
}