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: Wrong Answer)

//use itertools::Itertools;
use std::cmp;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::BinaryHeap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::io::Read;
use std::usize::MAX;

macro_rules! input {(source = $s:expr, $($r:tt)*) => {let mut iter = $s.split_whitespace();let mut next = || { iter.next().unwrap() };input_inner!{next, $($r)*}};($($r:tt)*) => {let stdin = std::io::stdin();let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));let mut next = move || -> String{bytes.by_ref().map(|r|r.unwrap() as char).skip_while(|c|c.is_whitespace()).take_while(|c|!c.is_whitespace()).collect()};input_inner!{next, $($r)*}};}
macro_rules! input_inner {($next:expr) => {};($next:expr, ) => {};($next:expr, $var:ident : $t:tt $($r:tt)*) => {let $var = read_value!($next, $t);input_inner!{$next $($r)*}};}
macro_rules! read_value {($next:expr, ( $($t:tt),* )) => {( $(read_value!($next, $t)),* )};($next:expr, [ $t:tt ; $len:expr ]) => {(0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()};($next:expr, chars) => {read_value!($next, String).chars().collect::<Vec<char>>()};($next:expr, usize1) => {read_value!($next, usize) - 1};($next:expr, [ $t:tt ]) => {{let len = read_value!($next, usize);(0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()}};($next:expr, $t:ty) => {$next().parse::<$t>().expect("Parse error")};}

fn solve() {
    input! {
        n: usize,
        k: usize,
        a: [isize;n]
    }

    let mut plus = vec![];
    let mut minus = vec![];
    let mut zero_count = 0;

    for i in 0..n {
        if a[i] > 0 {
            plus.push(a[i]);
        } else if a[i] < 0 {
            minus.push(a[i]);
        } else {
            zero_count += 1;
        }
    }

    if plus.len() + minus.len() < k {
        println!("{}", 0);
        return;
    }

    plus.sort();
    plus.reverse();
    minus.sort();
    minus.reverse();
    let mut plus_cum_multi = vec![1; plus.len() + 1];
    let mut minus_cum_multi = vec![1; minus.len() + 1];
    for i in 1..plus.len() + 1 {
        plus_cum_multi[i] = (plus_cum_multi[i - 1] * plus[i - 1]) % LARGE_PRIME;
    }
    for i in 1..minus.len() + 1 {
        minus_cum_multi[i] = (minus_cum_multi[i - 1] * minus[i - 1]) % LARGE_PRIME;
    }

    //println!("{:?},{:?}", plus_cum_multi, minus_cum_multi);

    let mut ans = 0;

    if plus.len() > 0 {
        let mut minus_consume = 0;
        while minus_consume < k + 1 {
            if k - minus_consume > plus.len() || minus_consume > minus.len() {
            } else {
                ans = cmp::max(
                    ans,
                    (plus_cum_multi[k - minus_consume] * minus_cum_multi[minus_consume])
                        % LARGE_PRIME,
                );
            }
            //println!("{} {}", minus_consume, k - minus_consume);
            minus_consume += 2;
        }

        println!("{}", (ans + LARGE_PRIME) % LARGE_PRIME);
    } else {
        ans = (minus_cum_multi[k] + LARGE_PRIME) % LARGE_PRIME;
        println!("{}", ans);
    }
}

fn main() {
    let thd = std::thread::Builder::new().stack_size(104_857_600);
    thd.spawn(|| solve()).unwrap().join().unwrap();

    /*
    // 入力を一括で読み込む場合
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    let mut input = buf.split_whitespace();

    // inputに対しnext()で読み込んでいく
    let q: usize = input.next().unwrap().parse().unwrap();
    */
}

const LARGE_PRIME: isize = 1_000_000_007;

// @〜でsnippet