Problem p02616 (python)

Fixed submission:
from bisect import bisect_left,bisect_right
p=10**9+7
n,k=map(int,input().split())
a=sorted(list(map(int,input().split())))
pi=1
ni=0
ans=1
i=0
while i<k-1:
  if a[ni]*a[ni+1]>a[-pi]*a[-pi-1]:
    ans=ans*a[ni]*a[ni+1]%p
    ni+=2
    i+=2
  else:
    ans=ans*a[-pi]%p
    pi+=1
    i+=1
if i==k-1:
  ans=ans*a[-pi]%p
if a[-1]<0 and k%2==1:
  ans=1
  for i in a[n-k:]:
    ans=ans*i%p
print(ans)

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left,bisect_right
p=10**9+7
n,k=map(int,input().split())
a=sorted(list(map(int,input().split())))
pi=1
ni=1
ans=1
for _ in range(k//2):
  if a[ni]*a[ni+1]>a[-pi]*a[-pi-1]:
    ans=ans*a[ni]*a[ni+1]%p
    ni+=2
  else:
    ans=ans*a[-pi]*a[-pi-1]%p
    pi+=2
if k%2==1:
  ans=ans*a[-pi]%p
print(ans)

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left,bisect_right
p=10**9+7
n,k=map(int,input().split())
a=sorted(list(map(int,input().split())))
minus=bisect_left(a,0) #負の数の個数
plus=n-bisect_right(a,0) #正の数の個数
neg=a[:minus] #負の数(絶対値大きい順)
pog=a[n-plus::][::-1] #正の数(絶対値大きい順)
zer=n-plus-minus #ゼロの数
ans=1
poi=0
nei=0
flag=0
co=0
sgn=1
if plus==1:
  alartp=1
else:
  alartp=0
if minus==1:
  alartn=1
else:
  alartn=0
if plus==0:
  if zer!=0 and k%2==1:
    print(0)
  else:
    for i in range(k):
      ans=ans*neg[-1-i]%p
    print(ans)
elif plus+minus<k:
  print(0)
elif minus==0:
  for _ in range(k):
    ans=ans*pog[poi]%p
    poi+=1
  print(ans)
else:
  i=0
  for i in range(k-2):
    if -neg[nei]>pog[poi]:
      ans=ans*neg[nei]%p
      sgn*=-1
      nei+=1
      if nei==minus-1:
        alartn=1
    else:
      ans=ans*pog[poi]%p
      poi+=1
      if poi==plus-1:
        alartp=1
    if alartp or alartn:
      break
  if alartn:
    if sgn==-1:
      ans=ans*neg[nei]%p
      i+=1
    for _ in range(k-i):
      ans=ans*pog[poi]%p
      poi+=1
  elif alartp==1:
    if (sgn==1 and (k-i)%2==1) or (sgn==-1 and (k-i)%2==0):
      ans=ans*pog[poi]
      i+=1
    for _ in range(k-i):
      ans=ans*neg[nei]
      nei+=1
  else:
    if sgn==1:
      a=max(pog[poi]*pog[poi+1],neg[nei]*neg[nei+1])
      ans=ans*a%p
    else:
      ans=ans*pog[poi]*neg[nei]%p
  print(ans)

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left,bisect_right
p=10**9+7
n,k=map(int,input().split())
a=sorted(list(map(int,input().split())))
minus=bisect_left(a,0) #負の数の個数
plus=n-bisect_right(a,0) #正の数の個数
neg=a[:minus] #負の数(絶対値大きい順)
pog=a[n-plus::][::-1] #正の数(絶対値大きい順)
zer=n-plus-minus #ゼロの数
ans=1
poi=0
nei=0
flag=0
co=0
sgn=1
if plus==1:
  alartp=1
else:
  alartp=0
if minus==1:
  alartn=1
else:
  alartn=0
if plus==0:
  if zer!=0 and k%2==1:
    print(0)
  else:
    for i in range(k):
      ans=ans*neg[-1-i]%p
    print(ans)
elif plus+minus<k:
  print(0)
elif minus==0:
  for _ in range(k):
    ans=ans*pog[poi]%p
    poi+=1
  print(ans)
else:
  i=0
  while (i<k-2) and (not alartn) and (not alartp):
    i+=1
    if -neg[nei]>pog[poi]:
      ans=ans*neg[nei]%p
      sgn*=-1
      nei+=1
      if nei==minus-1:
        alartn=1
    else:
      ans=ans*pog[poi]%p
      poi+=1
      if poi==plus-1:
        alartp=1
  if alartn:
    if sgn==-1:
      ans=ans*neg[nei]%p
      i+=1
    for _ in range(k-i):
      ans=ans*pog[poi]%p
      poi+=1
  elif alartp==1:
    if (sgn==1 and (k-i)%2==1) or (sgn==-1 and (k-i)%2==0):
      ans=ans*pog[poi]
      i+=1
    for _ in range(k-i):
      ans=ans*neg[nei]
      nei+=1
  else:
    if sgn==1:
      a=max(pog[poi]*pog[poi+1],neg[nei]*neg[nei+1])
      ans=ans*a%p
    else:
      ans=ans*pog[poi]*neg[nei]%p
  print(ans)