Problem p02616 (python)

Fixed submission:
import sys
input=lambda: sys.stdin.readline().rstrip()
n,k=map(int,input().split())
A=[int(i) for i in input().split()]
mod=10**9+7
A.sort(reverse=True,key=lambda x:abs(x))

if min(A)>0:
  ans=1
  for i in range(k):
    ans=(ans*A[i])%mod
  print(ans)
elif max(A)<0:
  ans=1
  if k%2==0:
    for i in range(k):
      ans=(ans*A[i])%mod
    print(ans)
  else:
    for i in range(k):
      ans=(ans*A[n-1-i])%mod
    print(ans)
elif n==k:
  ans=1
  for i in range(k):
    ans=(ans*A[i])%mod
  print(ans)
elif k==1:
  print(max(A)%mod)

else:
  L1,L2,R1,R2=[],[],[],[]
  ct=0
  for a in A:
    if a==0:
      continue
    if ct<k:
      if a>0:
        L1.append(a)
      elif a<0:
        L2.append(a)
      ct+=1
    else:
      if a>0:
        R1.append(a)
      elif a<0:
        R2.append(a)
  if ct<k:
    print(0)
  else:
    if not L1:
      if k%2==1:
        if not R1:
          print(0)


        else:
          c=R1[0]
          ans=1
          for i in range(len(L2)-1):
            ans=(ans*L2[i])%mod
          ans=(ans*c)%mod
          print(ans)
      else:
        ans=1
        for i in range(len(L2)):
          ans=(ans*L2[i])%mod
        print(ans)
    elif not L2:
      ans=1
      for i in range(len(L1)):
        ans=(ans*L1[i])%mod
      print(ans)
    else:
      if len(L2)%2==0:
        ans=1
        for a in L1:
          ans=(ans*a)%mod
        for a in L2:
          ans=(ans*a)%mod
        print(ans)
      else:
        if not R1:
          if not R2:
            if 0 in A:
              print(0)
            else:
              ans=1
              for a in L1:
                ans=(ans*a)%mod
              for a in L2:
                ans=(ans*a)%mod
              print(ans)
          else:
            d=-R2[0]
            ans=d
            for i in range(len(L1)-1):
              ans=(ans*L1[i])%mod
            for i in range(len(L2)):
              ans=(ans*abs(L2[i]))%mod
            print(ans)
        else:
          c=R1[0]
          a=L1[-1]
          b=L2[-1]
          if R2 and R2[0]*b>a*c:
            ans=R2[0]
            for i in range(len(L1)-1):
              ans=(ans*L1[i])%mod
            for i in range(len(L2)):
              ans=(ans*L2[i])%mod
            print(ans)
          else:
            ans=c
            for i in range(len(L1)):
              ans=(ans*L1[i])%mod
            for i in range(len(L2)-1):
              ans=(ans*L2[i])%mod
            print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input=lambda: sys.stdin.readline().rstrip()
n,k=map(int,input().split())
A=[int(i) for i in input().split()]
mod=10**9+7
A.sort(reverse=True,key=lambda x:abs(x))

if min(A)>0:
  ans=1
  for i in range(k):
    ans=(ans*A[i])%mod
  print(ans)
elif max(A)<0:
  ans=1
  if k%2==0:
    for i in range(k):
      ans=(ans*A[i])%mod
    print(ans)
  else:
    for i in range(k):
      ans=(ans*A[n-1-i])%mod
    print(ans)
else:
  L1,L2,R1,R2=[],[],[],[]
  ct=0
  for a in A:
    if a==0:
      continue
    if ct<k:
      if a>0:
        L1.append(a)
      elif a<0:
        L2.append(a)
      ct+=1
    else:
      if a>0:
        R1.append(a)
      elif a<0:
        R2.append(a)
  if ct<k:
    print(0)
  else:
    if not L1:
      if k%2==1:
        c=R1[0]
        ans=1
        for i in range(len(L2)-1):
          ans=(ans*L2[i])%mod
        ans=(ans*c)%mod
        print(ans)
      else:
        ans=1
        for i in range(len(L2)):
          ans=(ans*L2[i])%mod
        print(ans)
    elif not L2:
      ans=1
      for i in range(len(L1)):
        ans=(ans*L1[i])%mod
      print(ans)
    else:
      if len(L2)%2==0:
        ans=1
        for a in L1:
          ans=(ans*a)%mod
        for a in L2:
          ans=(ans*a)%mod
        print(ans)
      else:
        if not R1:
          if not R2:
            if 0 in A:
              print(0)
            else:
              ans=1
              ans=1
              for a in L1:
                ans=(ans*a)%mod
              for a in L2:
                ans=(ans*a)%mod
              print(ans)
          else:
            d=-R2[0]
            ans=d
            for i in range(len(L1)-1):
              ans=(ans*L1[i])%mod
            for i in range(len(L2)):
              ans=(ans*L2[i])%mod
            print(ans)
        else:
          c=R1[0]
          a=L1[-1]
          b=L2[-1]
          if R2 and R2[0]*b>a*c:
            ans=R2[0]
            for i in range(len(L1)-1):
              ans=(ans*L1[i])%mod
            for i in range(len(L2)):
              ans=(ans*L2[i])%mod
            print(ans)
          else:
            ans=c
            for i in range(len(L1)):
              ans=(ans*L1[i])%mod
            for i in range(len(L2)-1):
              ans=(ans*L2[i])%mod
            print(ans)