Problem p02616 (python)

Fixed submission:
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
a=list(map(int,input().split()))

INF=10**9+7

x=False
y=False
z=False
xl=[]
yl=[]
for i in a:
    if i>0:
        x=True
        xl.append(i)
    elif i<0:
        y=True
        yl.append(i)
    else:
        z=True

if not y:
    a.sort(reverse=True)
    ans=1
    for i in range(k):
        ans=(ans*a[i])%INF

else:
    if not x:
        if z:
            if k%2==0:
                a.sort()
                ans=1
                for i in range(k):
                    ans=(ans*a[i])%INF
            else:
                ans=0
        else:
            if k%2==0:
                a.sort()
                ans=1
                for i in range(k):
                    ans=(ans*a[i])%INF
            else:
                a.sort(reverse=True)
                ans=1
                for i in range(k):
                    ans=(ans*a[i])%INF
    else:
        if z:
            if len(xl)+len(yl)<k:
                ans=0
            elif len(xl)+len(yl)==k:
                ans=1
                for i in xl:
                    ans=(ans*i)%INF
                for i in yl:
                    ans=(ans*i)%INF
                if len(yl)%2==0:
                    pass
                else:
                    ans=0
            else:
                if k%2==0:
                    xyl=[]
                    xl.sort(reverse=True)
                    yl.sort()
                    for i in range(len(xl)//2):
                        xyl.append(xl[i*2]*xl[i*2+1])
                    for i in range(len(yl)//2):
                        xyl.append(yl[i*2]*yl[i*2+1])
                    xyl.sort(reverse=True)
                    ans=1
                    for i in range(k//2):
                        ans=(ans*xyl[i])%INF

                else:
                    xyl = []
                    xl.sort(reverse=True)
                    yl.sort()
                    for i in range((len(xl)-1) // 2):
                        xyl.append(xl[i * 2+1] * xl[i * 2 + 2])
                    for i in range(len(yl) // 2):
                        xyl.append(yl[i * 2] * yl[i * 2 + 1])
                    xyl.sort(reverse=True)
                    ans = xl[0]
                    for i in range((k-1) // 2):
                        ans = (ans * xyl[i]) % INF

        else:
            if k==n:
                ans=1
                for i in a:
                    ans=(ans*i)%INF
            else:
                if k%2==0:
                    xyl=[]
                    xl.sort(reverse=True)
                    yl.sort()
                    for i in range(len(xl)//2):
                        xyl.append(xl[i*2]*xl[i*2+1])
                    for i in range(len(yl)//2):
                        xyl.append(yl[i*2]*yl[i*2+1])
                    xyl.sort(reverse=True)
                    ans=1
                    for i in range(k//2):
                        ans=(ans*xyl[i])%INF

                else:
                    xyl = []
                    xl.sort(reverse=True)
                    yl.sort()
                    for i in range((len(xl)-1) // 2):
                        xyl.append(xl[i * 2+1] * xl[i * 2 + 2])
                    for i in range(len(yl) // 2):
                        xyl.append(yl[i * 2] * yl[i * 2 + 1])
                    xyl.sort(reverse=True)
                    ans = xl[0]
                    for i in range((k-1) // 2):
                        ans = (ans * xyl[i]) % INF


print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
a=list(map(int,input().split()))

INF=10**9+7

x=False
y=False
z=False
xl=[]
yl=[]
for i in a:
    if i>0:
        x=True
        xl.append(i)
    elif i<0:
        y=True
        yl.append(i)
    else:
        z=True

if not y:
    a.sort(reverse=True)
    ans=1
    for i in range(k):
        ans=(ans*a[i])%INF

else:
    if not x:
        if z:
            if k%2==0:
                a.sort()
                ans=1
                for i in range(k):
                    ans=(ans*a[i])%INF
            else:
                ans=0
        else:
            if k%2==0:
                a.sort()
                ans=1
                for i in range(k):
                    ans=(ans*a[i])%INF
            else:
                a.sort(reverse=True)
                ans=1
                for i in range(k):
                    ans=(ans*a[i])%INF
    else:
        if z:
            if len(xl)+len(yl)>k:
                ans=0
            
            if len(xl)+len(yl)==k:
                ans=1
                for i in xl:
                    ans=(ans*i)%INF
                for i in yl:
                    ans=(ans*i)%INF
                if len(yl)%2==0:
                    pass
                else:
                    ans=0
            
            if k%2==0:
                xyl=[]
                xl.sort(reverse=True)
                yl.sort()
                for i in range(len(xl)//2):
                    xyl.append(xl[i*2]*xl[i*2+1])
                for i in range(len(yl)//2):
                    xyl.append(yl[i*2]*yl[i*2+1])
                xyl.sort(reverse=True)
                ans=1
                for i in range(k//2):
                    ans=(ans*xyl[i])%INF

            else:
                xyl = []
                xl.sort(reverse=True)
                yl.sort()
                for i in range((len(xl)-1) // 2):
                    xyl.append(xl[i * 2+1] * xl[i * 2 + 2])
                for i in range(len(yl) // 2):
                    xyl.append(yl[i * 2] * yl[i * 2 + 1])
                xyl.sort(reverse=True)
                ans = xl[0]
                for i in range((k-1) // 2):
                    ans = (ans * xyl[i]) % INF
            
        else:
            if k==n:
                ans=1
                for i in a:
                    ans=(ans*i)%INF
            
            if k%2==0:
                xyl=[]
                xl.sort(reverse=True)
                yl.sort()
                for i in range(len(xl)//2):
                    xyl.append(xl[i*2]*xl[i*2+1])
                for i in range(len(yl)//2):
                    xyl.append(yl[i*2]*yl[i*2+1])
                xyl.sort(reverse=True)
                ans=1
                for i in range(k//2):
                    ans=(ans*xyl[i])%INF

            else:
                xyl = []
                xl.sort(reverse=True)
                yl.sort()
                for i in range((len(xl)-1) // 2):
                    xyl.append(xl[i * 2+1] * xl[i * 2 + 2])
                for i in range(len(yl) // 2):
                    xyl.append(yl[i * 2] * yl[i * 2 + 1])
                xyl.sort(reverse=True)
                ans = xl[0]
                for i in range((k-1) // 2):
                    ans = (ans * xyl[i]) % INF


print(ans)