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
  real(8) :: nn = -1.d0, pp = -1.d0
  read(*,*) n, k
  read(*,*) a(1:n, 1)
  if (n == k) then
    do i = 1, n
      x = modulo(x * a(i, 1), m)
    end do
    write(*,'(i0)') x
    stop
  end if
  call merge_sort(a(1:n, :))
  if (all(a(1:n, 1) <= 0)) then
    if (mod(k, 2) == 0) then
      do i = 1, k
        x = modulo(x * a(n - i + 1, 1), m)
      end do
    else
      do i = 1, k
        x = modulo(x * a(i, 1), m)
      end do
    end if
    write(*,'(i0)') x
    stop
  end if
  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)
        pp = a(i, 1) / real(posi, 8)
        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)
        nn = a(i, 1) / real(nega, 8)
        exit
      end if
    end do
  end if
  write(*,'(i0)') merge(y, z, pp > nn)
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