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 std::cmp;
use std::fmt::Debug;
use std::str::FromStr;
#[allow(dead_code)]
fn read_as_vec<T>() -> Vec<T>
where
    T: FromStr,
    <T as FromStr>::Err: Debug,
{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim()
        .split_whitespace()
        .map(|c| T::from_str(c).unwrap())
        .collect()
}

#[allow(dead_code)]
fn read_as_string() -> String {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

fn main() {
    let line: Vec<usize> = read_as_vec();
    let (n, k) = (line[0], line[1]);
    let mm = 1_000_000_007;
    let mut a: Vec<i64> = read_as_vec();
    if a.iter().filter(|x| **x != 0).count() < k {
        println!("0");
        return;
    }

    let has0 = a.contains(&0);
    let mut mv: Vec<i64> = a
        .clone()
        .into_iter()
        .filter(|x| *x < 0)
        .collect::<Vec<i64>>();
    let mut pv: Vec<i64> = a
        .clone()
        .into_iter()
        .filter(|x| *x > 0)
        .collect::<Vec<i64>>();
    mv.sort();
    pv.sort();
    pv.reverse();

    if mv.len() == 0 {
        let mut ans = 1;
        for i in 0..k {
            ans = (ans * pv[i]) % mm;
        }
        println!("{}", ans);
        return;
    }

    if pv.len() == 0 && k % 2 == 1 {
        if has0 {
            println!("0");
            return;
        } else {
            mv.reverse();
            let mut ans: i64 = 1;
            for i in 0..k {
                ans = (ans * mv[i]) % mm;
            }
            if ans < 0 {
                ans += mm;
            }
            println!("{}", ans);
            return;
        }
    }

    let mut mv_acc: Vec<i64> = vec![1; mv.len() + 1];
    let mut acc = 1;
    for i in 0..mv.len() {
        mv_acc[i] = acc;
        acc = (acc * mv[i].abs()) % mm;
    }
    *mv_acc.last_mut().unwrap() = acc;
    let mut pv_acc: Vec<i64> = vec![1; pv.len() + 1];
    let mut acc: i64 = 1;
    for i in 0..pv.len() {
        pv_acc[i] = acc;
        acc = (acc * pv[i]) % mm;
    }
    *pv_acc.last_mut().unwrap() = acc;

    let mut ans = 0;
    for i in 0..=k {
        let nm = i;
        let np = k - i;
        if nm % 2 == 1 {
            continue;
        }
        if nm >= mv_acc.len() || np >= pv_acc.len() {
            continue;
        }
        ans = cmp::max(ans, (pv_acc[np] * mv_acc[nm]) % mm);
    }
    println!("{}", ans);
}