Problem p02616 (python)

Fixed submission:
mod=10**9+7
n,k=map(int,input().split())
arr=list(map(int,input().split()))
arr=sorted(arr,reverse=True,key=lambda x:abs(x))
if k==n:
  ans=1
  for i in range(n):
    ans*=arr[i]
    ans%=mod
  print(ans)
else:
  if k%2==1 and max(arr)<0:
    ans=1
    for i in range(k):
      ans*=arr[n-1-i]
      ans%=mod
    print(ans)
  else:
    ans=1
    cnt=0
    for i in range(k):
      if arr[i]<0:
        cnt+=1
      ans*=arr[i]
      ans%=mod
    if cnt%2==0:
      print(ans)
    else:
      min_plus=-1
      min_minus=1
      for i in range(k):
        if arr[i]>=0:
          min_plus=arr[i]
        else:
          min_minus=arr[i]
      max_plus=-1
      max_minus=1
      for i in range(k,n):
        if arr[i]>=0 and max_plus==-1:
          max_plus=arr[i]
        if arr[i]<0 and max_minus==1:
          max_minus=arr[i]
      if min_plus==-1:        
        arr.remove(min_minus)
        ans=1
        for i in range(k-1):
          ans*=arr[i]
          ans%=mod
        ans*=max_plus
        ans%=mod
      elif min_minus==1:        
        arr.remove(min_plus)
        ans=1
        for i in range(k-1):
          ans*=arr[i]
          ans%=mod
        ans*=max_minus
        ans%=mod
      elif min_plus*max_plus>=min_minus*max_minus:
        arr.remove(min_minus)
        ans=1
        for i in range(k-1):
          ans*=arr[i]
          ans%=mod
        ans*=max_plus
        ans%=mod
      else:
        arr.remove(min_plus)
        ans=1
        for i in range(k-1):
          ans*=arr[i]
          ans%=mod
        ans*=max_minus
        ans%=mod
      print(ans)

Buggy submission (verdict: Runtime Error):
N, K=map(int, input().split())
A_list = list(map(int, input().split()))

flag = 0
for i in range(N):
    if A_list[i] >= 0:
        break
    if i == N-1:
        flag = 1

if flag == 1 and K % 2 ==1:
    A_list.sort(reverse = True)
    ans = 1
    for i in A_list[:K]:
        ans *= i
    print(ans % (10 ** 9 + 7))
else:
    A_list.sort(key=abs,reverse=True)
    ans = 1
    for i in A_list[:K]:
        ans *= i
    if ans >= 0:
        pass
    else:
        # (1)
        list_p = [x for x in A_list[K:] if x >= 0]
        list_n = [x for x in A_list[:K] if x < 0]

        ans1 = ans * list_p[0] / list_n[-1]
        # (2)
        list_p = [x for x in A_list[:K] if x >= 0]
        list_n = [x for x in A_list[K:] if x < 0]
        ans2 = ans * list_n[0] / list_p[-1]

        ans = max(ans1, ans2)
    print(ans % (10 ** 9 + 7))

Buggy submission (verdict: Runtime Error):
import numpy
N, K=map(int, input().split())
A_list = list(map(int, input().split()))

flag = 0
for i in range(N):
    if A_list[i] >= 0:
        break
    if i == N-1:
        flag = 1

if flag == 1 and K % 2 ==1:
    A_list.sort(reverse = True)
    print(numpy.prod(A_list[:K]) % (10 ** 9 + 7))
else:
    A_list.sort(key=abs,reverse=True)
    ans = 1
    for i in A_list[:K]:
        ans *= i
    if ans >= 0:
        pass
    else:
        # (1)
        list_p = [x for x in A_list[K:] if x >= 0]
        list_n = [x for x in A_list[:K] if x < 0]

        ans1 = ans * list_p[0] / list_n[-1]
        # (2)
        list_p = [x for x in A_list[:K] if x >= 0]
        list_n = [x for x in A_list[K:] if x < 0]
        ans2 = ans * list_n[0] / list_p[-1]

        ans = max(ans1, ans2)
    print(ans % (10 ** 9 + 7))