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 Haskell (GHC 8.8.3) (judge verdict: Wrong Answer)

-- https://github.com/minoki/my-atcoder-solutions
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeOperators #-}
import Data.Char (isSpace)
import Data.Int (Int64)
import Data.List (unfoldr)
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Unboxed.Mutable as UM
import qualified Data.Vector.Algorithms.Merge as A
import qualified Data.ByteString.Char8 as BS
import GHC.TypeNats (Nat, KnownNat, natVal, type (^), type (+))

solve :: U.Vector Int64 -> U.Vector Int64 -> Int -> (IntMod (10^9 + 7), Bool)
solve n p k = loop (U.toList n) (U.toList p) k 1
  where
    loop :: [Int64] -> [Int64] -> Int -> IntMod (10^9 + 7) -> (IntMod (10^9 + 7), Bool)
    loop [] ps !l !acc = (acc * product (map fromIntegral $ take l ps), False)
    loop ns [] !l !acc = let m = length ns
                             !acc' | odd l = acc * product (map fromIntegral $ drop (m - l) ns)
                                   | otherwise = acc * product (map fromIntegral $ take l ns)
                         in (acc', odd l)
    loop ns@(n0:n1:ns') ps@(p0:ps'@(p1:_)) !l !acc
      | n0 * n1 >= p0 * p1 = loop ns' ps (l - 2) (acc * fromIntegral (n0 * n1))
      | otherwise = loop ns ps' (l - 1) (acc * fromIntegral p0)
    loop [n0] ps !l !acc = let m = length ps
                           in if m < l then
                                (acc * fromIntegral n0 * product (map fromIntegral ps), True)
                              else
                                (acc * product (map fromIntegral $ take l ps), False)
    loop ns [p0] !l !acc | odd l = (acc * product (map fromIntegral $ take (l - 1) ns) * fromIntegral p0, False)
                         | otherwise = let m = length ns
                                       in if m >= l then
                                            (acc * product (map fromIntegral $ take l ns), False)
                                          else
                                            (acc * product (map fromIntegral ns) * fromIntegral p0, True)

main = do
  [n,k] <- unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
  as <- U.map fromIntegral . U.modify A.sort . U.unfoldrN n (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
  let n, nn, z, p :: U.Vector Int64
      (n, nn) = U.span (< 0) as
      (z, p) = U.span (== 0) nn
  if U.length n + U.length p < k then
    putStrLn "0"
  else do
    let (v, neg) = solve n (U.reverse p) k
    if neg && not (U.null z) then
      putStrLn "0"
    else
      print v

--
-- Modular Arithmetic
--

-- type N = IntMod (10^9 + 7)

newtype IntMod (m :: Nat) = IntMod { unwrapN :: Int64 } deriving (Eq)

instance Show (IntMod m) where
  show (IntMod x) = show x

instance KnownNat m => Num (IntMod m) where
  t@(IntMod x) + IntMod y
    | x + y >= modulus = IntMod (x + y - modulus)
    | otherwise = IntMod (x + y)
    where modulus = fromIntegral (natVal t)
  t@(IntMod x) - IntMod y
    | x >= y = IntMod (x - y)
    | otherwise = IntMod (x - y + modulus)
    where modulus = fromIntegral (natVal t)
  t@(IntMod x) * IntMod y = IntMod ((x * y) `rem` modulus)
    where modulus = fromIntegral (natVal t)
  fromInteger n = let result = IntMod (fromInteger (n `mod` fromIntegral modulus))
                      modulus = natVal result
                  in result
  abs = undefined; signum = undefined

{-# RULES
"^9/Int" forall x. x ^ (9 :: Int) = let u = x; v = u * u * u in v * v * v
"^9/Integer" forall x. x ^ (9 :: Integer) = let u = x; v = u * u * u in v * v * v
 #-}

fromIntegral_Int64_IntMod :: KnownNat m => Int64 -> IntMod m
fromIntegral_Int64_IntMod n = result
  where
    result | 0 <= n && n < modulus = IntMod n
           | otherwise = IntMod (n `mod` modulus)
    modulus = fromIntegral (natVal result)

{-# RULES
"fromIntegral/Int->IntMod" fromIntegral = fromIntegral_Int64_IntMod . (fromIntegral :: Int -> Int64) :: Int -> IntMod (10^9 + 7)
"fromIntegral/Int64->IntMod" fromIntegral = fromIntegral_Int64_IntMod :: Int64 -> IntMod (10^9 + 7)
 #-}