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 proconio::input;
use std::ops;
use std::iter;

fn main() {
    input! {
        n: usize,
        k: usize,
        v_a: [isize; n],
    }
    let mut v_a: Vec<isize> = v_a;

    v_a.sort_by_key(|k| k.abs());
    v_a.reverse();
    let v_pos : Vec<_> = v_a.iter().filter(|&&x| x > 0).collect();
    let v_neg : Vec<_> = v_a.iter().filter_map(|&x| if x < 0 {Some(-x)} else {None}).collect();

    if v_pos.len() + v_neg.len() < k {
        println!("0");
        return
    }

    if v_pos.len() == 0 && k % 2 == 1 {
        let min = v_neg.iter().rev().take(k).fold(ModInt(MOD-1), |acc, &n| acc * ModInt(n as i64));
        println!("{}", min.0);
        return
    }

    let mut n_pos = v_pos.len().min(k);
    if ((k as isize) - (n_pos as isize)) % 2 == 1 {
        n_pos -= 1;
    }
    let mut mpos = v_pos.iter().take(0);
    let mut mneg = v_neg.iter().take(0);
    let mut mul = 0.;

    while k <= n_pos + v_neg.len() {
        let mul_pos = v_pos.iter().take(n_pos).fold(1., |acc, &&n| acc + (n as f64).log10());
        let mul_neg = v_neg.iter().take(k - n_pos).fold(1., |acc, &n| acc + (n as f64).log10());
        if mul < mul_pos + mul_neg {
            mul = mul_pos + mul_neg;
            mpos = v_pos.iter().take(n_pos);
            mneg = v_neg.iter().take(k - n_pos);
        }
        if n_pos < 2 {
            break;
        }
        n_pos -= 2;
    }

    let mul_pos = mpos.fold(ModInt(1), |acc, &&n| acc * ModInt(n as i64));
    let mul_neg = mneg.fold(ModInt(1), |acc, &n| acc * ModInt(n as i64));
    let ans = mul_pos * mul_neg;

    println!("{}", ans.0);
}

const MOD: i64 = 1_000_000_007;

#[derive(Copy, Clone, Debug, PartialEq)]
struct ModInt (i64);

impl ModInt {
    fn new(value: i64) -> Self {
        ModInt(value % MOD)
    }

    /// a を (MOD - 2) 乗した余りを求める
    /// MOD - 2: 111011100110101100101000000101
    fn resiprocal(&self) -> Self {
        let mut v_res = [ModInt::new(0); 30];

        v_res[0] = self.clone();
        for i in 1..30 {
            v_res[i] = v_res[i - 1] * v_res[i - 1]
        }
        let mut prod = ModInt::new(1);
        for &i in [0, 2, 9, 11, 14, 15, 17, 19, 20, 23, 24, 25, 27, 28, 29].iter() {
            prod = prod * v_res[i]
        }
        prod
    }

    fn pow(&self, num: u32) -> Self {
        let mut res = ModInt::new(1);
        let mut num = num;
        let mut beki = *self;
        while num > 0 {
            if num % 2 == 1 {
                res = res * beki;
            }
            beki = beki * beki;
            num = num >> 1;
        }
        res
    }
}

impl ops::Add for ModInt {
    type Output = Self;
    fn add(self, rhs: ModInt) -> Self::Output {
        ModInt::new(self.0 + rhs.0)
    }
}

impl iter::Sum for ModInt {
    fn sum<I: Iterator<Item = ModInt>>(iter: I) -> Self {
        let mut total = ModInt::new(0);
        for i in iter {
            total = total + i
        }
        total
    }
}

impl ops::Sub for ModInt {
    type Output = Self;
    fn sub(self, rhs: ModInt) -> Self::Output {
        ModInt::new(self.0 - rhs.0)
    }
}

impl ops::Mul for ModInt {
    type Output = Self;
    fn mul(self, rhs: Self) -> Self::Output {
        ModInt::new(self.0 * rhs.0)
    }
}

impl ops::Div for ModInt {
    type Output = Self;
    fn div(self, rhs: Self) -> Self::Output {
        self * rhs.resiprocal()
    }
}