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 OCaml (4.10.0) (judge verdict: Wrong Answer)

Scanf.scanf "%d %d" (fun n k ->
    let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun a -> a)) in
    Array.sort (fun a b -> compare (abs a) (abs b)) a;

    let z = 1_000_000_007 in

    let ( *@) a b = (((a * b) mod z) + z) mod z in

    let rec loop rest i m z p minm minp =
        if rest = 0 then m, z, p, minm, minp else
            let m = if a.(i) < 0 then m + 1 else m in
            let z = if a.(i) = 0 then z + 1 else z in
            let p = if a.(i) > 0 then p + 1 else p in
            loop (rest - 1) (i - 1) m z p (if a.(i) < 0 then i else minm) (if a.(i) > 0 then i else minp)
    in
    let m, z, p, minm, minp = loop k (n - 1) 0 0 0 n n in
    let prod f t =
        let rec loop i acc =
            if i > t then acc else loop (i + 1) (acc *@ a.(i))
        in
        loop f 1
    in
    let prod2 f t skip alter =
        let rec loop i acc =
            if i > t then acc else
                let acc = if i = skip then acc else acc *@ a.(i) in
                loop (i + 1) acc
        in
        loop f a.(alter)
    in
    Printf.printf "%d\n" @@ if z > 0 then 0 else ( (* z = 0 *)
        if k = n then prod 0 (n - 1) else
        if m mod 2 = 0 then prod (n - k) (n - 1) else ( (* z = 0 && m % 2 = 1 *)
            let rec loop i maxm maxp zero =
                if i = n - k then maxm, maxp, zero else
                    if a.(i) > 0 then loop (i + 1) maxm i zero else
                    if a.(i) < 0 then loop (i + 1) i maxp zero else
                                      loop (i + 1) maxm maxp true
            in
            let maxm, maxp, zero = loop 0 (-1) (-1) false in
            (* case:
                * p > 0 && m % 2 = 1:
                    * maxm >= 0 && maxp >= 0 -> compare prod / a.(minm) * a.(maxp)
                                                        prod / a.(minp) * a.(maxm)
                    * maxm >= 0 && maxp <  0 -> prod / a.(minp) * a.(maxm)
                    * maxm <  0 && maxp >= 0 -> prod / a.(minm) * a.(maxp)
                    * else -> 0
                * p = 0 && m % 2 = 1 && zero -> 0
                * p = 0 && m % 2 = 1 && !zero -> phase2
             *)
            if p > 0 then (
                if maxm >= 0 && maxp >= 0 then (
                    if a.(maxp) * a.(minp) > a.(maxm) * a.(minm) then prod2 (n - k) (n - 1) minm maxp
                                                                 else prod2 (n - k) (n - 1) minp maxm
                ) else
                if maxm >= 0 && maxp <  0 then prod2 (n - k) (n - 1) minp maxm else
                if maxm <  0 && maxp >= 0 then prod2 (n - k) (n - 1) minm maxp else 0
            ) else if zero then 0 else prod 0 (k - 1)
        )
    )
)