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)]
#![allow(dead_code)]
use datastructure::*;
use grapth::*;
use input_scanner::*;
use math::*;
use string_algorithm::*;
use util::*;

use std::cmp;
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};

type Val = (i64, i64, i64);
const MOD: i64 = 1_000_000_007;


fn mul(mut a: Val, mut b: Val) -> Val {
    a.1 *= a.0;
    a.2 *= a.0;
    b.1 *= b.0;
    b.2 *= b.0;

    a.0 *= b.0;
    a.1 = a.1 * b.1 * MOD + a.1 + b.1;
    a.2 *= b.2;
    a.1 += a.2 / MOD;
    a.2 %= MOD;

    a.1 *= a.0;
    a.2 *= a.0;
    a
}

fn calc(a: &Vec<i64>, b: &Vec<i64>, k: usize) -> Val {
    let mut ret: Val = (-1, i64::min_value(),0);
    if a.len() + b.len() < k {
        return ret;
    }

    let mut sa: Vec<Val> = vec![(1,0,1)];
    let mut sb: Vec<Val> = vec![(1,0,1)];
    for x in a {
        sa.push(mul(*sa.last().unwrap(), (1,0,*x)));
    }
    for x in b {
        sb.push(mul(*sb.last().unwrap(), (-1,0,*x)));
    }

    for i in 0..sa.len() {
        if k < i || sb.len() + i <= k {
            continue;
        }
        ret = cmp::max(ret, mul(sa[i], sb[k - i]));
    }

    ret
}

fn main() {
    let mut scan = Scanner::new();
    let n: usize = scan.next();
    let k: usize = scan.next();
    let mut p: Vec<i64> = vec![];
    let mut m: Vec<i64> = vec![];
    let mut z: usize = 0;
    
    for _i in 0..n {
        let a: i64 = scan.next();
        if a==0{
            z += 1;
        }else if a<0 {
            m.push(a);
        }else{
            p.push(a);
        }
    }
    m.sort();
    p.sort();
    m.reverse();

    let mut ret: Val = calc(&p, &m, k);
    m.reverse();
    p.reverse();
    ret = cmp::max(ret, calc(&p, &m, k));
    if ret.0 == -1 && z > 0 {
        ret = (1,0,0);
    }

    if ret.0 == -1{
        println!("{}", (MOD + ret.2) % MOD);
    }else{
        println!("{}", ret.2);
    }
}

mod datastructure {}
mod grapth {}
mod math {}
mod string_algorithm {}
mod util {}
mod input_scanner {
    pub struct Scanner {
        bytes: Vec<u8>,
        begin: usize,
        end: usize,
    }

    impl Scanner {
        pub fn new() -> Self {
            use std::io::Read;
            let mut buffer: Vec<u8> = Vec::new();
            std::io::stdin().read_to_end(&mut buffer).unwrap();
            let end = (0..buffer.len())
                .find(|i| buffer[*i].is_ascii_whitespace())
                .unwrap_or(buffer.len());
            Scanner {
                bytes: buffer,
                begin: 0,
                end: end,
            }
        }
        pub fn next<T: std::str::FromStr>(&mut self) -> T {
            let s: &str =
                unsafe { std::str::from_utf8_unchecked(&self.bytes[self.begin..self.end]) };
            let ret: T = s.parse::<T>().ok().expect("Parse error.");
            self.skip();
            ret
        }
        pub fn next_vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
            (0..n).map(|_| self.next()).collect()
        }
        pub fn next_word(&mut self) -> Vec<u8> {
            let r = Vec::from(&self.bytes[self.begin..self.end]);
            self.skip();
            r
        }
        fn skip(&mut self) {
            self.begin = (self.end..self.bytes.len())
                .find(|i| !self.bytes[*i].is_ascii_whitespace())
                .unwrap_or(self.bytes.len());
            self.end = (self.begin..self.bytes.len())
                .find(|i| self.bytes[*i].is_ascii_whitespace())
                .unwrap_or(self.bytes.len());
        }
    }
}