Problem p02609 (python)

Fixed submission:
def main():

    n = int(input())
    x = list(str(input()))

    p = 0
    for i in range(n):
        if x[i] == '1':
            p += 1

    if p == 0:
        ans = [1]*n
        for i in range(n):
            print(ans[i])
        exit()

    if p == 1:
        ans = [0]*n
        for i in range(n):
            if x[i] == '1':
                ans[i] = 0
            else:
                if i != n-1:
                    if x[-1] != 1:
                        ans[i] = 1
                    else:
                        ans[i] = 2
                else:
                    ans[i] = 2
        for i in range(n):
            print(ans[i])
        exit()

    def power(a, n, mod):
        bi=str(format(n,"b")) #2進数
        res=1
        for i in range(len(bi)):
            res=(res*res) %mod
            if bi[i]=="1":
                res=(res*a) %mod
        return res

    X1 = 0
    X2 = 0
    import copy
    s = copy.copy(x)
    s.reverse()
    #print(s)
    for i in range(n):
        if s[i] == '1':
            X1 += power(2, i, p+1)
            X2 += power(2, i, p-1)

    #print(p)
    #print(X1)
    #print(X2)
    ans = [0]*n
    for i in range(n):
        if x[i] == '0':
            temp = X1
            temp += power(2, n-1-i, p+1)
            temp %= (p+1)
            if temp == 0:
                ans[i] = 1
                continue
            cnt = 0
            d = p+1
            while temp:
                cnt += 1
                temp %= d
                b = format(temp, 'b')
                d = 0
                for j in range(len(b)):
                    if b[j] == '1':
                        d += 1
        else:
            temp = X2
            temp -= power(2, n-1-i, p-1)
            temp %= (p-1)
            if temp == 0:
                ans[i] = 1
                continue
            cnt = 0
            d = p-1
            while temp:
                cnt += 1
                temp %= d
                b = format(temp, 'b')
                d = 0
                for j in range(len(b)):
                    if b[j] == '1':
                        d += 1
        ans[i] = cnt
    #print(*ans)
    for i in range(n):
        print(ans[i])

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():

    n = int(input())
    x = list(str(input()))

    p = 0
    for i in range(n):
        if x[i] == '1':
            p += 1

    def power(a, n, mod):
        bi=str(format(n,"b")) #2進数
        res=1
        for i in range(len(bi)):
            res=(res*res) %mod
            if bi[i]=="1":
                res=(res*a) %mod
        return res

    X1 = 0
    X2 = 0
    import copy
    s = copy.copy(x)
    s.reverse()
    #print(s)
    for i in range(n):
        if s[i] == '1':
            X1 += power(2, i, p+1)
            X2 += power(2, i, p-1)

    #print(p)
    #print(X1)
    #print(X2)
    ans = [0]*n
    for i in range(n):
        if x[i] == '0':
            temp = X1
            temp += power(2, n-1-i, p+1)
            temp %= (p+1)
            if temp == 0:
                ans[i] = 1
                continue
            cnt = 0
            d = p+1
            while temp:
                cnt += 1
                temp %= d
                b = format(temp, 'b')
                d = 0
                for j in range(len(b)):
                    if b[j] == '1':
                        d += 1
        else:
            temp = X2
            temp -= power(2, n-1-i, p-1)
            temp %= (p-1)
            if temp == 0:
                ans[i] = 1
                continue
            cnt = 0
            d = p-1
            while temp:
                cnt += 1
                temp %= d
                b = format(temp, 'b')
                d = 0
                for j in range(len(b)):
                    if b[j] == '1':
                        d += 1
        ans[i] = cnt
    #print(*ans)
    for i in range(n):
        print(ans[i])

if __name__ == '__main__':
    main()