Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def main():
    n,k = map(int, input().split())
    A = list(map(int, input().split()))
    ans = 1
    
    if k == n:
        for a in A:
            ans *= a
        print(ans%(10**9+7))
        
    elif k%2==1 and all([a<0 for a in A]):
        A = sorted(A, key=lambda x:abs(x))
        for a in A[:k]:
            ans*=a
        print(ans%(10**9+7))

    else:
        A = sorted(A, key=lambda x:abs(x), reverse=True)
        x1, x2 = None, None
        for a in A[:k]:
            ans*=a
            if a >= 0 and (x1 is None or a < x1): x1 = a
            if a < 0 and (x2 is None or a > x2): x2 = a
            
        if ans >= 0:
            print(ans%(10**9+7))
        else:
            y1, y2 = None, None
            for a in A[k:]:
                if a < 0 and (y1 is None or a < y1): y1 = a
                if a >= 0 and (y2 is None or a > y2): y2 = a
            
            tmp = [ans]
            if x1!=None and y1!=None:
                tmp.append(ans//x1*y1)
            if x2!=None and y2!=None:
                tmp.append(ans//x2*y2)
            ans = max(tmp)
            print(ans%(10**9+7))            
main()

Fixed submission:
import sys
input = sys.stdin.readline

def main():
    n,k = map(int, input().split())
    A = sorted(map(int, input().split()))
    mod = 10**9 + 7
    ans, sign = 1, 1

    l, r = 0, n-1

    if k%2:
        ans = A[r]
        r-=1
        k-=1
        if ans < 0:
            sign = -1
    while k:
        x = A[l]*A[l+1]
        y = A[r]*A[r-1]

        if x*sign > y*sign:
            ans = x%mod * ans%mod
            l+=2
        else:
            ans = y%mod * ans%mod
            r-=2
        k-=2
    print((ans+mod)%mod)
main()

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

def main():
    n,k = map(int, input().split())
    A = map(int, input().split())
    A = sorted(A, key=lambda x: abs(x), reverse=True)

    ans = 1

    minK, maxK = None, None
    for a in A[:k]:
        ans *= a
        if minK is None or a < minK:
            minK = a
        if maxK is None or a > maxK:
            maxK = a

    if ans >= 0:
        print(ans % (10**9+7))
    else:
        minK_1, maxK_1 = None, None
        for a in A[k:]:
            if minK_1 is None or a < minK_1:
                minK_1 = a
            if maxK_1 is None or a > maxK_1:
                maxK_1 = a

        tmpMax = max(ans//minK * maxK_1, ans//maxK * minK_1)
        if tmpMax > ans:
            ans = tmpMax

        print(ans % (10**9+7))
main()

Fixed submission:
import sys
input = sys.stdin.readline

def main():
    n,k = map(int, input().split())
    A = sorted(map(int, input().split()))
    mod = 10**9 + 7
    ans, sign = 1, 1

    l, r = 0, n-1

    if k%2:
        ans = A[r]
        r-=1
        k-=1
        if ans < 0:
            sign = -1
    while k:
        x = A[l]*A[l+1]
        y = A[r]*A[r-1]

        if x*sign > y*sign:
            ans = x%mod * ans%mod
            l+=2
        else:
            ans = y%mod * ans%mod
            r-=2
        k-=2
    print((ans+mod)%mod)
main()

Buggy python submission (verdict: Runtime Error):

def main():
    n,k=map(int,input().split())
    A = map(int,input().split())

    def prod(iterables):
        res = None
        for element in iterables:
            tmp = 1
            for e in element:
                tmp *= e
            if res is None or tmp > res:
                res = tmp
        return res
    
    pos, neg = [], []
    for a in A:
        if a >= 0:
            pos.append(a)
        else:
            neg.append(a)

    pos_n, neg_n = len(pos), len(neg)
    ans = None

    for i in range(neg_n):
        if k-i > pos_n:
            continue
        tmp = prod(permutations(neg, i)) * prod(permutations(pos, k-i)) % (10**9+7)
        if ans is None or tmp > ans:
            ans = tmp
    print(ans)
main()

Fixed submission:
import sys
input = sys.stdin.readline

def main():
    n,k = map(int, input().split())
    A = sorted(map(int, input().split()))
    mod = 10**9 + 7
    ans, sign = 1, 1

    l, r = 0, n-1

    if k%2:
        ans = A[r]
        r-=1
        k-=1
        if ans < 0:
            sign = -1
    while k:
        x = A[l]*A[l+1]
        y = A[r]*A[r-1]

        if x*sign > y*sign:
            ans = x%mod * ans%mod
            l+=2
        else:
            ans = y%mod * ans%mod
            r-=2
        k-=2
    print((ans+mod)%mod)
main()

Buggy python submission (verdict: Runtime Error):


def main():
    n,k=map(int,input().split())
    A = map(int,input().split())

    def prod(iterables):
        res = None
        for element in iterables:
            tmp = 1
            for e in element:
                tmp *= e
            if res is None or tmp > res:
                res = tmp
        return res
    
    pos, neg = [], []
    for a in A:
        if a >= 0:
            pos.append(a)
        else:
            neg.append(a)

    pos_n, neg_n = len(pos), len(neg)
    pos, neg

    ans = None

    for i in range(neg_n):
        if k-i > pos_n:
            continue
        tmp = prod(permutations(neg, i)) * prod(permutations(pos, k-i)) % (10**9+7)
        if ans is None or tmp > ans:
            ans = tmp
    print(ans)
main()

Fixed submission:
import sys
input = sys.stdin.readline

def main():
    n,k = map(int, input().split())
    A = sorted(map(int, input().split()))
    mod = 10**9 + 7
    ans, sign = 1, 1

    l, r = 0, n-1

    if k%2:
        ans = A[r]
        r-=1
        k-=1
        if ans < 0:
            sign = -1
    while k:
        x = A[l]*A[l+1]
        y = A[r]*A[r-1]

        if x*sign > y*sign:
            ans = x%mod * ans%mod
            l+=2
        else:
            ans = y%mod * ans%mod
            r-=2
        k-=2
    print((ans+mod)%mod)
main()