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

/*
        karmany evadhikaras te
        ma phalesu kadacana
        ma karma-phala-hetur bhur
        ma re sango'stv akarmani (B.G.-2.47)

Translation:
        Lord Krsna to Arjuna.
You have a right to perform your prescribed duty,
but you are not entitled to the fruits of action.
Never consider yourself the cause of the results of your activities,
and never be attached to not doing your duty.
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 200005;
#define ld long double
#define PI 3.14159265358979323846
const int X = 500000;

void flash() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(12);
}

// 3. Structure or classes
// 3. Ends here

// 1. Data structures declaration here
// 1. Ends here

// 2. Extra Functions here
int get(int x) {
  if (x == 0)
    return 0;
  int n = __builtin_popcount(x);
  return 1 + get(x % n);
}
// 2. Ends here

void solve();
int32_t main() {
  flash();
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt1 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt1++;
  }
  reverse(s.begin(), s.end());
  int mod = (cnt1 + 1);
  if (cnt1 > 1)
    mod *= (cnt1 - 1);
  int po2[N];
  po2[0] = 1;
  for (int i = 1; i < n; i++) {
    po2[i] = (po2[i - 1] * 2) % mod;
  }
  int num = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      num = (num + po2[i]) % mod;
  }
  // reverse(s.begin(),s.end());
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      if (cnt1 == 1)
        cout << "0\\n";
      else {
        int numd = (num - po2[i] + mod) % (cnt1 - 1);
        cout << 1 + get(numd) << "\\n";
      }
    } else {
      int numd = (num + po2[i] + mod) % (cnt1 + 1);
      cout << 1 + get(numd) << "\\n";
    }
  }
  return;
}
/*
Thanks to
hitman623
*/

Error: -11

Fixed submission:

/*
        karmany evadhikaras te
        ma phalesu kadacana
        ma karma-phala-hetur bhur
        ma re sango'stv akarmani (B.G.-2.47)

Translation:
        Lord Krsna to Arjuna.
You have a right to perform your prescribed duty,
but you are not entitled to the fruits of action.
Never consider yourself the cause of the results of your activities,
and never be attached to not doing your duty.
*/
#include <bits/stdc++.h>

using namespace std;
#define ll long long
const ll mod = 2000000011;
#define ld long double
#define PI 3.14159265358979323846
const int INF = 1000000000;
const int N = 50000 + 5;

void flash() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(4);
}

// 3. Structure or classes
// 3. Ends here

// 1. Data structures declaration here
map<int, bool> diag1;
map<int, bool> diag2;
map<int, bool> mx;
int cnt = 0;
int tx, ty;
int a[9];
// 1. Ends here

// 2. Extra Functions here
void startGraphing(int c, int count) {
  // cout<<"start--"<<c<<"\\n";
  if (count == 8) {
    cout << setw(2) << (++cnt) << "      ";
    for (int i = 0; i < 8; i++)
      cout << a[i] + 1 << " ";
    cout << "\\n";
    return;
  } else if (c == ty) {
    startGraphing(c + 1, count + 1);
  } else {
    for (int x = 0; x < 8; x++) {
      int d1 = x + c;
      int d2 = c - x + 7;
      if (!mx[x] && !diag1[d1] && !diag2[d2]) {
        diag1[d1] = 1;
        diag2[d2] = 1;
        mx[x] = 1;
        a[c] = x;
        startGraphing(c + 1, count + 1);
        diag1[d1] = 0;
        diag2[d2] = 0;
        mx[x] = 0;
      }
    }
  }
}
// 2. Ends here

void solve(int x, int y);
int32_t main() {
  flash();
  int t;
  t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << "SOLN       COLUMN\\n";
    cout << setw(2) << " #      1 2 3 4 5 6 7 8\\n\\n";
    for (int j = 0; j < N; j++) {
      mx[j] = false;
      a[j] = -1;
    }
    for (int j = 0; j < 15; j++)
      diag1[j] = diag2[j] = false;
    solve(x, y);
  }
  return 0;
}

void solve(int x, int y) {
  tx = x;
  ty = y;
  diag1[x + y] = 1;
  diag2[y - x + 7] = 1;
  mx[x] = 1;
  // int a[9];
  a[y] = x;
  startGraphing(0, 0);
  cout << cnt;
}