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: Accepted)

#[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 from_isize(value: isize) -> Self {
            Self::new(if value < 0 {
                (Self::modulo() as isize + value) as usize
            } else {
                value as usize
            })
        }
        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::Add<isize> for ModInt {
        type Output = Self;
        fn add(self, other: isize) -> Self {
            let other = ModInt::from_isize(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::Sub<isize> for ModInt {
        type Output = Self;
        fn sub(self, other: isize) -> Self {
            let other = ModInt::from_isize(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<isize> for ModInt {
        type Output = Self;
        fn mul(self, other: isize) -> Self {
            let other = ModInt::from_isize(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::Div<isize> for ModInt {
        type Output = Self;
        fn div(self, other: isize) -> Self {
            let other = ModInt::from_isize(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::AddAssign<isize> for ModInt {
        fn add_assign(&mut self, other: isize) {
            *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::SubAssign<isize> for ModInt {
        fn sub_assign(&mut self, other: isize) {
            *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::MulAssign<isize> for ModInt {
        fn mul_assign(&mut self, other: isize) {
            *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::ops::DivAssign<isize> for ModInt {
        fn div_assign(&mut self, other: isize) {
            *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 n == k {
        let mut res = ModInt::new(1);
        for e in a {
            res *= if e < 0 {
                (ModInt::modulo() as isize + e) as usize
            } else {
                e as usize
            };
        }
        println!("{}", res);
        return;
    }

    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
            }
        } else {
            for e in a.into_iter().rev().take(k) {
                res *= e
            }
        }
        println!("{}", res);
        return;
    }

    a.sort_by_key(|e| Reverse(e.abs()));

    let mut minus = 0usize;
    let mut res = ModInt::new(1);
    for i in 0..k {
        if a[i] < 0 {
            minus += 1;
        }
        res *= a[i];
    }

    if minus % 2 == 0 {
        println!("{}", res);
        return;
    }

    let mut p1 = None;
    let mut m1 = None;
    for i in 0..k {
        if a[i] < 0 {
            m1 = Some(a[i]);
        } else if a[i] > 0 {
            p1 = Some(a[i]);
        }
    }

    let mut p2 = None;
    let mut m2 = None;
    for i in (k..n).rev() {
        if a[i] < 0 {
            m2 = Some(a[i]);
        } else if a[i] > 0 {
            p2 = Some(a[i]);
        }
    }

    let pm1 = match (p1, m2) {
        (Some(p1), Some(m2)) => Some((p1, m2)),
        _ => None,
    };
    let pm2 = match (p2, m1) {
        (Some(p2), Some(m1)) => Some((p2, m1)),
        _ => None,
    };

    match (pm1, pm2) {
        (Some((p1, m2)), Some((p2, m1))) => {
            if m2.abs() * m1.abs() < p1 * p2 {
                res /= m1;
                res *= p2;
            } else {
                res /= p1;
                res *= m2;
            }
        }
        (Some((p1, m2)), None) => {
            res /= p1;
            res *= m2;
        }
        (None, Some((p2, m1))) => {
            res /= m1;
            res *= p2;
        }
        _ => (),
    }
    println!("{}", res);
}