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)

#![allow(non_snake_case)]
#![allow(unused_imports)]

use proconio::{input, fastout};
use proconio::marker::*;
use whiteread::parse_line;
use std::collections::*;
use num::*;
use num_traits::*;
use superslice::*;
use std::ops::*;

use itertools::Itertools;
use itertools_num::ItertoolsNum;

pub mod mod_int {
    use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
    const MOD: i64 = 1e9 as i64 + 7;
    #[derive(Clone, Copy, Debug)]
    pub struct ModInt<T: Copy + Clone + Add + AddAssign + Mul + MulAssign + Sub + SubAssign>(pub T);
    #[warn(unused_macros)]
    macro_rules !mod_int_impl {($($t :ty ) *) =>($(impl Add <ModInt <$t >>for ModInt <$t >{type Output =ModInt <$t >;fn add (self ,other :ModInt <$t >) ->ModInt <$t >{self +other .0 } } impl Add <$t >for ModInt <$t >{type Output =ModInt <$t >;fn add (self ,rhs :$t ) ->ModInt <$t >{let mut tmp =rhs +self .0 ;if tmp >=self ::MOD as $t {tmp =tmp -self ::MOD as $t ;} ModInt (tmp ) } } impl Sub <ModInt <$t >>for ModInt <$t >{type Output =ModInt <$t >;fn sub (self ,rhs :ModInt <$t >) ->ModInt <$t >{self -rhs .0 } } impl Sub <$t >for ModInt <$t >{type Output =ModInt <$t >;fn sub (self ,rhs :$t ) ->ModInt <$t >{let rhs =if rhs >=self ::MOD as $t {rhs %self ::MOD as $t } else {rhs } ;let value =if self .0 <rhs {self .0 +self ::MOD as $t } else {self .0 } ;ModInt (value -rhs ) } } impl AddAssign <ModInt <$t >>for ModInt <$t >{fn add_assign (&mut self ,rhs :ModInt <$t >) {*self =*self +rhs ;} } impl AddAssign <$t >for ModInt <$t >{fn add_assign (&mut self ,rhs :$t ) {*self =*self +rhs ;} } impl SubAssign <ModInt <$t >>for ModInt <$t >{fn sub_assign (&mut self ,rhs :ModInt <$t >) {*self =*self -rhs ;} } impl SubAssign <$t >for ModInt <$t >{fn sub_assign (&mut self ,rhs :$t ) {*self =*self -rhs ;} } impl Div <$t >for ModInt <$t >{type Output =ModInt <$t >;fn div (self ,mut rhs :$t ) ->ModInt <$t >{if rhs >=MOD as $t {rhs %=MOD as $t ;} self *ModInt (rhs ) .pow ((MOD -2 ) as usize ) } } impl Div <ModInt <$t >>for ModInt <$t >{type Output =ModInt <$t >;fn div (self ,rhs :ModInt <$t >) ->ModInt <$t >{self /rhs .0 } } impl DivAssign <$t >for ModInt <$t >{fn div_assign (&mut self ,rhs :$t ) {*self =*self /rhs } } impl DivAssign <ModInt <$t >>for ModInt <$t >{fn div_assign (&mut self ,rhs :ModInt <$t >) {*self =*self /rhs } } impl Mul <ModInt <$t >>for ModInt <$t >{type Output =ModInt <$t >;fn mul (self ,rhs :ModInt <$t >) ->ModInt <$t >{self *rhs .0 } } impl Mul <$t >for ModInt <$t >{type Output =ModInt <$t >;fn mul (self ,rhs :$t ) ->ModInt <$t >{ModInt (self .0 *rhs %self ::MOD as $t ) } } impl MulAssign <ModInt <$t >>for ModInt <$t >{fn mul_assign (&mut self ,rhs :ModInt <$t >) {*self =*self *rhs ;} } impl MulAssign <$t >for ModInt <$t >{fn mul_assign (&mut self ,rhs :$t ) {*self =*self *rhs ;} } impl ModInt <$t >{pub fn new (x :$t ) ->Self {ModInt (x %self ::MOD as $t ) } pub fn pow (self ,e :usize ) ->ModInt <$t >{let mut result =ModInt ::<$t >::new (1 ) ;let mut cur =self ;let mut e =e ;while e >0 {if e &1 ==1 {result *=cur ;} cur *=cur ;e >>=1 ;} result } } ) *) }
    mod_int_impl !(usize i32 i64 u32 u64) ;
}

#[fastout]
fn main() {
    input!{
        n: usize, k: usize,
        a_vec: [i64; n],
    }

    type ModInt = mod_int::ModInt<i64>;

    let mut plus = a_vec.iter().filter(|&x| x > &0).map(|x| *x).collect::<Vec<i64>>();
    plus.sort();
    let mut minus = a_vec.iter().filter(|&x| x <= &0).map(|x| *x).collect::<Vec<i64>>();
    minus.sort();

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

    if k % 2 == 0 {
        let mut rm_zero = a_vec.iter().filter(|&x| x != &0).map(|x| *x).collect::<Vec<i64>>();
        if rm_zero.len() < k {
            ans = ModInt::new(0)
        } else {

            rm_zero.sort_by_key(|x| x.abs());
            rm_zero.reverse();
            for i in 0..k-1 {
                ans *= rm_zero[i];
            }

            let mut idx = k-1;
            loop {
                if (ans * rm_zero[idx]).0 > 0 {
                    ans *= rm_zero[idx];
                    break;
                }

                idx += 1;
            }
        }
    } else {
        if plus.len() >= k {
            plus.reverse();
            for x in plus[0..k].iter() {
                ans *= *x;
            }
        } else {
            let mut rm_zero = a_vec.iter().filter(|&x| x != &0).map(|x| *x).collect::<Vec<i64>>();
            if rm_zero.len() != n {
                ans = ModInt::new(0)
            } else {
                rm_zero.sort_by_key(|x| x.abs());
                for i in 0..k {
                    ans *= rm_zero[i];
                }
                ans = ModInt::new(1e9 as i64 + 7 + ans.0)
            }
        }
    }

    println!("{}", ans.0);
}