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)

#[allow(unused_imports)]
use proconio::{input, marker::*};

#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

#[allow(unused_imports)]
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*).unwrap();
    }
}

#[allow(unused_imports)]
use std::cmp::{max, min, Reverse};

use self::modint::ModInt;
mod modint {
    use std;
    #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
    pub struct ModInt(usize);
    impl ModInt {
        pub fn new(value: usize) -> Self {
            ModInt(value % Self::modulo())
        }
        pub fn modulo() -> usize {
            1_000_000_007
        }
        pub fn pow(&self, n: usize) -> Self {
            let mut res = ModInt::new(1);
            let mut beki = *self;
            let mut n = n;
            while n > 0 {
                if n % 2 == 1 {
                    res *= beki;
                }
                beki *= beki;
                n /= 2;
            }
            res
        }
    }
    impl std::ops::Add for ModInt {
        type Output = Self;
        fn add(self, other: Self) -> Self {
            let mut value = self.0 + other.0;
            if value > Self::modulo() {
                value -= Self::modulo();
            }
            ModInt::new(value)
        }
    }
    impl std::ops::Add<usize> for ModInt {
        type Output = Self;
        fn add(self, other: usize) -> Self {
            let other = ModInt::new(other);
            self + other
        }
    }
    impl std::ops::Sub for ModInt {
        type Output = Self;
        fn sub(self, other: Self) -> Self {
            let mut value = self.0;
            if value < other.0 {
                value += Self::modulo();
            }
            value -= other.0;
            ModInt::new(value)
        }
    }
    impl std::ops::Sub<usize> for ModInt {
        type Output = Self;
        fn sub(self, other: usize) -> Self {
            let other = ModInt::new(other);
            self - other
        }
    }
    impl std::ops::Mul for ModInt {
        type Output = Self;
        fn mul(self, other: Self) -> Self {
            let value = self.0 * other.0 % Self::modulo();
            ModInt::new(value)
        }
    }
    impl std::ops::Mul<usize> for ModInt {
        type Output = Self;
        fn mul(self, other: usize) -> Self {
            let other = ModInt::new(other);
            self * other
        }
    }
    impl std::ops::Mul<ModInt> for usize {
        type Output = ModInt;
        fn mul(self, other: ModInt) -> Self::Output {
            let value = ModInt::new(self);
            value * other
        }
    }
    impl std::ops::Div for ModInt {
        type Output = Self;
        fn div(self, other: Self) -> Self {
            let mut exp = Self::modulo() - 2;
            let mut value = self.0;
            let mut ovalue = other.0;
            while exp > 0 {
                if exp % 2 == 1 {
                    value = value * ovalue % Self::modulo();
                }
                ovalue = ovalue * ovalue % Self::modulo();
                exp /= 2;
            }
            ModInt::new(value)
        }
    }
    impl std::ops::Div<usize> for ModInt {
        type Output = Self;
        fn div(self, other: usize) -> Self {
            let other = ModInt::new(other);
            self / other
        }
    }
    impl std::ops::AddAssign for ModInt {
        fn add_assign(&mut self, other: Self) {
            *self = *self + other;
        }
    }
    impl std::ops::AddAssign<usize> for ModInt {
        fn add_assign(&mut self, other: usize) {
            *self = *self + other;
        }
    }
    impl std::ops::SubAssign for ModInt {
        fn sub_assign(&mut self, other: Self) {
            *self = *self - other;
        }
    }
    impl std::ops::SubAssign<usize> for ModInt {
        fn sub_assign(&mut self, other: usize) {
            *self = *self - other;
        }
    }
    impl std::ops::MulAssign for ModInt {
        fn mul_assign(&mut self, other: Self) {
            *self = *self * other;
        }
    }
    impl std::ops::MulAssign<usize> for ModInt {
        fn mul_assign(&mut self, other: usize) {
            *self = *self * other;
        }
    }
    impl std::ops::DivAssign for ModInt {
        fn div_assign(&mut self, other: Self) {
            *self = *self / other;
        }
    }
    impl std::ops::DivAssign<usize> for ModInt {
        fn div_assign(&mut self, other: usize) {
            *self = *self / other;
        }
    }
    impl std::fmt::Debug for ModInt {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            write!(f, "{}", self.0)
        }
    }
    impl std::fmt::Display for ModInt {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            write!(f, "{}", self.0)
        }
    }
}

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

    if a.iter().all(|&e| e <= 0) {
        a.sort();
        let mut res = ModInt::new(1);
        if k % 2 == 0 {
            for e in a.into_iter().take(k) {
                res *= e.abs() as usize;
            }
        } else {
            for e in a.into_iter().rev().take(k) {
                res *= (e + ModInt::modulo() as isize) as usize;
            }
        }
        println!("{}", res);
        return;
    }

    let mut minus = Vec::new();
    let mut plus = Vec::new();

    for e in a {
        if e < 0 {
            minus.push(e.abs() as usize);
        } else if e > 0 {
            plus.push(e as usize);
        }
    }

    if minus.len() + plus.len() < k || (minus.len() + plus.len() == k && plus.len() % 2 != k % 2) {
        println!("0");
        return;
    }

    minus.sort_by_key(|&e| Reverse(e));
    plus.sort_by_key(|&e| Reverse(e));

    let p = if plus.len() % 2 == k % 2 {
        min(plus.len(), k)
    } else {
        min(plus.len() - k % 2, k)
    };

    let mut res = ModInt::new(1);

    for i in 0..p {
        res *= plus[i];
    }

    for i in 0..k - p {
        res *= minus[i];
    }

    for i in (k - p..min(minus.len(), k)).step_by(2) {
        if i + 1 >= min(minus.len(), k) {
            break;
        }
        let mt = minus[i] * minus[i + 1];
        let pt = plus[k - i - 1] * plus[k - i - 2];
        if mt <= pt {
            break;
        }
        res *= mt;
        res /= pt;
    }
    println!("{}", res);
}