Problem p02616 (python)

Fixed submission:
N, K = map(int, input().split())
A = list(map(int, input().split()))

mod = 10 ** 9 + 7
plus = []
zero = []
minus = []
ans = 1

for k in range(N):
  if A[k] > 0:
    plus.append(A[k])
  elif A[k] < 0:
    minus.append(-1 * A[k])
  else:
    zero.append(A[k])
plus.sort(reverse = True)
minus.sort(reverse = True)
lp = len(plus)
lm = len(minus)

if len(zero) + K > N:
  print(0)
  exit()
elif lp == 0: 
  if K % 2 == 1:
    if len(zero) > 0:
      print(0)
      exit()
    else:
      for k in range(K):
        ans = ans * (-1) * minus[- k - 1] % mod  
      print(ans)
      exit()
  else:
    for k in range(K):
      ans = ans * (-1) * minus[k] % mod
    print(ans)
    exit()
elif lm == 0:
  for k in range(K):
    ans = ans * plus[k] % mod
  print(ans)
  exit()

p = 0
m = 0
for k in range(K):
  if plus[p] >= minus[m]:
    p += 1
    if p == lp:
      m = K - p  
      break
  else:
    m += 1
    if m == lm:
      p = K - m
      break

def main(mp, mm, plus, minus, mod, K):
  ans = 1
  for k in range(mp):
    ans = ans * plus[k] % mod
  for k in range(mm):
    ans = ans * (-1) * minus[k] % mod
  print(ans)
  exit()

if m%2 == 0:
  main(p, m, plus, minus, mod, K)
else:
  if p == lp:
    if m != lm:
      main(p - 1, m + 1, plus, minus, mod, K)
    else:
      if len(zero) > 0:
        print(0)
        exit()
      else:
        main(p, m, plus, minus, mod, K)
  elif m == lm:
    main(p + 1, m - 1, plus, minus, mod, K)
  elif p == 0:
    main(p + 1, m - 1, plus, minus, mod, K)
  else:
    if plus[p] * plus[p - 1] > minus[m - 1] * minus[m]:
      main(p + 1, m - 1, plus, minus, mod, K)
    else:
      main(p - 1, m + 1, plus, minus, mod, K)

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

mod = 10 ** 9 + 7
plus = []
zero = []
minus = []
ans = 1

for k in range(N):
  if A[k] > 0:
    plus.append(A[k])
  elif A[k] < 0:
    minus.append(-1 * A[k])
  else:
    zero.append(A[k])
plus.sort(reverse = True)
minus.sort(reverse = True)
lp = len(plus)
lm = len(minus)

if len(zero) + K > N:
  print(0)
  exit()
elif lp == 0: 
  if K % 2 == 1:
    if len(zero) > 0:
      print(0)
      exit()
    else:
      for k in range():
        ans = ans * (-1) * minus[- k - 1] % mod  
      print(ans)
      exit()
  else:
    for k in range(K):
      ans = ans * (-1) * minus[k] % mod
    print(ans)
    exit()
elif lm == 0:
  for k in range(K):
    ans = ans * plus[k] % mod
  print(ans)
  exit()

p = 0
m = 0
for k in range(K):
  if plus[p] >= minus[m]:
    p += 1
    if p == lp:
      m = K - p  
      break
  else:
    m += 1
    if m == lm:
      p = K - m
      break

def main(mp, mm, plus, minus, mod, K):
  if mp > len(plus) or mm > len(minus):
    exit()
  ans = 1
  for k in range(mp):
    ans = ans * plus[k] % mod
  for k in range(mm):
    ans = ans * (-1) * minus[k] % mod
  print(ans)
  exit()

if m%2 == 0:
  main(p, m, plus, minus, mod, K)
else:
  if p == lp:
    if m != lm:
      main(p - 1, m + 1, plus, minus, mod, K)
    else:
      if len(zero) > 0:
        print(0)
        exit()
      else:
        main(p, m, plus, minus, mod, K)
  elif m == lm:
    main(p + 1, m - 1, plus, minus, mod, K)
  elif p == 0:
    main(p + 1, m - 1, plus, minus, mod, K)
  else:
    if plus[p] * plus[p - 1] > minus[m - 1] * minus[m]:
      main(p + 1, m - 1, plus, minus, mod, K)
    else:
      main(p - 1, m + 1, plus, minus, mod, K)

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

mod = 10 ** 9 + 7
plus = []
zero = []
minus = []
ans = 1

for k in range(N):
  if A[k] > 0:
    plus.append(A[k])
  elif A[k] < 0:
    minus.append(-1 * A[k])
  else:
    zero.append(A[k])
plus.sort(reverse = True)
minus.sort(reverse = True)
lp = len(plus)
lm = len(minus)

if len(zero) + K > N:
  print(0)
  exit()
elif lp == 0: 
  if K % 2 == 1:
    if len(zero) > 0:
      print(0)
      exit()
    else:
      for k in range():
        ans = ans * (-1) * minus[- k - 1] % mod  
      print(ans)
      exit()
  else:
    for k in range(K):
      ans = ans * (-1) * minus[k] % mod
    print(ans)
    exit()
elif lm == 0:
  for k in range(K):
    ans = ans * plus[k] % mod
  print(ans)
  exit()

p = 0
m = 0
for k in range(K):
  if plus[p] >= minus[m]:
    p += 1
    if p == lp:
      m = K - p  
      break
  else:
    m += 1
    if m == lm:
      p = K - m
      break

def main(mp, mm, plus, minus, mod, K):
  if mp < 0 or mm < 0:
    exit()  
  ans = 1
  for k in range(mp):
    ans = ans * plus[k] % mod
  for k in range(mm):
    ans = ans * (-1) * minus[k] % mod
  print(ans)
  exit()

if m%2 == 0:
  main(p, m, plus, minus, mod, K)
else:
  if p == lp:
    if m != lm:
      main(p - 1, m + 1, plus, minus, mod, K)
    else:
      if len(zero) > 0:
        print(0)
        exit()
      else:
        main(p, m, plus, minus, mod, K)
  elif m == lm:
    main(p + 1, m - 1, plus, minus, mod, K)
  elif p == 0:
    main(p + 1, m - 1, plus, minus, mod, K)
  else:
    if plus[p] * plus[p - 1] > minus[m - 1] * minus[m]:
      main(p + 1, m - 1, plus, minus, mod, K)
    else:
      main(p - 1, m + 1, plus, minus, mod, K)

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

mod = 10 ** 9 + 7
plus = []
zero = []
minus = []
ans = 1

for k in range(N):
  if A[k] > 0:
    plus.append(A[k])
  elif A[k] < 0:
    minus.append(-1 * A[k])
  else:
    zero.append(A[k])
plus.sort(reverse = True)
minus.sort(reverse = True)
lp = len(plus)
lm = len(minus)

if len(zero) + K > N:
  print(0)
  exit()
elif lp == 0: 
  if K % 2 == 1:
    if len(zero) > 0:
      print(0)
      exit()
    else:
      for k in range():
        ans = ans * (-1) * minus[- k - 1] % mod  
      print(ans)
      exit()
  else:
    for k in range(K):
      ans = ans * (-1) * minus[k] % mod
    print(ans)
    exit()
elif lm == 0:
  for k in range(K):
    ans = ans * plus[k] % mod
  print(ans)
  exit()

p = 0
m = 0
for k in range(K):
  if plus[p] >= minus[m]:
    p += 1
    if p == lp:
      m = K - p  
      break
  else:
    m += 1
    if m == lm:
      p = K - m
      break

def main(mp, mm, plus, minus, mod, K):
  ans = 1
  for k in range(mp):
    ans = ans * plus[k] % mod
  for k in range(mm):
    ans = ans * (-1) * minus[k] % mod
  print(ans)
  exit()

if m%2 == 0:
  main(p, m, plus, minus, mod, K)
else:
  if p == lp:
    if m != lm:
      main(p - 1, m + 1, plus, minus, mod, K)
    else:
      if len(zero) > 0:
        print(0)
        exit()
      else:
        main(p, m, plus, minus, mod, K)
  elif m == lm:
    main(p + 1, m-1, plus, minus, mod, K)
  elif p == 0:
    main(p + 1, m-1, plus, minus, mod, K)
  else:
    if plus[p] * plus[p-1] > minus[m-1] * minus[m]:
      main(p + 1, m-1, plus, minus, mod, K)
    else:
      main(p - 1, m + 1, plus, minus, mod, K)

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

mod = 10 ** 9 + 7
plus = []
zero = []
minus = []
ans = 1

for k in range(N):
  if A[k] > 0:
    plus.append(A[k])
  elif A[k] < 0:
    minus.append(-1 * A[k])
  else:
    zero.append(A[k])
plus.sort(reverse = True)
minus.sort(reverse = True)
lp = len(plus)
lm = len(minus)

if len(zero) + K > N:
  print(0)
  exit()
elif lp == 0: 
  if K % 2 == 1:
    if len(zero) > 0:
      print(0)
      exit()
    else:
      for k in range():
        ans = ans * (-1) * minus[- k - 1] % mod  
      print(ans)
      exit()
  else:
    for k in range(K):
      ans = ans * (-1) * minus[k] % mod
    print(ans)
    exit()
elif lm == 0:
  for k in range(K):
    ans = ans * plus[k] % mod
  print(ans)
  exit()

p = 0
m = 0
for k in range(K):
  if plus[p] >= minus[m]:
    p += 1
    if p == lp:
      m = K - p  
      break
  else:
    m += 1
    if m == lm:
      p = K - m
      break

def main(mp, mm, plus, minus, mod, K):
  ans = 1
  for k in range(mp):
    ans = ans * plus[k] % mod
  for k in range(mm):
    ans = ans * (-1) * minus[k] % mod
  print(ans)
  exit()

if m%2 == 0:
  main(p, m, plus, minus, mod, K)
else:
  if p == lp:
    if m != lm:
      main(p-1, m, plus, minus, mod, K)
    else:
      if len(zero) > 0:
        print(0)
        exit()
      else:
        main(p, m, plus, minus, mod, K)
  elif m == lm:
    main(p, m-1, plus, minus, mod, K)
  elif p == 0:
    main(p, m-1, plus, minus, mod, K)
  else:
    if plus[p] * plus[p-1] > minus[m-1] * minus[m]:
      main(p, m-1, plus, minus, mod, K)
    else:
      main(p-1, m, plus, minus, mod, K)

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

mod = 10 ** 9 + 7
plus = []
zero = []
minus = []
ans = 1

for k in range(N):
  if A[k] > 0:
    plus.append(A[k])
  elif A[k] < 0:
    minus.append(-1 * A[k])
  else:
    zero.append(A[k])
plus.sort(reverse = True)
minus.sort(reverse = True)
lp = len(plus)
lm = len(minus)

if lp == 0 and K % 2 == 1:
  for k in range(K):
    ans = ans * minus[- k - 1] % mod
  ans = -1 * ans % mod  
  print(ans)
  exit()
if lp == 0 and K % 2 == 0:
  for k in range(K):
    ans = ans * minus[k] % mod
  print(ans)
  exit()
if lm == 0:
  for k in range(K):
    ans = ans * plus[k] % mod
  print(ans)
  exit()
if len(zero) + K > N:
  print(0)
  exit()

p = 0
m = 0
for k in range(K):
  if plus[p] >= minus[m]:
    p += 1
    if p == lp:
      m = K - p  
      break
  else:
    m += 1
    if m == lm:
      p = K - m
      break

def main(mp, mm, plus, minus, mod, K):
  p = 0
  m = 0
  ans = 1
  if mp == 0:
    for _ in range(K):
      ans = ans * minus[_] % mod
  elif mm == 0:
    for _ in range(K):
      ans = ans * plus[_] % mod
  else:
    for _ in range(K):
      if plus[p] >= minus[m]:
        ans = ans * plus[p] % mod
        p += 1
        if p == mp:
          for j in range(K - p - m):
            ans = ans * minus[m + j] % mod
          break
      else:
        ans = ans * minus[m] % mod
        m += 1
        if m == mm:
          for j in range(K - p - m):
            ans = ans * plus[p + j] % mod
          break  
  print(ans)
  exit()

if m%2 == 0:
  main(lp, lm, plus, minus, mod, K)
else:
  if p == lp:
    if m != lm:
      main(lp-1, lm, plus, minus, mod, K)
    else:
      if len(zero) > 0:
        print(0)
        exit()
      else:
        main(lp, lm, plus, minus, mod, K)
  elif m == lm:
    main(lp, lm-1, plus, minus, mod, K)
  elif p == 0:
    main(lp, lm-1, plus, minus, mod, K)
  else:
    if plus[p] * plus[p-1] > minus[m-1] * minus[m]:
      main(lp, lm-1, plus, minus, mod, K)
    else:
      main(lp-1, lm, plus, minus, mod, K)

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

mod = 10 ** 9 + 7
plus = []
zero = []
minus = []
ans = 1

for k in range(N):
  if A[k] > 0:
    plus.append(A[k])
  elif A[k] < 0:
    minus.append(-1 * A[k])
  else:
    zero.append(A[k])
plus.sort(reverse = True)
minus.sort(reverse = True)
lp = len(plus)
lm = len(minus)

if lp == 0 and K % 2 == 1:
  for k in range(K):
    ans = ans * minus[- k - 1] % mod
  ans = -1 * ans % mod  
  print(ans)
  exit()
if lp == 0 and K % 2 == 0:
  for k in range(K):
    ans = ans * minus[k] % mod
  print(ans)
  exit()
if lm == 0:
  for k in range(K):
    ans = ans * plus[k] % mod
  print(ans)
  exit()
if len(zero) + K > N:
  print(0)
  exit()

p = 0
m = 0
for k in range(K):
  if plus[p] >= minus[m]:
    p += 1
    if p == lp:
      m = K - p  
      break
  else:
    m += 1
    if m == lm:
      p = K - m
      break

def main(mp, mm, plus, minus, mod, K):
  p = 0
  m = 0
  ans = 1
  for k in range(K):
    if plus[p] >= minus[m]:
      ans = ans * plus[p] % mod
      p += 1
      if p == mp:
        for j in range(K - p - m):
          ans = ans * minus[m + j] % mod
        break
    else:
      ans = ans * minus[m] % mod
      m += 1
      if m == mm:
        for j in range(K - p - m):
          ans = ans * plus[p + j] % mod
        break  
  print(ans)
  exit()

if m%2 == 0:
  main(lp, lm, plus, minus, mod, K)
else:
  if p == lp:
    if m != lm:
      main(lp-1, lm, plus, minus, mod, K)
    else:
      if len(zero) > 0:
        print(0)
        exit()
      else:
        main(lp, lm, plus, minus, mod, K)
  elif m == lm:
    main(lp, lm-1, plus, minus, mod, K)
  elif p == 0:
    main(lp, lm-1, plus, minus, mod, K)
  else:
    if plus[p] * plus[p-1] > minus[m-1] * minus[m]:
      main(lp, lm-1, plus, minus, mod, K)
    else:
      main(lp-1, lm, plus, minus, mod, K)

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

mod = 10**9 + 7
plus = []
zero = []
minus = []
ans = 1

for k in range(N):
  if A[k] > 0:
    plus.append(A[k])
  elif A[k] < 0:
    minus.append(-1 * A[k])
  else:
    zero.append(A[k])
plus.sort(reverse = True)
minus.sort(reverse = True)

p = 0
m = 0
for k in range(K):
  if plus[p] >= minus[m]:
    p += 1
    if p == len(plus):
      m = K - p  
      break
  else:
    m += 1
    if m == len(minus):
      p = K - m
      break

def main(mp, mm, plus, minus, mod, K):
  p = 0
  m = 0
  ans = 1
  for k in range(K):
    if plus[p] >= minus[m]:
      ans = ans * plus[p] % mod
      p += 1
      if p == mp:
        for j in range(K - p - m):
          ans = ans * minus[m + j] % mod
        m = K - p  
        break
    else:
      ans = ans * minus[m] % mod
      m += 1
      if m == mm:
        for j in range(K - p - m):
          ans = ans * plus[p + j] % mod
        p = K - m
        break  
  print(ans)



if len(zero) + K > N:
  print(0)
  #print('l23')
  exit()

if len(plus) == 0 and K % 2 == 1:
  for k in range(K):
    ans = ans * minus[-k-1] % mod
  ans = -1 * ans % mod  
  print(ans)
  #print('l31')
  exit()

if len(plus) == 0 and K % 2 == 0:
  for k in range(K):
    ans = ans * minus[k] % mod
  print(ans)
  #print('l38')
  exit()

if len(minus) == 0:
  for k in range(K):
    ans = ans * plus[k] % mod
  print(ans)
  #print('l45')
  exit()



if m%2 == 0:
  main(len(plus), len(minus), plus, minus, mod, K)
  #print('l93')
else:
  if p == len(plus) and m != len(minus):
    main(len(plus)-1, len(minus), plus, minus, mod, K)
    #print('l97')
    exit()
  elif p == len(plus) and m == len(minus):
    if len(zero) > 0:
      print(0)
      #print('l102')
    else:
      main(len(plus), len(minus), plus, minus, mod, K)
      #print('l105')
    exit()
  elif p != len(plus) and m == len(minus):
    main(len(plus), len(minus)-1, plus, minus, mod, K)
    #print('l109')
    exit()
  elif p == 0:
    main(len(plus), len(minus)-1, plus, minus, mod, K)
    #print('l113')
    exit()
  else:
    if plus[p] * plus[p-1] > minus[m-1] * minus[m]:
      main(len(plus), len(minus)-1, plus, minus, mod, K)
      #print('l118')
      exit()
    else:
      main(len(plus)-1, len(minus), plus, minus, mod, K)
      #print('l122')
      exit()

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

mod = 10**9 + 7
plus = []
zero = []
minus = []


for k in range(N):
  if A[k] > 0:
    plus.append(A[k])
  elif A[k] < 0:
    minus.append(-1 * A[k])
  else:
    zero.append(A[k])
plus.sort(reverse = True)
minus.sort(reverse = True)

p = 0
m = 0
for k in range(K):
  if plus[p] >= minus[m]:
    p += 1
    if p == len(plus):
      m = K - p  
      break
  else:
    m += 1
    if m == len(minus):
      p = K - m
      break

def main(mp, mm, plus, minus, mod, K):
  p = 0
  m = 0
  ans = 1
  for k in range(K):
    if plus[p] >= minus[m]:
      ans = ans * plus[p] % mod
      p += 1
      if p == mp:
        for j in range(K - p - m):
          ans = ans * minus[m + j] % mod
        m = K - p  
        break
    else:
      ans = ans * minus[m] % mod
      m += 1
      if m == mm:
        for j in range(K - p - m):
          ans = ans * plus[p + j] % mod
        p = K - m
        break  
  print(ans)



if len(zero) + K > N:
  print(0)
  #print('l23')
  exit()

if len(plus) == 0 and K % 2 == 1:
  for k in range(K):
    ans = ans * minus[-k-1] % mod
  ans = -1 * ans % mod  
  print(ans)
  #print('l31')
  exit()

if len(plus) == 0 and K % 2 == 0:
  for k in range(K):
    ans = ans * minus[k] % mod
  print(ans)
  #print('l38')
  exit()

if len(minus) == 0:
  for k in range(K):
    ans = ans * plus[k] % mod
  print(ans)
  #print('l45')
  exit()



if m%2 == 0:
  main(len(plus), len(minus), plus, minus, mod, K)
  #print('l93')
else:
  if p == len(plus) and m != len(minus):
    main(len(plus)-1, len(minus), plus, minus, mod, K)
    #print('l97')
    exit()
  elif p == len(plus) and m == len(minus):
    if len(zero) > 0:
      print(0)
      #print('l102')
    else:
      main(len(plus), len(minus), plus, minus, mod, K)
      #print('l105')
    exit()
  elif p != len(plus) and m == len(minus):
    main(len(plus), len(minus)-1, plus, minus, mod, K)
    #print('l109')
    exit()
  elif p == 0:
    main(len(plus), len(minus)-1, plus, minus, mod, K)
    #print('l113')
    exit()
  else:
    if plus[p] * plus[p-1] > minus[m-1] * minus[m]:
      main(len(plus), len(minus)-1, plus, minus, mod, K)
      #print('l118')
      exit()
    else:
      main(len(plus)-1, len(minus), plus, minus, mod, K)
      #print('l122')
      exit()

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

mod = 10**9 + 7
plus = []
zero = []
minus = []
ans = 1

for k in range(N):
  if A[k] > 0:
    plus.append(A[k])
  elif A[k] < 0:
    minus.append(-1 * A[k])
  else:
    zero.append(A[k])
plus.sort(reverse = True)
minus.sort(reverse = True)


if len(zero) + K > N:
  print(0)
  exit()

if len(plus) == 0 and K % 2 == 1:
  for k in range(K):
    ans = ans * minus[-k-1] % mod
  ans = -1 * ans % mod  
  print(ans)
  exit()

if len(plus) == 0 and K % 2 == 0:
  for k in range(K):
    ans = ans * minus[k] % mod
  print(ans)
  exit()

if len(minus) == 0:
  for k in range(K):
    ans = ans * plus[k] % mod
  print(ans)
  exit()

p = 0
m = 0
for k in range(K):
  if plus[p] >= minus[m]:
    ans = ans * plus[p] % mod
    p += 1
    if p = len(plus):
      for j in range(K - p - m):
        ans = ans * minus[m + j] % mod
      m = K - p  
      break
  else:
    ans = ans * minus[m] % mod
    m += 1
    if m = len(minus):
      for j in range(K - p - m):
        ans = ans * plus[p + j] % mod
      p = K - m
      break  

if m%2 == 0:
  print(ans)
else:
  if p == len(plus) and m != len(minus):
    ans = ans / plus[p-1] % mod
    ans = ans * minus[m] % mod
    print(ans)
    exit()
  elif p == len(plus) and m == len(minus):
    if len(zero) > 0:
      print(0)
    else:
      print(ans)
    exit()
  elif p != len(plus) and m == len(minus):
    ans = ans / minus[m - 1] % mod
    ans = ans * plus[p] % mod
    print(ans)
    exit()
  elif p == 0:
    ans = ans / minus[m-1] % mod
    ans = ans * plus[p] % mod
    print(ans)
    exit()
  else:
    if plus[p] * plus[p-1] > minus[m-1] * minus[m]:
      ans = ans / minus[m-1] % mod
      ans = ans * plus[p] % mod
      print(ans)
      exit()
    else:
      ans = ans / plus[p-1] % mod
      ans = ans * minus[m] % mod
      print(ans)
      exit()

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

mod = 10**9 + 7
plus = []
zero = []
minus = []
ans = 1

for k in range(N):
  if A[k] > 0:
    plus.append(A[k])
  elif A[k] < 0:
    minus.append(-1 * A[k])
  else:
    zero.append(A[k])
plus.sort(reverse = True)
minus.sort(reverse = True)


if len(zero) + K > N:
  print(0)
  exit()

if len(plus) == 0 and K % 2 == 1:
  for k in range(K):
    ans = ans * minus[-k-1] % mod
  ans = -1 * ans % mod  
  print(ans)
  exit()

if len(plus) == 0 and K % 2 == 0:
  for k in range(K):
    ans = ans * minus[k] % mod
  print(ans)
  exit()

if len(minus) == 0:
  for k in range(K):
    ans = ans * plus[k] % mod
  print(ans)
  exit()

p = 0
m = 0
for k in range(K):
  if plus[p] >= minus[m] and p + 1  <= len(plus):
    ans = ans * plus[p] % mod
    p += 1
    if p >= len(plus):
      for j in range(K - p - m):
        ans = ans * minus[m+j] % mod
      m = K - p  
      break
  else:
    ans = ans * minus[m] % mod
    m += 1
    if m >= len(minus):
      for j in range(K - p - m):
        ans = ans * plus[p+j] % mod
      p = K - m
      break  

if m%2 == 0:
  print(ans)
else:
  if p == len(plus) and m != len(minus):
    ans = ans / plus[p-1] % mod
    ans = ans * minus[m] % mod
    print(ans)
    exit()
  elif p == len(plus) and m == len(minus):
    if len(zero) > 0:
      print(0)
    else:
      print(ans)
    exit()
  elif p != len(plus) and m == len(minus):
    ans = ans / minus[m-1] % mod
    ans = ans * plus[p] % mod
    print(ans)
    exit()
  elif p == 0:
    ans = ans / minus[m-1] % mod
    ans = ans * plus[p] % mod
    print(ans)
    exit()
  else:
    if plus[p] * minus[p-1] > plus[m-1] * minus[m]:
      ans = ans / minus[m-1] % mod
      ans = ans * plus[p] % mod
      print(ans)
      exit()
    else:
      ans = ans / plus[p-1] % mod
      ans = ans * minus[m] % mod
      print(ans)
      exit()

Buggy submission (verdict: Runtime Error):
N , K = map(int, input().split())
A =list(map(int, input().split()))
mod = 10**9 + 7
count =0
for k in range(len(A)):
    if A[k] == 0:
        count +=1
        A[k] = float("inf")
if N-count < K:
    print(0)
    exit()
A.sort()
A = A[:N-count]
A.sort(reverse=True)
#print(A)
ans = 1
plus = 0
minus = -1
countp = 0
countm = 0
if A[0] >0:
    for k in range(K):
        if abs(A[plus]) >= abs(A[minus]):
            ans = ans*A[plus]%mod
            plus+=1
        else:
            ans = ans*A[minus]%mod
            if A[minus] <0:
                lasm = 0+minus
            minus-=1
            
    if minus%2 == 1:
        print(ans%mod)
    else:
        a = max(A[plus], A[minus])
        print(ans//A[lasm]*a%mod)
else:
    for k in range(K):
        ans = ans*A[k]%mod
    print(ans%mod)