Problem p02616 (python)

Fixed submission:
from sys import stdin
n,k = map(int,stdin.readline().split())
arr = sorted(list(map(int,stdin.readline().split())),key=lambda x:abs(x))
mod = 10**9+7
pos,neg = [],[]
for i in arr:
    if i>=0:
        pos.append(i)
    else:
        neg.append(i)
if not len(pos):
    ans = 1
    if k%2:
        for i in range(k):
            ans *= neg[i]
            ans %= mod
    else:
        for i in range(k):
            ans *= neg[-1-i]
            ans %= mod
else:
    ans = 1
    i=len(pos)-1
    j=len(neg)-1
    val = 0
    if n==k:
        for x in arr:
            ans *= x
            ans %= mod
    else:
        if k%2:
            ans *= pos[i]
            i -= 1
            val += 1
        while val != k:
            if i<1:
                ans *= neg[j]*neg[j-1]
                ans %= mod
                j -= 2
            elif j<1:
                ans *= pos[i]*pos[i-1]
                ans %= mod
                i -= 2
            else:
                if pos[i]*pos[i-1]>neg[j]*neg[j-1]:
                    ans *= pos[i]*pos[i-1]
                    ans %= mod
                    i -= 2
                else:
                    ans *= neg[j]*neg[j-1]
                    ans %= mod
                    j -= 2
            val += 2
print(ans)

Buggy submission (verdict: Runtime Error):
from sys import stdin
n,k = map(int,stdin.readline().split())
arr = sorted(list(map(int,stdin.readline().split())),key=lambda x:abs(x))
mod = 10**9+7
pos,neg = [],[]
for i in arr:
    if i>=0:
        pos.append(i)
    else:
        neg.append(i)
if not len(pos):
    ans = 1
    if k%2:
        for i in range(k):
            ans *= neg[i]
            ans %= mod
    else:
        for i in range(k):
            ans *= neg[-1-i]
            ans %= mod
else:
    ans = 1
    i=len(pos)-1
    j=len(neg)-1
    val = 0
    if n==k:
        for x in arr:
            ans *= x
            ans %= mod
    elif k==1:
        ans = pos[i]%mod
    else:
        while val != k-2:
            if i == 0:
                ans *= neg[j]
                j -= 1
            elif j == 0:
                ans *= pos[i]
                i -= 1
            else:
                if pos[i]>neg[j]:
                    ans *= pos[i]
                    i -= 1
                else:
                    ans *= neg[j]
                    j -= 1
            ans %= mod
            val += 1
        if (len(neg)-1-j)%2:
            ans *= pos[i]*neg[j]
        else:
            if i==0:
                ans *= neg[j]*neg[j-1]
            elif j==0:
                ans *= pos[i]*pos[i-1]
            else:
                ans *= max(pos[i]*pos[i-1],neg[j]*neg[j-1])
        ans %= mod
print(ans)

Buggy submission (verdict: Runtime Error):
from sys import stdin
n,k = map(int,stdin.readline().split())
arr = sorted(list(map(int,stdin.readline().split())),key=lambda x:abs(x))
mod = 10**9+7
pos,neg = [],[]
for i in arr:
    if i>=0:
        pos.append(i)
    else:
        neg.append(i)
if not len(pos):
    ans = 1
    if k%2:
        for i in range(k):
            ans *= neg[i]
            ans %= mod
    else:
        for i in range(k):
            ans *= neg[-1-i]
            ans %= mod
else:
    ans = 1
    i=len(pos)-1
    j=len(neg)-1
    val = 0
    if n==k:
        for x in arr:
            ans *= x
            ans %= mod
    elif k==1:
        ans = pos[i]%mod
    else:
        while val != k-2:
            if i == 0:
                ans *= neg[j]
                j -= 1
            elif j == 0:
                ans *= pos[i]
                i -= 1
            else:
                if pos[i]>neg[j]:
                    ans *= pos[i]
                    i -= 1
                else:
                    ans *= neg[j]
                    j -= 1
            ans %= mod
            val += 1
        if (j-len(neg)+1)%2:
            ans *= pos[i]*neg[j]
        else:
            if i==0:
                ans *= neg[j]*neg[j-1]
            elif j==0:
                ans *= pos[i]*pos[i-1]
            else:
                ans *= max(pos[i]*pos[i-1],neg[j]*neg[j-1])
        ans %= mod
print(ans)

Buggy submission (verdict: Runtime Error):
from sys import stdin
n,k = map(int,stdin.readline().split())
arr = sorted(list(map(int,stdin.readline().split())),key=lambda x:abs(x))
mod = 10**9+7
pos,neg = [],[]
for i in arr:
    if i>=0:
        pos.append(i)
    else:
        neg.append(i)
if not len(pos):
    ans = 1
    if k%2:
        for i in range(k):
            ans *= neg[i]
            ans %= mod
    else:
        for i in range(k):
            ans *= neg[-1-i]
            ans %= mod
else:
    ans = 1
    i=len(pos)-1
    j=len(neg)-1
    pro = 1
    for r in range(k):
        pro *= pos[i]
        pro %= mod
        i -= 1
    ans = max(ans,pro)
    while i < len(pos)-1 and j > 0:
        if neg[j]*neg[j-1]>pos[i]*pos[i+1]:
            pro *= pow(pos[i]*pos[i+1],mod-2,mod)
            pro %= mod
            pro *= neg[j]*neg[j-1]
            pro %= mod
            ans = max(ans,pro)
            i += 2
            j -= 2
        else:
            break
print(ans)

Buggy submission (verdict: Runtime Error):
from sys import stdin
n,k = map(int,stdin.readline().split())
arr = sorted(list(map(int,stdin.readline().split())),key=lambda x:abs(x))
mod = 10**9+7
pos,neg = [],[]
for i in arr:
    if i>=0:
        pos.append(i)
    else:
        neg.append(i)
if not len(pos):
    ans = 1
    if k%2:
        for i in range(k):
            ans *= neg[i]
            ans %= mod
    else:
        for i in range(k):
            ans *= neg[-1-i]
            ans %= mod
else:
    ans = 1
    i=len(pos)-1
    j=len(neg)-1
    val = 0
    if k%2:
        ans *= pos[i]
        i -= 1
        val += 1
    while val != k:
        if i<1:
            ans *= neg[j]*neg[j-1]
            ans %= mod
            j -= 2
        elif j<1:
            ans *= pos[i]*pos[i-1]
            ans %= mod
            i -= 2
        else:
            if pos[i]*pos[i-1]>neg[i]*neg[i-1]:
                ans *= pos[i]*pos[i-1]
                ans %= mod
                i -= 2
            else:
                ans *= neg[j] *neg[j-1]
                ans %= mod
                j -= 2
        k -= 2
print(ans)