Buggy python submission (verdict: Runtime Error):
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    mod = 10 ** 9+ 7
    ans = 1

    def answer(a):
        ans =1
        for x in a:
            ans *= x
            ans %= mod
        return ans

    if n == k:
        print(answer(a))
        return
    a.sort(reverse=True, key= lambda x:abs(x))
    if sum(x<0 for x in a[:k])%2 == 0:
        print(answer(a[:k])
    else:
        if all(x < 0 for x in a):
            print(answer(a[-k:]))
        else:
            try:
                x1, y1= min([x for x in a[:k] if x > 0]), min([x for x in a[k:] if x < 0])
            except ValueError:
                x1, y1 = 1, 0
            try:
                x2, y2= max([x for x in a[:k] if x < 0]),\
                    max([x for x in a[k:] if x >= 0])
            except ValueError:
                x2, y2 = 1, 0
            if abs(x2*y1) > abs(x1*y2):           
                a[a.index(x1)] = y1
            else:
                a[a.index(x2)] = y2
            print(answer(a[:k]))
            


if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    mod = 10 ** 9+ 7
    ans = 1

    def answer(a):
        ans =1
        for x in a:
            ans *= x
            ans %= mod
        return ans

    if n == k:
        print(answer(a))
        return
    a.sort(reverse=True, key= lambda x:abs(x))
    if sum(x<0 for x in a[:k])%2 == 0:
        print(answer(a[:k]))
    else:
        if all(x < 0 for x in a):
            print(answer(a[-k:]))
        else:
            try:
                x1, y1= min([x for x in a[:k] if x > 0]), min([x for x in a[k:] if x < 0])
            except ValueError:
                x1, y1 = 1, 0
            try:
                x2, y2= max([x for x in a[:k] if x < 0]),\
                    max([x for x in a[k:] if x >= 0])
            except ValueError:
                x2, y2 = 1, 0
            if abs(x2*y1) > abs(x1*y2):           
                a[a.index(x1)] = y1
            else:
                a[a.index(x2)] = y2
            print(answer(a[:k]))
            


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    mod = 10 ** 9+ 7
    ans = 1

    def answer(a):
        ans =1
        for i in range(k):
            ans *= a[i]
            ans %= mod
        print(ans)

    if n == k:
        answer(a)
        return

    if all([True if x < 0 else False for x in a]) and k%2 == 1:
        a.sort(reverse=True)
        answer(a)
        return

    a.sort(reverse=True, key=lambda x: abs(x))
    flag = 1
    for i in range(k):
        if a[i] < 0:
            flag ^= 1
    if flag or 0 in a[:k]:
        answer(a)
        return
    
    x1, x2, y1, y2 = k-1, k-1, k, k
    while a[x1] >= 0 and x1 >= 0:
        x1 -= 1
    while a[x2] < 0 and x2 >= 0:
        x2 -= 1
    while a[y1] < 0 and y1 < n:
        y1 += 1
    while a[y2] >= 0 and y2 < n:
        y2 += 1

    if x1 == -1 or y1 == n:
        a[x2], a[y2] = a[y2], a[x2]
    
    elif x2 == -1 or y2 == n:
        a[x1], a[y1] = a[y1], a[x1]
    else:
        if a[x1]*a[y2] > a[x2]*a[y1]:
            a[x2], a[y2] = a[y2], a[x2]
        else:
            a[x1], a[y1] = a[y1], a[x1]
    answer(a)
    
    

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    mod = 10 ** 9+ 7
    ans = 1

    def answer(a):
        ans =1
        for x in a:
            ans *= x
            ans %= mod
        return ans

    if n == k:
        print(answer(a))
        return
    a.sort(reverse=True, key= lambda x:abs(x))
    if sum(x<0 for x in a[:k])%2 == 0:
        print(answer(a[:k]))
    else:
        if all(x < 0 for x in a):
            print(answer(a[-k:]))
        else:
            try:
                x1, y1= min([x for x in a[:k] if x > 0]), min([x for x in a[k:] if x < 0])
            except ValueError:
                x1, y1 = 1, 0
            try:
                x2, y2= max([x for x in a[:k] if x < 0]),\
                    max([x for x in a[k:] if x >= 0])
            except ValueError:
                x2, y2 = 1, 0
            if abs(x2*y1) > abs(x1*y2):           
                a[a.index(x1)] = y1
            else:
                a[a.index(x2)] = y2
            print(answer(a[:k]))
            


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    mod = 10 ** 9+ 7
    ans = 1

    def answer(a):
        ans =1
        for i in range(k):
            ans *= a[i]
            ans %= mod
        print(ans)

    if n == k:
        answer(a)
        return

    if all([True if x < 0 else False for x in a]):
        a.sort(reverse=True)
        answer(a)
        return

    a.sort(reverse=True, key=lambda x: abs(x))
    flag = 1
    for i in range(k):
        if a[i] < 0:
            flag ^= 1
    if flag or 0 in a[:k]:
        answer(a)
        return
    
    x1, x2, y1, y2 = k-1, k-1, k, k
    while a[x1] >= 0 and x1 >= 0:
        x1 -= 1
    while a[x2] < 0 and x2 >= 0:
        x2 -= 1
    while a[y1] < 0 and y1 < n:
        y1 += 1
    while a[y2] >= 0 and y2 < n:
        y2 += 1

    if x1 == -1 or y1 == n:
        a[x2], a[y2] = a[y2], a[x2]
    
    elif x2 == -1 or y2 == n:
        a[x1], a[y1] = a[y1], a[x1]
    else:
        if a[x1]*a[y2] > a[x2]*a[y1]:
            a[x2], a[y2] = a[y2], a[x2]
        else:
            a[x1], a[y1] = a[y1], a[x1]
    answer(a)
    
    

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    mod = 10 ** 9+ 7
    ans = 1

    def answer(a):
        ans =1
        for x in a:
            ans *= x
            ans %= mod
        return ans

    if n == k:
        print(answer(a))
        return
    a.sort(reverse=True, key= lambda x:abs(x))
    if sum(x<0 for x in a[:k])%2 == 0:
        print(answer(a[:k]))
    else:
        if all(x < 0 for x in a):
            print(answer(a[-k:]))
        else:
            try:
                x1, y1= min([x for x in a[:k] if x > 0]), min([x for x in a[k:] if x < 0])
            except ValueError:
                x1, y1 = 1, 0
            try:
                x2, y2= max([x for x in a[:k] if x < 0]),\
                    max([x for x in a[k:] if x >= 0])
            except ValueError:
                x2, y2 = 1, 0
            if abs(x2*y1) > abs(x1*y2):           
                a[a.index(x1)] = y1
            else:
                a[a.index(x2)] = y2
            print(answer(a[:k]))
            


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    mod = 10 ** 9+ 7
    ans = 1

    def answer(a):
        ans =1
        for i in range(k):
            ans *= a[i]
            ans %= mod
        print(ans)

    if n == k:
        answer(a)
        return

    if all([True if x < 0 else False for x in a]):
        a.sort(reverse=True)
        answer(a)
        return

    a.sort(reverse=True, key=lambda x: abs(x))
    flag = 1
    for i in range(k):
        if a[i] < 0:
            flag ^= 1
    if flag or a[k-1] == 0:
        answer(a)
        return
    
    x1, x2, y1, y2 = k-1, k-1, k, k
    while a[x1]*a[x2] > 0 and x2 >= 0:
        x2 -= 1

    while a[x1]*a[y1] > 0 and y1 < n:
        y1 += 1

    if x2 == -1:
        a[x1], a[y1] = a[y1], a[x1]
    else:
        while a[x2]*a[y2] > 0 and y2 < n:
            y2 += 1
        if y1 == n:
            a[x2], a[y2] = a[y2], a[x2]
        else:
            if a[x1]*a[y2] < a[x2]*a[y1]:
                a[x1], a[y1] = a[y1], a[x1]
            else:
                a[x2], a[y2] = a[y2], a[x2]
    answer(a)
 

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    mod = 10 ** 9+ 7
    ans = 1

    def answer(a):
        ans =1
        for x in a:
            ans *= x
            ans %= mod
        return ans

    if n == k:
        print(answer(a))
        return
    a.sort(reverse=True, key= lambda x:abs(x))
    if sum(x<0 for x in a[:k])%2 == 0:
        print(answer(a[:k]))
    else:
        if all(x < 0 for x in a):
            print(answer(a[-k:]))
        else:
            try:
                x1, y1= min([x for x in a[:k] if x > 0]), min([x for x in a[k:] if x < 0])
            except ValueError:
                x1, y1 = 1, 0
            try:
                x2, y2= max([x for x in a[:k] if x < 0]),\
                    max([x for x in a[k:] if x >= 0])
            except ValueError:
                x2, y2 = 1, 0
            if abs(x2*y1) > abs(x1*y2):           
                a[a.index(x1)] = y1
            else:
                a[a.index(x2)] = y2
            print(answer(a[:k]))
            


if __name__ == '__main__':
    main()