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

#include <iostream>
#include <string>

using namespace std;

long long int ModPow(long long int a, long long int p, long long int M) {
  long long int res = 1;
  long long int f = a;
  while (p > 0) {
    if (p & 1) {
      res = (res * f) % M;
    }
    f = (f * f) % M;
    p >>= 1;
  }
  return res;
}

long long int Popcount(long long int X) {
  long long int cntr = 0;
  while (X > 0) {
    if (X & 1)
      cntr++;
    X >>= 1;
  }
  return cntr;
};

int main() {
  int N;
  cin >> N;
  string s;
  long long int baseM = 0;
  long long int evenR = 0;
  long long int oddR = 0;
  for (int i = 0; i < N; i++) {
    char c;
    cin >> c;
    s.push_back(c);
    if (c - '0')
      baseM++;
  }
  if (baseM != 1) {
    for (int i = 0; i < N; i++) {
      if (s[i] - '0') {
        evenR = (evenR + ModPow(2, N - i - 1, baseM + 1)) % (baseM + 1);
        oddR = (oddR + ModPow(2, N - i - 1, baseM - 1)) % (baseM - 1);
      }
    }
    for (int k = 0; k < N; k++) {
      long long int cntr = 1;
      long long int R = 0;
      long long int M = 0;
      if (s[k] - '0') {
        M = baseM - 1;
        R = (oddR - ModPow(2, N - k - 1, M)) % M;
      } else {
        M = baseM + 1;
        R = (evenR + ModPow(2, N - k - 1, M)) % M;
      }
      if (R < 0)
        R += M;
      if (R == 0 || M == 0) {
        cout << cntr << endl;
        continue;
      }
      do {
        M = Popcount(R);
        R = R % M;
        cntr++;
      } while (R > 0);
      cout << cntr << endl;
    }
  } else {
    for (int i = 0; i < N; i++) {
      if (s[i] - '0') {
        evenR = (evenR + ModPow(2, N - i - 1, baseM + 1)) % (baseM + 1);
      }
    }
    for (int k = 0; k < N; k++) {
      long long int cntr = 1;
      long long int R = 0;
      long long int M = 0;
      if (s[k] - '0') {
        cout << 0 << endl;
        continue;
      } else {
        M = baseM + 1;
        R = (evenR + ModPow(2, N - k - 1, M)) % M;
      }
      if (R < 0)
        R += M;
      if (R == 0 || M == 0) {
        cout << cntr << endl;
        continue;
      }
      do {
        M = Popcount(R);
        R = R % M;
        cntr++;
      } while (R > 0);
      cout << cntr << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

long long int ModPow(long long int a, long long int p, long long int M) {
  long long int res = 1;
  long long int f = a;
  while (p > 0) {
    if (p & 1) {
      res = (res * f) % M;
    }
    f = (f * f) % M;
    p >>= 1;
  }
  return res;
}

long long int Popcount(long long int X) {
  long long int cntr = 0;
  while (X > 0) {
    if (X & 1)
      cntr++;
    X >>= 1;
  }
  return cntr;
};

int main() {
  int N;
  cin >> N;
  string s;
  long long int baseM = 0;
  long long int evenR = 0;
  long long int oddR = 0;
  for (int i = 0; i < N; i++) {
    char c;
    cin >> c;
    s.push_back(c);
    if (c - '0')
      baseM++;
  }
  for (int i = 0; i < N; i++) {
    if (s[i] - '0') {
      evenR = (evenR + ModPow(2, N - i - 1, baseM + 1)) % (baseM + 1);
      oddR = (oddR + ModPow(2, N - i - 1, baseM - 1)) % (baseM - 1);
    }
  }
  for (int k = 0; k < N; k++) {
    long long int cntr = 1;
    long long int R = 0;
    long long int M = 0;
    if (s[k] - '0') {
      M = baseM - 1;
      R = (oddR - ModPow(2, N - k - 1, M)) % M;
    } else {
      M = baseM + 1;
      R = (evenR + ModPow(2, N - k - 1, M)) % M;
    }
    if (R < 0)
      R += M;
    if (R == 0 || M == 0) {
      cout << cntr << endl;
      continue;
    }
    do {
      M = Popcount(R);
      R = R % M;
      cntr++;
    } while (R > 0);
    cout << cntr << endl;
  }
}