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: Runtime Error)

// https://atcoder.jp/contests/abc173/tasks/abc173_e

use proconio::fastout;
use proconio::input;
use std::collections::VecDeque;

const MOD: i64 = 1000000007;

#[fastout]
fn main() {
    input! {
        n: usize, k: usize,
        mut a_s: [i64; n],
    }
    let ans = if n == k || (a_s.iter().max().unwrap() < &0 && k % 2 != 0) {
        a_s.sort_unstable_by(|&a, &b| b.cmp(&a));
        let ans = a_s[..k].iter().fold(1, |acc, &cur| (acc * cur) % MOD);
        if ans < 0 {
            MOD + ans
        } else {
            ans
        }
    } else {
        let mut ps: Vec<_> = a_s.iter().filter(|&&x| x >= 0).collect();
        let mut ns: Vec<_> = a_s.iter().filter(|&&x| x < 0).collect();
        ps.sort_unstable_by(|&a, &b| b.abs().cmp(&a.abs()));
        ns.sort_unstable_by(|&a, &b| b.abs().cmp(&a.abs()));
        let mut ps: VecDeque<_> = ps.iter().map(|&&x| x).collect();
        let mut ns: VecDeque<_> = ns.iter().map(|&&x| x).collect();
        let mut acc = 1;
        let mut size = 0;
        while size < k {
            if size == k - 1 {
                acc = (acc * ps.pop_front().unwrap()) % MOD;
                size += 1;
            } else {
                let mut pi = ps.iter();
                let mut ni = ns.iter();
                let x = (pi.next(), pi.next(), ni.next(), ni.next());
                match x {
                    (Some(p0), Some(p1), Some(n0), Some(n1)) => {
                        if p0 * p1 < n0 * n1 {
                            acc = (acc * ns.pop_front().unwrap()) % MOD;
                            acc = (acc * ns.pop_front().unwrap()) % MOD;
                            size += 2;
                        } else {
                            acc = (acc * ps.pop_front().unwrap()) % MOD;
                            size += 1;
                        }
                    }
                    (Some(_), _, _, _) => {
                        acc = (acc * ps.pop_front().unwrap()) % MOD;
                        size += 1;
                    }
                    (_, _, Some(_), Some(_)) => {
                        acc = (acc * ns.pop_front().unwrap()) % MOD;
                        acc = (acc * ns.pop_front().unwrap()) % MOD;
                        size += 2;
                    }
                    _ => (),
                }
            }
        }
        acc
    };
    println!("{}", ans);
}