Buggy python submission (verdict: Runtime Error):
plus = []
minus = []
zero = 0
ans = 1

for i in a:
    if i > 0:
        plus.append(i)
    elif i < 0:
        minus.append(-i)
    else:
        zero += 1

plus.sort()
minus.sort()
p_l = len(plus)
m_l = len(minus)
cnt = 0

if k == 1:
    print(max(a))
elif zero > n - k:
    print(0)
elif zero == n - k:
    for i in plus:
        ans = (i * ans) % num
    for j in minus:
        ans = (ans * j) % num
    if m_l % 2 == 0:
        print(ans)
    else:
        print(-ans % num)
elif len(plus) == 0:
    if k % 2 == 0:
        for j in range(k):
            ans = ans * (minus[-(j + 1)]) % num
        print(ans % num)
    else:
        for j in range(k):
            ans = ans * (minus[j]) % num
        print(-ans % num)
else:
    for i in range(k):
        if p_l > 1 and m_l > 1:
            if plus[-1] >= minus[-1]:
                ans = ans * plus.pop() % num
                p_l -= 1
            else:
                ans = ans * minus.pop() % num
                cnt += 1
                m_l -= 1
        elif m_l == 1:
            if cnt % 2 == 1:
                ans = ans * minus.pop() % num
                m_l -= 1
                cnt += 1
            else:
                ans = ans * plus.pop() % num
                p_l -= 1
        elif p_l == 1:
            if k - i - 1 % 2 == 0:
                ans = ans * plus.pop() % num
                p_l -= 1
            else:
                ans = ans * minus.pop() % num
                cnt += 1
                m_l -= 1
        elif p_l == 0:
            ans = ans * minus.pop() % num
            m_l -= 1
            cnt += 1
        elif m_l == 0:
            ans = ans * plus.pop() % num
            p_l -= 1
    if cnt % 2 == 0:
        print(ans % num)
    else:
        print(-ans % num)

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

plus = []
minus = []
zero = 0
ans = 1

for i in a:
    if i > 0:
        plus.append(i)
    elif i < 0:
        minus.append(-i)
    else:
        zero += 1

plus.sort()
minus.sort()
p_l = len(plus)
m_l = len(minus)
cnt = 0


def tail_num(p):
    return p[-1] * p[-2]


if k == 1:
    print(max(a) % num)
elif zero > n - k:
    print(0)
elif zero == n - k:
    for i in plus:
        ans = (i * ans) % num
    for j in minus:
        ans = (ans * j) % num
    if m_l % 2 == 0:
        print(ans)
    else:
        print(-ans % num)
elif len(plus) == 0:
    if k % 2 == 0:
        for j in range(k):
            ans = ans * (minus[-(j + 1)]) % num
        print(ans % num)
    else:
        if 0 in a:
            print(0)
        else:
            for j in range(k):
                ans = ans * (minus[j]) % num
            print(-ans % num)
else:
    if k % 2 == 1:
        ans = ans * plus.pop() % num
        p_l -= 1
    for i in range(k//2):
        if p_l >= 2 and m_l >= 2:
            if tail_num(minus) >= tail_num(plus):
                ans = ans * tail_num(minus) % num
                minus.pop()
                minus.pop()
                m_l -= 2
            else:
                ans = ans * tail_num(plus) % num
                plus.pop()
                plus.pop()
                p_l -= 2
        else:
            if p_l <= 1:
                ans = ans * tail_num(minus) % num
                minus.pop()
                minus.pop()
                m_l -= 2
            else:
                ans = ans * tail_num(plus) % num
                plus.pop()
                plus.pop()
                p_l -= 2
    print(ans)

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

plus = []
minus = []
zero = 0
ans = 1

for i in a:
    if i > 0:
        plus.append(i)
    elif i < 0:
        minus.append(-i)
    else:
        zero += 1
        
plus.sort(reverse=True)
minus.sort(reverse=True)
plus_l = len(plus)
minus_l = len(minus)
plus_p = 0
minus_p = 0
plus_b = 1
minus_b = 1

if zero > n - k:
    print(0)
elif len(plus) == 0:
    for j in range(k):
        ans *= (minus[(j + 1)]) % num
    if k % 2 == 0:
        print(ans % num)
    else:
        print(-ans % num)
else:
    if k % 2 == 0:
        while plus_l >= 2 and minus_l >= 2 and k >= 2:
            plus_b = plus[plus_p] * plus[plus_p + 1]
            minus_b = minus[minus_p] * minus[minus_p + 1]
            if plus_b >= minus_b:
                ans *= plus_b % num
                plus_p += 2
                plus_l -= 2
                
            else:
                ans *= minus_b % num
                minus_p += 2
                minus_l -= 2
                
            k -= 2
        if k >= 2:
            if plus_l < 2 and minus_l >= 2:
                while k >= 2:
                    minus_b = minus[minus_p] * minus[minus_p + 1]
                    ans *= minus_b % num
                    minus_p += 2
                    minus_l -= 2
                    k -= 2
            elif plus_l >= 2 and minus_l < 2:
                while k >= 2:
                    plus_b = plus[plus_p] * plus[plus_p + 1]
                    ans *= plus_b % num
                    plus_p += 2
                    plus_l -= 2
                    k -= 2
            if k == 2 and plus_l == 1 and minus_l == 1:
                ans *= (plus[plus_p] * minus[minus_p]) % num
    else:
        while plus_l >= 3 and minus_l >= 3 and k >= 2:
            plus_b = plus[plus_p] * plus[plus_p + 1]
            minus_b = minus[minus_p] * minus[minus_p + 1]
            if plus_b >= minus_b:
                ans *= plus_b % num
                plus_p += 2
                plus_l -= 2
                
            else:
                ans *= minus_b % num
                minus_p += 2
                minus_l -= 2
                
            k -= 2
        if k >= 2:
            if plus_l < 2 and minus_l >= 3:
                while k >= 2:
                    minus_b = minus[minus_p] * minus[minus_p + 1]
                    ans *= minus_b % num
                    minus_p += 2
                    minus_l -= 2
                    k -= 2
            elif plus_l >= 3 and minus_l < 2:
                while k >= 2:
                    plus_b = plus[plus_p] * plus[plus_p + 1]
                    ans *= plus_b % num
                    plus_p += 2
                    plus_l -= 2
                    k -= 2
        if k == 1:
            ans *= plus[plus_p]  % num
    print(ans % num)

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

plus = []
minus = []
zero = 0
ans = 1

for i in a:
    if i > 0:
        plus.append(i)
    elif i < 0:
        minus.append(-i)
    else:
        zero += 1

plus.sort()
minus.sort()
p_l = len(plus)
m_l = len(minus)
cnt = 0


def tail_num(p):
    return p[-1] * p[-2]


if k == 1:
    print(max(a) % num)
elif zero > n - k:
    print(0)
elif zero == n - k:
    for i in plus:
        ans = (i * ans) % num
    for j in minus:
        ans = (ans * j) % num
    if m_l % 2 == 0:
        print(ans)
    else:
        print(-ans % num)
elif len(plus) == 0:
    if k % 2 == 0:
        for j in range(k):
            ans = ans * (minus[-(j + 1)]) % num
        print(ans % num)
    else:
        if 0 in a:
            print(0)
        else:
            for j in range(k):
                ans = ans * (minus[j]) % num
            print(-ans % num)
else:
    if k % 2 == 1:
        ans = ans * plus.pop() % num
        p_l -= 1
    for i in range(k//2):
        if p_l >= 2 and m_l >= 2:
            if tail_num(minus) >= tail_num(plus):
                ans = ans * tail_num(minus) % num
                minus.pop()
                minus.pop()
                m_l -= 2
            else:
                ans = ans * tail_num(plus) % num
                plus.pop()
                plus.pop()
                p_l -= 2
        else:
            if p_l <= 1:
                ans = ans * tail_num(minus) % num
                minus.pop()
                minus.pop()
                m_l -= 2
            else:
                ans = ans * tail_num(plus) % num
                plus.pop()
                plus.pop()
                p_l -= 2
    print(ans)

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

plus = []
minus = []
zero = 0
ans = 1

for i in a:
    if i > 0:
        plus.append(i)
    elif i < 0:
        minus.append(-i)
    else:
        zero += 1
        
plus.sort(reverse=True)
minus.sort(reverse=True)
plus_l = len(plus)
minus_l = len(minus)
plus_p = 0
minus_p = 0
plus_b = 1
minus_b = 1

if zero > n - k:
    print(0)
elif len(plus) == 0:
    for j in range(k):
        ans *= (minus[(j + 1)]) % num
    if k % 2 == 0:
        print(ans % num)
    else:
        print(-ans % num)
else:
    if k % 2 == 0:
        while plus_l >= 2 and minus_l >= 2 and k >= 2:
            plus_b = plus[plus_p] * plus[plus_p + 1]
            minus_b = minus[minus_p] * minus[minus_p + 1]
            if plus_b >= minus_b:
                ans *= plus_b % num
                plus_p += 2
                plus_l -= 2
                
            else:
                ans *= minus_b % num
                minus_p += 2
                minus_l -= 2
                
            k -= 2
        if k >= 2:
            if plus_l < 2 and minus_l >= 2:
                while k >= 2:
                    minus_b = minus[minus_p] * minus[minus_p + 1]
                    ans *= minus_b % num
                    minus_p += 2
                    minus_l -= 2
                    k -= 2
            elif plus_l >= 2 and minus_l < 2:
                while k >= 2:
                    plus_b = plus[plus_p] * plus[plus_p + 1]
                    ans *= plus_b % num
                    plus_p += 2
                    plus_l -= 2
                    k -= 2
            if k == 2 and plus_l == 1 and minus_l == 1:
                ans *= (plus[plus_p] * minus[minus_p]) % num
    else:
        while plus_l >= 3 and minus_l >= 3 and k >= 2:
            plus_b = plus[plus_p] * plus[plus_p + 1]
            minus_b = minus[minus_p] * minus[minus_p + 1]
            if plus_b >= minus_b:
                ans *= plus_b % num
                plus_p += 2
                plus_l -= 2
                
            else:
                ans *= minus_b % num
                minus_p += 2
                minus_l -= 2
                
            k -= 2
        if k >= 2:
            if plus_l < 2 and minus_l >= 3:
                while k >= 2:
                    minus_b = minus[minus_p] * minus[minus_p + 1]
                    ans *= minus_b % num
                    minus_p += 2
                    minus_l -= 2
                    k -= 2
            elif plus_l >= 3 and minus_l < 2:
                while k >= 2:
                    plus_b = plus[plus_p] * plus[plus_p + 1]
                    ans *= plus_b % num
                    plus_p += 2
                    plus_l -= 2
                    k -= 2
        if k == 1:
            ans *= plus[plus_p]  % num
    print(ans % num)

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

plus = []
minus = []
zero = 0
ans = 1

for i in a:
    if i > 0:
        plus.append(i)
    elif i < 0:
        minus.append(-i)
    else:
        zero += 1

plus.sort()
minus.sort()
p_l = len(plus)
m_l = len(minus)
cnt = 0


def tail_num(p):
    return p[-1] * p[-2]


if k == 1:
    print(max(a) % num)
elif zero > n - k:
    print(0)
elif zero == n - k:
    for i in plus:
        ans = (i * ans) % num
    for j in minus:
        ans = (ans * j) % num
    if m_l % 2 == 0:
        print(ans)
    else:
        print(-ans % num)
elif len(plus) == 0:
    if k % 2 == 0:
        for j in range(k):
            ans = ans * (minus[-(j + 1)]) % num
        print(ans % num)
    else:
        if 0 in a:
            print(0)
        else:
            for j in range(k):
                ans = ans * (minus[j]) % num
            print(-ans % num)
else:
    if k % 2 == 1:
        ans = ans * plus.pop() % num
        p_l -= 1
    for i in range(k//2):
        if p_l >= 2 and m_l >= 2:
            if tail_num(minus) >= tail_num(plus):
                ans = ans * tail_num(minus) % num
                minus.pop()
                minus.pop()
                m_l -= 2
            else:
                ans = ans * tail_num(plus) % num
                plus.pop()
                plus.pop()
                p_l -= 2
        else:
            if p_l <= 1:
                ans = ans * tail_num(minus) % num
                minus.pop()
                minus.pop()
                m_l -= 2
            else:
                ans = ans * tail_num(plus) % num
                plus.pop()
                plus.pop()
                p_l -= 2
    print(ans)