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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define deg(x) cout << #x << "=" << x << endl;
#define fd(x) for (int i = 0; i < x; i++)
#define fdd(a, x) for (int i = a; i < x; i++)
#define fx(x, n) for (int i = n - 1; i >= x; i--)
#define mst(x) memset(x, 0, sizeof(x));
#define fi first
#define se second
#define TB ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ok "ok"
#define HH puts("");
#define ls rt << 1
#define rs rt << 1 | 1
#define pb(a, b) a.push_back(b);
#define FIN freopen("1.in", "r", stdin);
#define FOUT freopen("1.out", "w", stdout);
using namespace std;
typedef long long ll;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, int> pss;
typedef unsigned long long ull;
const ll mod = 1e9 + 7;
const ll inf = 1e9 + 7;
const long double eps = 1e-8;
ll qpow(ll a, ll b, ll m) {
  ll r = 1;
  a %= m;
  for (; b; b >>= 1) {
    if (b & 1)
      r = r * a % m;
    a = a * a % m;
  }
  return r;
}
const double pi = acos(-1);
//__builtin_popcount
string s;
ll f1[203040], f2[203040];

int fun(int q) {
  if (q == 0)
    return 0;
  return 1 + fun(q % __builtin_popcount(q));
}

int main() {
  TB int n;
  cin >> n;
  cin >> s;
  ll m = count(s.begin(), s.end(), '1'), sum1 = 0, sum2 = 0, flag1 = 1,
     flag2 = 1;
  ll m1 = m - 1;
  ll m2 = m + 1;
  ll flog = 0;
  if (m == 1) {
    m1++;
    flog = 1; // deg(ok)
  }
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      sum1 += flag1;
      sum2 += flag2;
      sum1 %= m1;
      sum2 %= m2;
    }
    f1[i] = flag1;
    f2[i] = flag2;
    flag1 *= 2;
    flag2 *= 2;
    flag1 %= m1;
    flag2 %= m2;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (flog) {
        cout << 0 << '\\n';
        continue;
      }
      ll ans = 1, mo = (sum1 - f1[i] + m1) % m1;
      ans += fun(mo);
      cout << ans << '\\n';
    } else {
      ll ans = 1, mo = (sum2 + f2[i] + m2) % m2;
      ans += fun(mo);
      cout << ans << '\\n';
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define deg(x) cout << #x << "=" << x << endl;
#define fd(x) for (int i = 0; i < x; i++)
#define fdd(a, x) for (int i = a; i < x; i++)
#define fx(x, n) for (int i = n - 1; i >= x; i--)
#define mst(x) memset(x, 0, sizeof(x));
#define fi first
#define se second
#define TB ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ok "ok"
#define HH puts("");
#define ls rt << 1
#define rs rt << 1 | 1
#define pb(a, b) a.push_back(b);
#define FIN freopen("1.in", "r", stdin);
#define FOUT freopen("1.out", "w", stdout);
using namespace std;
typedef long long ll;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, int> pss;
typedef unsigned long long ull;
const ll mod = 1e9 + 7;
const ll inf = 1e9 + 7;
const long double eps = 1e-8;
ll qpow(ll a, ll b, ll m) {
  ll r = 1;
  a %= m;
  for (; b; b >>= 1) {
    if (b & 1)
      r = r * a % m;
    a = a * a % m;
  }
  return r;
}
const double pi = acos(-1);
//__builtin_popcount
string s;
ll f1[203040], f2[203040];

int fun(int q) {
  if (q == 0)
    return 0;
  return 1 + fun(q % __builtin_popcount(q));
}

int main() {
  TB int n;
  cin >> n;
  cin >> s;
  ll m = count(s.begin(), s.end(), '1'), sum1 = 0, sum2 = 0, flag1 = 1,
     flag2 = 1;
  ll m1 = m - 1;
  ll m2 = m + 1;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      sum1 += flag1;
      sum2 += flag2;
      sum1 %= m1;
      sum2 %= m2;
    }
    f1[i] = flag1;
    f2[i] = flag2;
    flag1 *= 2;
    flag2 *= 2;
    flag1 %= m1;
    flag2 %= m2;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      ll ans = 1, mo = (sum1 - f1[i] + m1) % m1;
      ans += fun(mo);
      cout << ans << '\\n';
    } else {
      ll ans = 1, mo = (sum2 + f2[i] + m2) % m2;
      ans += fun(mo);
      cout << ans << '\\n';
    }
  }
  return 0;
}