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 Fortran (GNU Fortran 9.2.1) (judge verdict: Wrong Answer)

program multiplication_4
  implicit none
  integer(8), parameter :: m = 1000000007
  integer :: n, k, i, j, cnt = 0
  integer(8) :: a(200000, 2) = 0, nega = -1, posi = -1, x = 1, y = -1, z = -1
  read(*,*) n, k
  read(*,*) a(1:n, 1)
  do i = 1, n
    if (a(i, 1) >= 0) then
      a(i, 2) = 0
    else
      a(i, 1) = -a(i, 1)
      a(i, 2) = 1
    end if
  end do
  call merge_sort(a(1:n, :))
  do i = 1, k
    x = mod(x * a(i, 1), m)
    if (a(i, 2) == 1) then
      cnt = cnt + 1
      nega = a(i, 1)
    else
      posi = a(i, 1)
    end if
  end do
  if (x == 0 .or. mod(cnt, 2) == 0) then
    write(*,'(i0)') x
    stop
  end if
  if (posi /= -1) then
    do i = k + 1, n
      if (a(i, 2) == 1) then
        y = mod(x * a(i, 1), m)
        y = mod(y * inv(posi), m)
        exit
      end if
    end do
  end if
  if (nega /= -1) then
    do i = k + 1, n
      if (a(i, 2) == 0) then
        z = mod(x * a(i, 1), m)
        z = mod(z * inv(nega), m)
        exit
      end if
    end do
  end if
  if (y /= -1 .or. z /= -1) then
    write(*,'(i0)') max(y, z)
    stop
  end if
  j = n
  do while (j >= 1 .and. a(j, 1) == 0)
    j = j - 1
  end do
  if (n - j >= k .or. j - k + 1 < 1) then
    write(*,'(i0)') 0
    stop
  end if
  x = 1
  do i = j, j - k + 1, -1
    x = mod(x * a(i, 1), m)
  end do
  write(*,'(i0)') modulo(-x, m)
contains
  subroutine merge_sort(a)
    integer(8), intent(inout) :: a(:, :)
    integer :: n, m, l, i, u
    n = size(a, 1)
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m / 2
        u = min(2 * i * l, n)
        call merger(a(2 * (i - 1) * l + 1:(2 * i - 1) * l, :), a((2 * i - 1) * l + 1:u, :))
      end do
      l = 2 * l
      m = (m + 1) / 2
    end do
  end
  subroutine merger(a1, a2)
    integer(8), intent(inout) :: a1(:, :), a2(:, :)
    integer(8) :: a(size(a1, 1) + size(a2, 1), size(a1, 2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(a1, 1)
    n2 = size(a2, 1)
    do while (i1 <= n1 .and. i2 <= n2)
      if (a1(i1, 1) > a2(i2, 1) .or. &
      (a1(i1, 1) == a2(i2, 1) .and. a1(i1, 2) <= a2(i2, 2))) then
        a(i1+i2-1, :) = a1(i1, :)
        i1 = i1+1
      else
        a(i1+i2-1, :) = a2(i2, :)
        i2 = i2+1
       end if
    end do
    if (i1 <= n1) then
      a(i1 + i2 - 1:n1 + n2, :) = a1(i1:n1, :)
    else
      a(i1 + i2 - 1:n1 + n2, :) = a2(i2:n2, :)
    end if
    a1 = a(1:n1, :)
    a2 = a(n1 + 1:n1 + n2, :)
  end
  pure elemental integer(8) function inv(v) result(res)
    integer(8), intent(in) :: v
    integer(8) :: a, b, c, x, y, z
    a = v
    b = m
    c = 0
    res = 1
    do while (b /= 0)
      x = b
      y = mod(a, b)
      z = res - a / b * c
      res = c
      a = x
      b = y
      c = z
    end do
    res = modulo(res, m)
  end
end program multiplication_4