Problem p02616 (python)

Fixed submission:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n, k = LI()
a = LI()
a.sort(key = lambda x:abs(x), reverse = True)
ans = 1
p = 0
m = 0
plus = []
minus = []
use = []
f = 0
for i in range(n):
    if a[i] >= 0:
        p += 1
        if i < k:
            plus.append(a[i])
            use.append(a[i])
            ans *= a[i]
            ans %= mod
    else:
        m += 1
        if i < k:
            minus.append(a[i])
            f ^= 1
            use.append(a[i])
            ans *= -a[i]
            ans %= mod
if f:
    ans = -ans
if ans < 0:
    if k == n:
        print(ans % mod)
    elif p == 0:
        a.sort(key = lambda x:abs(x))
        ans = 1
        for i in range(k):
            ans *= -a[i]
            ans %= mod
        if k % 2:
            ans = -ans
        print(ans % mod)
    else:
        for i in range(k, n):
            if a[i] < 0:
                break
        else:
            ans = -ans
            ans *= pow(-minus[-1], mod-2, mod)
            ans *= max(a[k:])
            print(ans % mod)
            quit()
        if plus:
            ans = -ans
            ans *= pow(plus[-1], mod-2, mod)
            ans %= mod
            ans *= pow(-minus[-1], mod-2, mod)
            ans %= mod
            ans1 = plus[-1] * max(a[k:])
            ans2 = minus[-1] * min(a[k:])
            print(max(ans1, ans2) * ans % mod)
        else:
            ans = -ans
            ans *= pow(-minus[-1], mod-2, mod)
            ans %= mod
            ans *= max(a[k:])
            print(ans % mod)
else:
    print(ans)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n, k = LI()
a = LI()
a.sort(key = lambda x:abs(x), reverse=True)

use = a[:k]
ans = 1
f = 0
plus = -1
minus = -1
for i in use:
    if i < 0:
        f ^= 1
    if i >= 0:
        plus = i
    else:
        minus = i
    ans *= abs(i)
    ans %= mod

if f == 0:
    print(ans)
else:
    if max(a) < 0:
        ans = 1
        for i in range(n-1, n-k-1,-1):
            ans *= abs(a[i])
            ans %= mod
        if k % 2:
            print((-ans) % mod)
        else:
            print(ans)
    else:
        ret1 = -inf
        ret2 = inf
        if min(a[k:]) >= 0:
            if plus != -1:
                ret1 = plus * max(a[k:])
                ans *= pow(minus, mod-2, mod)
                ans %= mod
        elif min(a[k:]) < 0:
            if minus != -1:
                ret2 = minus * min(a[k:])
                ans *= pow(minus, mod-2, mod)
                ans %= mod
        if ret1 >= ret2:
            ans *= ret1
            ans %= mod
        else:
            ans *= ret2
            ans %= mod
        print(ans)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n, k = LI()
a = LI()
a.sort(key = lambda x:abs(x), reverse = True)
ans = 1
p = 0
m = 0
plus = []
minus = []
f = 0
for i in range(n):
    if a[i] >= 0:
        plus.append(a[i])
        p += 1
        if i < k:
            ans *= a[i]
            ans %= mod
    else:
        minus.append(a[i])
        m += 1
        if i < k:
            f ^= 1
            ans *= -a[i]
            ans %= mod
if f:
    ans = -ans
if ans < 0:
    if k == n:
        print(ans)
    elif p == 0:
        a.sort(key = lambda x:abs(x))
        ans = 1
        for i in range(k):
            ans *= -a[i]
            ans %= mod
        if k % 2:
            ans = -ans
        print(ans % mod)
    else:
        ans1 = ans // minus[-1]
        f1 = 0
        f2 = 0
        for i in range(k, n):
            if a[i] >= 0:
                ans1 *= a[i]
                f1 = 1
                break
        ans2 = ans // plus[-1]
        ans2 = -ans2
        for i in range(k, n):
            if a[i] < 0:
                ans2 *= -a[i]
                f2 = 1
                break
        if f1 and f2:
            print(max(ans1, ans2) % mod)
        elif f1:
            print(ans1 % mod)
        else:
            print(ans2 % mod)
else:
    print(ans)