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 Rust (1.42.0) (judge verdict: Accepted)

fn main() {
    proconio::input! {
        n: usize,
        mut k: usize,
        a: [i32;n],
    }
    let mut x=vec![];
    let mut y=vec![];
    let mut z=0;
    for a in a {
        if a>0 {
            x.push(a as u64);
        }
        else if a<0 {
            y.push((-a)as u64);
        }
        else {
            z+=1;
        }
    }
    x.sort();
    y.sort();
    let mut r;
    if x.len()>0 {
        if x.len()+y.len()<k {
            r=0;
        }
        else if x.len()+y.len()==k {
            let odd=y.len()&1==1;
            if z>0 && odd {
                r=0;
            }
            else {
                r=1;
                for a in x {
                    r=r*a%MOD;
                }
                for a in y {
                    r=r*a%MOD;
                }
                if odd {
                    r=(MOD-r)%MOD;
                }
            }
        }
        else {
            r=1;
            if k&1==1 {
                r=x.pop().unwrap();
                k-=1;
            }
            let mut i=x.len();
            let mut j=y.len();
            while k>0 {
                let f = if i>1 && j>1 {
                    x[i-2]*x[i-1]>y[j-2]*y[j-1]
                }
                else {
                    i>1
                };
                if f {
                    r=x[i-2]*x[i-1]%MOD*r%MOD;
                    x.pop();x.pop();
                    i-=2;
                }
                else {
                    r=y[j-2]*y[j-1]%MOD*r%MOD;
                    y.pop();y.pop();
                    j-=2;
                }
                k-=2;
            }
        }
    }
    else {
        if y.len()<k || (k&1==1 && z>0) {
            r=0;
        }
        else if k&1==1 {
            r=1;
            for &a in &y[0..k] {
                r=r*a%MOD;
            }
            r=(MOD-r)%MOD;
        }
        else {
            y.reverse();
            r=1;
            for &a in &y[0..k] {
                r=r*a%MOD;
            }
        }
    }
    println!("{}",r);
}
const MOD: u64=1000000007;