Problem p02862 (python)

Fixed submission:
x,y=map(int,input().split())
def cal(number,mod):
    m=mod-2
    mod2=""
    while m!=0:
        if m%2==0:
            mod2="0"+mod2
        else:
            mod2="1"+mod2
        m=m//2
    two=[]
    for i in range(len(mod2)):
        if mod2[len(mod2)-1-i]=="1":
            two.append(i)
    ret=1
    for i in two:
        v=i
        retsub=number
        while v>0:
            retsub=retsub**2
            retsub=retsub%mod
            v-=1
        ret*=retsub
    ret=ret%mod
    return ret
ans=0
M=10**9+7
if (2*x-y)%3==0:
    n=(x+y)//3
    r=(2*x-y)//3
    if n<r or r<0:
        print(0)
        exit()
    l=[1]
    num=1
    for i in range(1,n+1):
        p=(num*i)%(M)
        l.append(p)
        num=p
    a=l[n]
    b=cal(l[n-r],M)
    c=cal(l[r],M)
    ans=(a*b*c)%M
print(ans)

Buggy submission (verdict: Runtime Error):
x,y=map(int,input().split())
def cal(number,mod):
    m=mod-2
    mod2=""
    while m!=0:
        if m%2==0:
            mod2="0"+mod2
        else:
            mod2="1"+mod2
        m=m//2
    two=[]
    for i in range(len(mod2)):
        if mod2[len(mod2)-1-i]=="1":
            two.append(i)
    ret=1
    for i in two:
        v=i
        retsub=number
        while v>0:
            retsub=retsub**2
            retsub=retsub%mod
            v-=1
        ret*=retsub
    ret=ret%mod
    return ret
ans=0
M=10**9+7
if (2*x-y)%3==0:
    n=(x+y)//3
    r=(2*x-y)//3
    if n<r:
        print(0)
        exit()
    l=[1]
    num=1
    for i in range(1,n+1):
        p=(num*i)%(M)
        l.append(p)
        num=p
    a=l[n]
    b=cal(l[n-r],M)
    c=cal(l[r],M)
    ans=(a*b*c)%M
print(ans)

Buggy submission (verdict: Runtime Error):
x,y=map(int,input().split())
def cal(number,mod):
    m=mod-2
    mod2=""
    while m!=0:
        if m%2==0:
            mod2="0"+mod2
        else:
            mod2="1"+mod2
        m=m//2
    two=[]
    for i in range(len(mod2)):
        if mod2[len(mod2)-1-i]=="1":
            two.append(i)
    ret=1
    for i in two:
        v=i
        retsub=number
        while v>0:
            retsub=retsub**2
            retsub=retsub%mod
            v-=1
        ret*=retsub
    ret=ret%mod
    return ret
ans=0
M=10**9+7
if (2*x-y)%3==0:
    n=(x+y)//3
    r=(2*x-y)//3
    if n<r:
        print(0)
        exit()
    l=[0]
    num=1
    for i in range(1,n+1):
        p=(num*i)%(M)
        l.append(p)
        num=p
    a=l[n]
    b=cal(l[n-r],M)
    c=cal(l[r],M)
    ans=(a*b*c)%M

print(ans)

Buggy submission (verdict: Runtime Error):
x,y=map(int,input().split())
def cal(number,mod):
    m=mod-2
    mod2=""
    while m!=0:
        if m%2==0:
            mod2="0"+mod2
        else:
            mod2="1"+mod2
        m=m//2
    two=[]
    for i in range(len(mod2)):
        if mod2[len(mod2)-1-i]=="1":
            two.append(i)
    ret=1
    for i in two:
        v=i
        retsub=number
        while v>0:
            retsub=retsub**2
            retsub=retsub%mod
            v-=1
        ret*=retsub
    ret=ret%mod
    return ret
ans=0
M=10**9+7
if (2*x-y)%3==0:
    n=(x+y)//3
    r=(2*x-y)//3
    l=[]
    num=1
    for i in range(1,n+1):
        p=(num*i)%(M)
        l.append(p)
        num=p
    ans=(l[n-1]*cal(l[n-r-1],M)*cal(l[r-1],M))%M
print(ans)