Problem p02609 (python)

Fixed submission:
def power(x, y, p) : 
    if y==0: return 1%p
    res = 1     # Initialize result 
  
    # Update x if it is more 
    # than or equal to p 
    x = x % p  
      
    if (x == 0) : 
        return 0
  
    while (y > 0) : 
          
        # If y is odd, multiply 
        # x with result 
        if ((y & 1) == 1) : 
            res = (res * x) % p 
  
        # y must be even now 
        y = y >> 1      # y = y/2 
        x = (x * x) % p 
    return res  

F = [0,1,1]
for i in range(3,200001):
	F += [ 1 + F[i%(bin(i).count('1'))]]
	

n=int(input())
s=input()
ones = s.count('1')
a = ones -1
b = ones
c = ones+1
moda = modb = modc = 0
for i in range(n-1,-1,-1):
	pos = n-1-i
	if s[i]=='1':
		if a>0 : moda = (moda + power(2, pos, a) ) %a
		modc = (modc + power(2, pos, c) ) %c

for i in range(n):
	if s[i] == '0': #ajout
		add = power(2, n-1-i, c)
		ans = (modc + add) % c
		# ~ print("i",i, "add",add,"ans",ans)
		print(F[ans]+1)
	else: # retrait
		if a<=0: print(0)
		else:
			sub = power(2, n-1-i, a)
			ans = (moda - sub) % a
			print(F[ans]+1)

Buggy submission (verdict: Runtime Error):
def power(x, y, p) : 
    if y==0: return 1%p
    res = 1     # Initialize result 
  
    # Update x if it is more 
    # than or equal to p 
    x = x % p  
      
    if (x == 0) : 
        return 0
  
    while (y > 0) : 
          
        # If y is odd, multiply 
        # x with result 
        if ((y & 1) == 1) : 
            res = (res * x) % p 
  
        # y must be even now 
        y = y >> 1      # y = y/2 
        x = (x * x) % p 
    return res  

F = [0,1,1]
for i in range(3,100001):
	F += [ 1 + F[i%(bin(i).count('1'))]]
	

n=int(input())
s=input()
ones = s.count('1')
a = ones -1
b = ones
c = ones+1
moda = modb = modc = 0
for i in range(n-1,-1,-1):
	pos = n-1-i
	if s[i]=='1':
		if a>0 : moda = (moda + power(2, pos, a) ) %a
		if b>0 : modb = (modb + power(2, pos, b) ) %b
		modc = (modc + power(2, pos, c) ) %c

for i in range(n):
	if s[i] == '0': #ajout
		add = power(2, n-1-i, c)
		ans = (modc + add) % c
		# ~ print("i",i, "add",add,"ans",ans)
		print(F[ans]+1)
	else: # retrait
		if a<=0: print(0)
		else:
			sub = power(2, n-1-i, a)
			ans = (moda - sub) % a
			print(F[ans]+1)

Buggy submission (verdict: Runtime Error):
def power(x, y, p) : 
    res = 1     # Initialize result 
  
    # Update x if it is more 
    # than or equal to p 
    x = x % p  
      
    if (x == 0) : 
        return 0
  
    while (y > 0) : 
          
        # If y is odd, multiply 
        # x with result 
        if ((y & 1) == 1) : 
            res = (res * x) % p 
  
        # y must be even now 
        y = y >> 1      # y = y/2 
        x = (x * x) % p 
          
    return res  

F = [0,1,1]
for i in range(3,100001):
	F += [ 1 + F[i%(bin(i).count('1'))]]
	

n=int(input())
s=input()
ones = s.count('1')
a = ones -1
b = ones
c = ones+1
moda = modb = modc = 0
for i in range(n-1,-1,-1):
	pos = n-1-i
	if s[i]=='1':
		if a>0 : moda = (moda + power(2, pos, a) ) %a
		if b>0 : modb = (modb + power(2, pos, b) ) %b
		modc = (modc + power(2, pos, c) ) %c

for i in range(n):
	if s[i] == '0': #ajout
		add = power(2, n-1-i, c)
		ans = (modc + add) % c
		print(F[ans]+1)
	else: # retrait
		if a<=0: print(0)
		else:
			sub = power(2, n-1-i, a)
			ans = (moda - sub) % a
			print(F[ans]+1)