Problem:
Score : 500 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

We will choose exactly K of these elements. Find the maximum possible product of the chosen elements.

Then, print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Constraints

1 \leq K \leq N \leq 2\times 10^5

|A_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 \ldots A_N

Output

Print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Sample Input 1

4 2
1 2 -3 -4

Sample Output 1

12

The possible products of the two chosen elements are 2, -3, -4, -6, -8, and 12, so the maximum product is 12.

Sample Input 2

4 3
-1 -2 -3 -4

Sample Output 2

1000000001

The possible products of the three chosen elements are -24, -12, -8, and -6, so the maximum product is -6.

We print this value modulo (10^9+7), that is, 1000000001.

Sample Input 3

2 1
-1 1000000000

Sample Output 3

1000000000

The possible products of the one chosen element are -1 and 1000000000, so the maximum product is 1000000000.

Sample Input 4

10 10
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1

Sample Output 4

999983200

Be sure to print the product modulo (10^9+7).

Solution in Ruby (2.7.1) (judge verdict: Wrong Answer)

#!/usr/bin/env ruby

require 'set'

class Hash
  def push(key, value)
    self[key] = [] if self[key] == nil
    self[key].push(value)
  end
end

class Array
  def lower_bound(value)
    left = -1;
    right = self.length;
    while left + 1 < right
      mid = left + (right - left) / 2;
      if self[mid] >= value
        right = mid
      else
        left = mid
      end
    end
    right
  end

  def unique
    res = [self.first]
    each_cons(2) do |a, b|
      if a != b
        res.push(b)
      end
    end
    res
  end
end

def get_ints
  gets.chomp.split.map(&:to_i)
end

def get_ints_minus_one
  get_ints.map { |x| x - 1 }
end

def get_int
  gets.chomp.to_i
end

# mod m での aの逆元
def modinv(a, m)
  b = m; u = 1; v = 0

  while b != 0
    t = a / b
    a -= t * b
    c = a; a = b; b = c
    u -= t * v
    c = u; u = v; v = c
  end
  u %= m
  if u < 0
    u += m
  else
    u
  end
end

N, K = get_ints
A = get_ints

MOD = 10 ** 9 + 7

ax = A.filter { |a| a != 0 }.sort_by { |x| x.abs  }.reverse

fu_only = ax.all? { |x| x < 0 }

if ax.length < K
  puts 0
elsif fu_only && K % 2 == 1
  if ax.length != N
    puts 0
  else
    # 絶対値が小さい順に変更
    ax = ax.reverse
    sum = 1
    ax[0...K].each do |a|
      sum = (sum * a.abs) % MOD
    end
    puts MOD - sum
  end
else
  fu_count = ax[0...K].count { |x| x < 0 }
  if fu_count % 2 == 1
    last_minus = ax[0...K].filter { |x| x < 0 }.last
    last_plus = ax[0...K].filter { |x| x > 0 }.last
    first_minus = ax[K..].find { |x| x < 0 }
    first_plus = ax[K..].find { |x| x > 0 }

    last_minus_come = false
    sum_without_last_minus = 1
    last_plus_come = false
    sum_without_last_plus = 1
    ax[0...K].each do |a|
      fu = a < 0
      if a == last_minus && !last_minus_come
        last_minus_come = true
        sum_without_last_plus = sum_without_last_plus * a.abs % MOD
      elsif a == last_plus && !last_plus_come
        last_plus_come = true
        sum_without_last_minus = sum_without_last_minus * a.abs % MOD
      else
        sum_without_last_plus = sum_without_last_plus * a.abs % MOD
        sum_without_last_minus = sum_without_last_minus * a.abs % MOD
      end
    end
    sum_without_last_minus = sum_without_last_minus * first_minus.abs  if first_minus
    sum_without_last_plus = sum_without_last_plus * first_plus if first_plus
    puts [sum_without_last_minus, sum_without_last_plus].max % MOD
  else
    sum = 1
    ax[0...K].each do |a|
      sum = sum * a.abs % MOD
    end
    puts sum
  end
  # sum = 1
  # last_minus = nil
  # last_plus = nil
  # ax[0...K].each do |a|
  #   kake = sum * a
  #   last_plus = a if a > 0
  #   last_minus = a if a < 0
  #   sum = (sum * a).abs % MOD
  #   sum = - sum if kake < 0
  # end
  #
  # if sum < 0
  #   sum_abs = sum.abs
  #   # K以降に負があるかをチェック
  #   fu = ax[K..].find { |x| x < 0 }
  #
  #   if fu.nil?
  #     minv = modinv(-last_minus, MOD)
  #     sum = (sum_abs * minv) % MOD # 負の最後の要素を割る
  #     sum *= ax[K]
  #   else
  #     kouho1 = -10**9
  #     if last_minus
  #       minv = modinv(-last_minus, MOD)
  #       kouho1 = (sum_abs * minv) % MOD # 負の最後の要素を割る
  #       first_plus = ax[K..].find { |x| x > 0 }
  #       kouho1 *= kouho1 * first_plus if first_plus
  #     end
  #
  #     kouho2 = -10**9
  #     if last_plus
  #       minv2 = modinv(last_plus, MOD)
  #       kouho2 = (sum_abs * minv2) % MOD # 正の最後の要素を割る
  #       first_minus = ax[K..].find { |x| x < 0 }
  #       kouho2 *= (first_minus).abs if first_minus
  #     end
  #
  #     sum = [kouho1, kouho2].max
  #     sum %= MOD
  #   end
  # end
  #
  # puts sum
end