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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
using namespace std;
const int INF = 1001001001;
const long long LINF = 1001002003004005006ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int mod = 0;
  rep(i, n) if (x[i] == '1') mod++;
  ll mod1[n];
  ll modm1[n];
  mod1[n - 1] = (1 % (mod + 1));
  if (mod - 1 > 0)
    modm1[n - 1] = (1 % (mod - 1));
  for (int i = n - 1; i > 0; i--) {
    mod1[i - 1] = (mod1[i] * 2) % (mod + 1);
    if (mod - 1 > 0)
      modm1[i - 1] = (modm1[i] * 2) % (mod - 1);
  }
  int f[mod + 5];
  f[0] = 0;
  rep(i, mod + 2) {
    int t = (i + 1) % __builtin_popcount(i + 1);
    f[i + 1] = f[t] + 1;
  }
  ll sum1 = 0;
  ll summ1 = 0;
  rep(i, n) {
    if (x[i] == '1') {
      sum1 += mod1[i];
      sum1 %= mod + 1;
    }
  }
  rep(i, n) {
    if (x[i] == '1') {
      summ1 += modm1[i];
      if (mod - 1 > 0)
        summ1 %= mod - 1;
    }
  }
  // sum1%=(mod+1);
  // summ1%=(mod-1);
  rep(i, n) {
    ll res;
    if (x[i] == '1') {
      if (mod == 1) {
        cout << 0 << endl;
        continue;
      }
      res = summ1;
      res -= modm1[i];
      if (res < 0)
        res += (mod - 1);
      cout << f[res] + 1 << endl;
    } else {
      res = sum1;
      res += mod1[i];
      res %= mod + 1;
      cout << f[res] + 1 << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
using namespace std;
const int INF = 1001001001;
const long long LINF = 1001002003004005006ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int mod = 0;
  rep(i, n) if (x[i] == '1') mod++;
  int mod1[n];
  int modm1[n];
  mod1[n - 1] = (1 % (mod + 1));
  if (mod - 1 > 0)
    modm1[n - 1] = (1 % (mod - 1));
  for (int i = n - 1; i > 0; i--) {
    mod1[i - 1] = (mod1[i] * 2) % (mod + 1);
    if (mod - 1 > 0)
      modm1[i - 1] = (modm1[i] * 2) % (mod - 1);
  }
  int f[mod + 5];
  f[0] = 0;
  rep(i, mod + 2) {
    int t = (i + 1) % __builtin_popcount(i + 1);
    f[i + 1] = f[t] + 1;
  }
  ll sum1 = 0;
  ll summ1 = 0;
  rep(i, n) {
    if (x[i] == '1') {
      sum1 += mod1[i];
      sum1 %= mod + 1;
    }
  }
  rep(i, n) {
    if (x[i] == '1') {
      summ1 += modm1[i];
      summ1 %= mod - 1;
    }
  }
  // sum1%=(mod+1);
  // summ1%=(mod-1);
  rep(i, n) {
    ll res;
    if (x[i] == '1') {
      if (mod == 1) {
        cout << 0 << endl;
        continue;
      }
      res = summ1;
      res -= modm1[i];
      if (res < 0)
        res += (mod - 1);
      // cout << res << endl;
      cout << f[res] + 1 << endl;
    } else {
      res = sum1;
      res += mod1[i];
      res %= mod + 1;
      cout << f[res] + 1 << endl;
    }
  }
}