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::fmt::Debug;
use std::str::FromStr;

pub struct TokenReader {
    reader: std::io::Stdin,
    tokens: Vec<String>,
    index: usize,
}

impl TokenReader {
    pub fn new() -> Self {
        Self {
            reader: std::io::stdin(),
            tokens: Vec::new(),
            index: 0,
        }
    }

    pub fn next<T>(&mut self) -> T
    where
        T: FromStr,
        T::Err: Debug,
    {
        if self.index >= self.tokens.len() {
            self.load_next_line();
        }
        self.index += 1;
        self.tokens[self.index - 1].parse().unwrap()
    }

    pub fn vector<T>(&mut self) -> Vec<T>
    where
        T: FromStr,
        T::Err: Debug,
    {
        if self.index >= self.tokens.len() {
            self.load_next_line();
        }
        self.index = self.tokens.len();
        self.tokens.iter().map(|tok| tok.parse().unwrap()).collect()
    }

    pub fn load_next_line(&mut self) {
        let mut line = String::new();
        self.reader.read_line(&mut line).unwrap();

        self.tokens = line
            .split_whitespace()
            .map(String::from)
            .collect();
        self.index = 0;
    }
}

fn euclid(a: i64, b: i64) -> (i64, i64) {
    if b == 0 {
        return (1, 0);
    }

    let p = euclid(b, a % b);
    (p.1, p.0 - a / b * p.1)
}

fn inv_mod(num: i64, the_mod: i64) -> i64 {
    let mut inv = euclid(num, the_mod).0;
    while inv < 0 {
        inv += the_mod;
    }
    inv
}

fn solve(vec: &mut Vec<i64>, k: usize) -> i64 {
    const MOD: i64 = 1e9 as i64 + 7;
    let mut has_zero = false;
    let mut has_pos = false;

    for &num in vec.iter() {
        if num == 0 {
            has_zero = true;
        } else if num > 0 {
            has_pos = true;
        }
    }

    if k % 2 == 1 && !has_pos {
        if has_zero {
            return 0;
        }

        vec.sort();
        vec.reverse();

        let mut res = 1;
        for i in 0..k {
            res = (res * vec[i]) % MOD + MOD;
        }
        return res;
    }

    vec.sort_by(|&a, &b| b.abs().cmp(&a.abs()));

    let mut res = 1;
    let mut used = 0;
    let mut prev_neg: Option<i64> = None;
    let mut prev_pos: Option<i64> = None;

    for &num in vec.iter() {
        if num > 0 {
            res = (res * num) % MOD;
            used += 1;
            prev_pos = Some(num);
        } else if let Some(neg) = prev_neg {
            if used + 2 > k {
                if let Some(pos) = prev_pos {
                    if neg.abs() >= pos.abs() {
                        res = (res * inv_mod(pos, MOD)) % MOD;
                        res = (res * neg * num) % MOD;
                        used += 1;
                        prev_neg = None;
                    } else {
                        continue;
                    }
                } else {
                    continue;
                }
            } else {
                res = (res * neg * num) % MOD;
                used += 2;
                prev_neg = None;
            }
        } else {
            prev_neg = Some(num);
        }

        if used >= k {
            break;
        }
    }

    if used + 1 == k {
        res = (res * prev_neg.unwrap()) % MOD + MOD;
    }

    res
}

fn main() {
    let mut reader = TokenReader::new();
    let _ = reader.next::<usize>();
    let k = reader.next::<usize>();
    let mut vec = reader.vector::<i64>();

    let res = solve(&mut vec, k);
    println!("{}", res);
}