Buggy python submission (verdict: Time Limit Exceeded):
import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.read
from collections import deque
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    if max(A) > 0:
        plus = True
    else:
        plus = False

    A.sort(reverse=True)

    if K%2==1 and plus==False:
        ans = 1
        for i in A[:K]:
            ans *= i % MOD
        ans %= MOD
    else:
        ans = 1
        left = 0
        right = N-1
        if K%2==1:
            ans *= A[left]
            left += 1
        for _ in range(K//2):
            if A[left]*A[left+1]>=A[right]*A[right-1]:
                ans *= A[left]*A[left+1]%MOD
                left += 2
            else:
                ans *= A[right]*A[right-1] % MOD
                right -= 2
        ans %= MOD

    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.read
from collections import deque
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    n, k = map(int, readline().split())
    A = list(map(int, readline().split()))
    MOD = int(1e9) + 7
    A.sort()
    ans = 1
    if A[n - 1] == 0 and (k & 1):
        ans = 0
    elif A[n - 1] <= 0 and (k & 1):
        for i in range(n - 1, n - k - 1, -1):
            ans = (ans * A[i]) % MOD
    else:
        i = 0
        j = n - 1
        if k & 1:
            ans = (ans * A[j]) % MOD
            j -= 1
            k -= 1
        k >>= 1
        for itr in range(k):
            left_product = A[i] * A[i + 1]
            right_product = A[j] * A[j - 1]
            if left_product > right_product:
                ans = (ans * left_product) % MOD
                i += 2
            else:
                ans = (ans * right_product) % MOD
                j -= 2

    print(ans % MOD)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.read
from collections import deque
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    plus = False

    for a in A:
        if a>0:
            plus = True
            break

    A.sort(reverse=True)

    if K%2==1 and plus==False:
        ans = 1
        for i in A[:K]:
            ans *= i % MOD
        ans %= MOD
    else:
        A = deque(A)
        ans = 1
        left = 0
        right = N-1
        if K%2==1:
            ans *= A[left]
            left += 1
        for _ in range(K//2):
            if A[left]*A[left+1]>=A[right]*A[right-1]:
                ans *= A[left]*A[left+1]%MOD
                left += 2
            else:
                ans *= A[right]*A[right-1] % MOD
                right -= 2
        ans %= MOD

    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.read
from collections import deque
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    n, k = map(int, readline().split())
    A = list(map(int, readline().split()))
    MOD = int(1e9) + 7
    A.sort()
    ans = 1
    if A[n - 1] == 0 and (k & 1):
        ans = 0
    elif A[n - 1] <= 0 and (k & 1):
        for i in range(n - 1, n - k - 1, -1):
            ans = (ans * A[i]) % MOD
    else:
        i = 0
        j = n - 1
        if k & 1:
            ans = (ans * A[j]) % MOD
            j -= 1
            k -= 1
        k >>= 1
        for itr in range(k):
            left_product = A[i] * A[i + 1]
            right_product = A[j] * A[j - 1]
            if left_product > right_product:
                ans = (ans * left_product) % MOD
                i += 2
            else:
                ans = (ans * right_product) % MOD
                j -= 2

    print(ans % MOD)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.read
from collections import deque
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    plus = False

    for a in A:
        if a>0:
            plus = True
            break

    A.sort(reverse=True)

    if K%2==1 and plus==False:
        ans = 1
        for i in A[:K]:
            ans *= i % MOD
        ans %= MOD
    else:
        A = deque(A)
        ans = 1
        if K%2==1:
            ans *= A.popleft()
        for _ in range(K//2):
            if A[0]*A[1]>=A[-1]*A[-2]:
                ans *= A.popleft()*A.popleft()%MOD
            else:
                ans *= A.pop() * A.pop() % MOD
        ans %= MOD

    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.read
from collections import deque
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    n, k = map(int, readline().split())
    A = list(map(int, readline().split()))
    MOD = int(1e9) + 7
    A.sort()
    ans = 1
    if A[n - 1] == 0 and (k & 1):
        ans = 0
    elif A[n - 1] <= 0 and (k & 1):
        for i in range(n - 1, n - k - 1, -1):
            ans = (ans * A[i]) % MOD
    else:
        i = 0
        j = n - 1
        if k & 1:
            ans = (ans * A[j]) % MOD
            j -= 1
            k -= 1
        k >>= 1
        for itr in range(k):
            left_product = A[i] * A[i + 1]
            right_product = A[j] * A[j - 1]
            if left_product > right_product:
                ans = (ans * left_product) % MOD
                i += 2
            else:
                ans = (ans * right_product) % MOD
                j -= 2

    print(ans % MOD)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.read
from collections import deque
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    plus = False
    for a in A:
        if a>0:
            plus = True
            break

    A.sort(reverse=True)

    if K%2==1 and plus==False:
        ans = 1
        for i in A[:K]:
            ans *= i % MOD
        ans %= MOD
    else:
        A = deque(A)
        ans = 1
        if K%2==1:
            ans *= A.popleft()
        for _ in range(K//2):
            if A[0]*A[1]>=A[-1]*A[-2]:
                ans *= A.popleft()*A.popleft()%MOD
            else:
                ans *= A.pop() * A.pop() % MOD
        ans %= MOD

    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.read
from collections import deque
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    n, k = map(int, readline().split())
    A = list(map(int, readline().split()))
    MOD = int(1e9) + 7
    A.sort()
    ans = 1
    if A[n - 1] == 0 and (k & 1):
        ans = 0
    elif A[n - 1] <= 0 and (k & 1):
        for i in range(n - 1, n - k - 1, -1):
            ans = (ans * A[i]) % MOD
    else:
        i = 0
        j = n - 1
        if k & 1:
            ans = (ans * A[j]) % MOD
            j -= 1
            k -= 1
        k >>= 1
        for itr in range(k):
            left_product = A[i] * A[i + 1]
            right_product = A[j] * A[j - 1]
            if left_product > right_product:
                ans = (ans * left_product) % MOD
                i += 2
            else:
                ans = (ans * right_product) % MOD
                j -= 2

    print(ans % MOD)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.read
from collections import deque
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    plus = False
    for a in A:
        if a>0:
            plus = True
            break


    if K%2==1 and plus==False:
        A.sort(reverse=True)
        ans = 1
        for i in minus[:K]:
            ans *= i % MOD
        ans %= MOD
    else:
        A.sort(reverse=True)
        A = deque(A)
        ans = 1
        if K%2==1:
            ans *= A.popleft()
        for _ in range(K//2):
            if A[0]*A[1]>=A[-1]*A[-2]:
                ans *= A.popleft()*A.popleft()%MOD
            else:
                ans *= A.pop() * A.pop() % MOD
        ans %= MOD

    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.read
from collections import deque
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    n, k = map(int, readline().split())
    A = list(map(int, readline().split()))
    MOD = int(1e9) + 7
    A.sort()
    ans = 1
    if A[n - 1] == 0 and (k & 1):
        ans = 0
    elif A[n - 1] <= 0 and (k & 1):
        for i in range(n - 1, n - k - 1, -1):
            ans = (ans * A[i]) % MOD
    else:
        i = 0
        j = n - 1
        if k & 1:
            ans = (ans * A[j]) % MOD
            j -= 1
            k -= 1
        k >>= 1
        for itr in range(k):
            left_product = A[i] * A[i + 1]
            right_product = A[j] * A[j - 1]
            if left_product > right_product:
                ans = (ans * left_product) % MOD
                i += 2
            else:
                ans = (ans * right_product) % MOD
                j -= 2

    print(ans % MOD)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.read
from collections import deque
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    plus = []
    minus = []
    plus_count = 0
    zero_count = 0
    for a in A:
        if a>0:
            plus.append(a)
            plus_count += 1
        else:
            minus.append(a)

    if K%2==1 and plus_count==0:
        minus.sort(reverse=True)
        ans = 1
        for i in minus[:K]:
            ans *= i % MOD
        ans %= MOD
    else:
        A.sort(reverse=True)
        A = deque(A)
        ans = 1
        if K%2==1:
            ans *= A.popleft()
        for _ in range(K//2):
            if A[0]*A[1]>=A[-1]*A[-2]:
                ans *= A.popleft()*A.popleft()%MOD
            else:
                ans *= A.pop() * A.pop() % MOD
        ans %= MOD

    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.read
from collections import deque
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    n, k = map(int, readline().split())
    A = list(map(int, readline().split()))
    MOD = int(1e9) + 7
    A.sort()
    ans = 1
    if A[n - 1] == 0 and (k & 1):
        ans = 0
    elif A[n - 1] <= 0 and (k & 1):
        for i in range(n - 1, n - k - 1, -1):
            ans = (ans * A[i]) % MOD
    else:
        i = 0
        j = n - 1
        if k & 1:
            ans = (ans * A[j]) % MOD
            j -= 1
            k -= 1
        k >>= 1
        for itr in range(k):
            left_product = A[i] * A[i + 1]
            right_product = A[j] * A[j - 1]
            if left_product > right_product:
                ans = (ans * left_product) % MOD
                i += 2
            else:
                ans = (ans * right_product) % MOD
                j -= 2

    print(ans % MOD)

if __name__ == '__main__':
    main()