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: Runtime Error)

use std::io::stdin;
use std::str::FromStr;

const M: i64 = 1_000_000_007;

fn main(){
    let (n, k): (usize, usize) = get_pair();
    let mut xs: Vec<i64> = get_vec();

    if n == k { 
        println!("{}", xs.iter().fold(1, |acc, x| acc * (x + M) % M))
    } else if xs.iter().fold(true, |acc, &x| acc && (x < 0)) {
        xs.sort_by(|a, b| b.cmp(a));
        println!("{}", xs.iter().take(k).fold(1, |acc, x| acc * (x + M) % M));
    } else {
        xs.sort_by(|a, b| b.abs().cmp(&a.abs()));
        let ps = xs.iter().filter(|&x| x >= &0);
        let ns = xs.iter().filter(|&x| x < &0);

        let mut zs = Vec::new();
        let mut stock = None;
        for p in ps {
            if let Some(s) = stock.clone() {
                zs.push((s * p % M, Some(s)));
                stock = None;
            } else {
                stock = Some(*p);
            }
        }
        if let Some(s) = stock {
            zs.push((0, Some(s)))
        }
        for n in ns {
            if let Some(s) = stock.clone() {
                zs.push((s * n % M, None));
                stock = None;
            } else {
                stock = Some(*n);
            }
        }

        zs.sort_by(|a, b| b.0.cmp(&a.0));

        let mut a = zs.iter().take(k/2).fold(1, |acc, &z| acc * z.0 % M);
        if k % 2 == 1 {
            let nx = zs.iter().skip(k/2).filter_map(|(_, s)| s.clone()).next().unwrap();
            a = a * nx % M;
        }

        println!("{}", a);
    }
}

#[allow(dead_code)]
fn get_line() -> String {
    let mut s = String::new();
    match stdin().read_line(&mut s){
        Ok(_) => {s.trim().to_string()}
        Err(_) => String::new()
    }
}

#[allow(dead_code)]
fn get_vec<T: std::str::FromStr>() -> Vec<T> {
    let line = get_line();
    line.split_whitespace().filter_map(|x| x.parse().ok()).collect()
}

#[allow(dead_code)]
fn get_one<T: FromStr + Copy>() -> T {
    let v = get_vec();
    v[0]
}

#[allow(dead_code)]
fn get_pair<T: FromStr + Copy>() -> (T, T) {
    let v = get_vec();
    (v[0], v[1])
}

#[allow(dead_code)]
fn get_triple<T: FromStr + Copy>() -> (T, T, T) {
    let v = get_vec();
    (v[0], v[1], v[2])
}

#[allow(dead_code)]
fn get_chars() -> Vec<char> {
    get_line().chars().collect()
}

#[allow(dead_code)]
fn vec_min(xs: &Vec<i64>) -> i64 {
    xs.iter().map(|&x|x).fold(std::i64::MAX, std::cmp::min)
}

#[allow(dead_code)]
fn vec_max(xs: &Vec<i64>) -> i64 {
    xs.iter().map(|&x|x).fold(std::i64::MIN, std::cmp::max)
}

#[allow(dead_code)]
fn vec_sum(xs: &Vec<i64>) -> i64 {
    xs.iter().fold(0, |acc, &x| acc+x)
}