Problem p02616 (python)

Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10**9 + 7

positive = []
negative = []
zero = 0
for t in a:
    if t < 0:
        negative.append(t)
    elif t == 0:
        positive.append(0)
        zero += 1
    else:
        positive.append(t)
if n - zero < k:
    print(0)
    exit()

positive.sort()
negative.sort(reverse=True)

if not positive:
    ans = 1
    if k % 2 == 1:
        if zero != 0:
            print(0)
            exit()
        for i in range(k):
            ans *= negative[i]
            ans %= mod
    else:
        for i in range(k):
            ans *= negative.pop()
            ans %= mod
    print(ans)
    exit()

ans = 1
# negative 処理
check = []
if len(negative) % 2 == 1:
    ww = negative[0]
    negative = negative[1:]
while negative:
    check.append(negative.pop() * negative.pop())

# positive
if k % 2 == 1:
    ans *= positive.pop()
    k -= 1

if len(positive) % 2 == 1:
    w = positive[0]
    positive = positive[1:]
while positive:
    check.append(positive.pop() * positive.pop())
check.sort()


for i in range(k//2):
    if check:
        ans *= check.pop()
    else:
        print(ans*w*ww%mod)
        exit()
    ans %= mod

print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10**9 + 7

positive = []
negative = []
zero = 0
for t in a:
    if t < 0:
        negative.append(t)
    elif t == 0:
        positive.append(0)
        zero += 1
    else:
        positive.append(t)
if n - zero < k:
    print(0)
    exit()

positive.sort()
negative.sort(reverse=True)

if not positive:
    ans = 1
    if k % 2 == 1:
        if zero != 0:
            print(0)
            exit()
        for i in range(k):
            ans *= negative[i]
            ans %= mod
    else:
        for i in range(k):
            ans *= negative.pop()
            ans %= mod
    print(ans)
    exit()

ans = 1
# negative 処理
check = []
if len(negative) % 2 == 1:
    negative = negative[1:]
while negative:
    check.append(negative.pop() * negative.pop())

# positive
if k % 2 == 1:
    ans *= positive.pop()
    k -= 1

if len(positive) % 2 == 1:
    positive = positive[1:]
while positive:
    check.append(positive.pop() * positive.pop())
check.sort()

for i in range(k//2):
    ans *= check.pop()
    ans %= mod

print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10**9 + 7

positive = []
negative = []
zero = 0
for t in a:
    if t < 0:
        negative.append(t)
    elif t == 0:
        positive.append(0)
        zero += 1
    else:
        positive.append(t)
if n - zero < k:
    print(0)
    exit()

positive.sort()
negative.sort(reverse=True)

if not positive:
    ans = 1
    if k % 2 == 1:
        if zero != 0:
            print(0)
            exit()
        for i in range(k):
            ans *= negative[i]
            ans %= mod
    else:
        for i in range(k):
            ans *= negative.pop()
            ans %= mod
    print(ans)
    exit()

ans = 1
# negative 処理
check = []
for i in reversed(range(len(negative) % 2, len(negative), 2)):
    check.append(negative[i+1] * negative[i])
# positive

if k % 2 == 1:
    ans *= positive.pop()
    k -= 1

for i in reversed(range(len(positive) % 2, len(positive), 2)):
    check.append(positive[i+1] * positive[i])
check.sort()

for i in range(k//2):
    ans *= check.pop()
    ans %= mod

print(ans)