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

/*-In the name of God-

Author : Syed Mujtaba
Seek knowledge from the Cradle to the Grave !!
https://www.linkedin.com/in/syed-mujtaba
*/

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define ll long long
#define pb push_back
#define fi first
#define se second
#define MAXN 200064
// #define mod             1000000007  //998244353 // 1e9 + 7
#define MAX 8000000000000000064LL
#define MIN -8000000000000000064LL
#define mt make_tuple
#define eps 1e-6
#define endl '\\n'
#define test                                                                   \\
  int T_T;                                                                     \\
  cin >> T_T;                                                                  \\
  while (T_T--)
#define fast_io                                                                \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
int mod;
int xyp(int x, int y) {
  int res = 1;
  while (y > 0) {
    if (y % 2 == 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    y /= 2;
  }
  return res % mod;
}

signed main() {
  fast_io int n;
  cin >> n;
  string s;
  cin >> s;
  int u = 0;
  for (auto z : s) {
    if (z == '1') {
      u++;
    }
  }
  mod = u + 1;
  int mo1, mo2;
  // int x[100] = {'\\0'};
  int mo = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      mo = (mo + xyp(2, (n - 1 - i))) % mod;
  }
  mo1 = mo;
  mod = u - 1;
  mo = 0;
  // cout << mod << "!!\\n";
  if (mod != 0)
    for (int i = 0; i < n; i++) {
      if (s[i] == '1')
        mo = (mo + xyp(2, (n - 1 - i))) % mod;
      mo2 = mo;
    }
  else
    mo2 = 0;
  // cout << mo2 << "!!\\n";

  for (int i = 0; i < n; i++) {
    int bits = u;
    int mo;
    if (s[i] == '1') {
      bits--;
      mo = mo2;
      if (bits == 0) {
        cout << 0 << endl;
      } else {
        mod = bits;
        mo = (mo + bits + bits - xyp(2, (n - 1 - i))) % bits;

        int ac = 1;
        while (mo > 0) {
          mo %= __builtin_popcountll(mo);
          ac++;
        }
        cout << ac << endl;
      }

    } else {
      bits++;
      mo = mo1;
      mod = bits;
      mo = (mo + xyp(2, n - i - 1)) % bits;
      int ac = 1;
      while (mo > 0) {
        mo %= __builtin_popcountll(mo);
        ac++;
      }
      cout << ac << endl;
    }
  }
}


Error: 0

Fixed submission:

/*-In the name of God-

Author : Syed Mujtaba
Seek knowledge from the Cradle to the Grave !!
https://www.linkedin.com/in/syed-mujtaba
*/

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define ll long long
#define pb push_back
#define fi first
#define se second
#define MAXN 200064
// #define mod             1000000007  //998244353 // 1e9 + 7
#define MAX 8000000000000000064LL
#define MIN -8000000000000000064LL
#define mt make_tuple
#define eps 1e-6
#define endl '\\n'
#define test                                                                   \\
  int T_T;                                                                     \\
  cin >> T_T;                                                                  \\
  while (T_T--)
#define fast_io                                                                \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
int mod;
int xyp(int x, int y) {
  int res = 1;
  while (y > 0) {
    if (y % 2 == 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    y /= 2;
  }
  return res % mod;
}

signed main() {
  fast_io int n;
  cin >> n;
  string s;
  cin >> s;
  int u = 0;
  for (auto z : s) {
    if (z == '1') {
      u++;
    }
  }
  mod = u;
  int x[100] = {'\\0'};
  for (mod = 1; mod <= 70; mod++) {
    int mo = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == '1')
        mo = (mo + xyp(2, (n - 1 - i))) % mod;
    }
    x[mod] = mo;
  }
  // cout << x[2] << endl;
  // cout << u << endl;
  for (int i = 0; i < n; i++) {
    int bits = u;
    int mo;
    if (s[i] == '1') {
      bits--;
      if (bits == 0) {
        cout << 0 << endl;
      } else {
        mo = x[bits];
        mod = bits;
        mo = (mo - xyp(2, (n - 1 - i)) + mod + mod) % mod;
        int ac = 1;
        while (mo > 0) {
          mo %= __builtin_popcountll(mo);
          ac++;
        }
        cout << ac << endl;
      }
    } else {
      bits++;
      int mo = x[bits];
      mod = bits;
      mo = (mo + xyp(2, (n - 1 - i))) % mod;
      int ac = 1;
      while (mo > 0) {
        mo %= __builtin_popcountll(mo);
        ac++;
      }
      cout << ac << endl;
    }
  }
}