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 C++ (GCC 9.2.1) (judge verdict: Accepted)

//                                               KEEP GRINDING
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 

using namespace std;
using namespace __gnu_pbds; 

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>   new_data_set;

#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 

#define int long long
#define ld long double
#define MOD 1000000007
#define precise(i) fixed << setprecision(10) << i
#define f(i,a,b) for(int i=a;i<b;++i)

#define endl '\n'
#define debug cout<<"\n========================================\n";
#define err1(a) cout<<#a<<" "<<a<<endl;
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<endl;
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<endl;
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<endl;

#define all(a) a.begin(),a.end()
#define allr(a) a.rbegin(),a.rend()
#define show(a) for(auto xyz:a)cout<<xyz<<" ";cout<<endl;
#define PQ priority_queue
#define LB lower_bound  
#define UB upper_bound   

#define fr first
#define sc second
#define sz(x) (int)(x).size()

int power(int x, int y, int m=MOD) { 
    if (y == 0) return 1; 
    int p = power(x, y/2, m) % m; 
    p = (p * p) % m; 
  	return (y%2 == 0)? p : (x * p) % m; 
}  
void solve(){
	int n,k,pos=0,sign=1;
	cin>>n>>k;
	vector<int> a(n);
	f(i,0,n){
		cin>>a[i];
		if(a[i]>0)pos++;
	}
	if( n == k or (pos == 0 and k&1 ) ){
		sort(allr(a));
		int ans=1;
		f(i,0,k){
			ans=(ans*a[i])%MOD;
		}
		cout<<(ans%MOD+MOD)%MOD;
		return;
	}
	auto cmp=[&](int x,int y){
		return abs(x)>abs(y);
	};
	sort(all(a),cmp);
	int ans=1;
	f(i,0,k){
		ans=(ans*a[i])%MOD;
		sign*=( a[i]>=0 ? 1 : -1 );
	}
	if(sign>0){
		cout<<ans;
	}else{
		int MAXP=0,MAXN=-1e12,MINP=1e12,MINN=0;
		f(i,0,k){
			if(a[i]<0)MAXN=max(MAXN,a[i]);
			else	  MINP=min(MINP,a[i]);
		}
		f(i,k,n){
			if(a[i]<0)MINN=min(MINN,a[i]);
			else	  MAXP=max(MAXP,a[i]);
		}
		if( abs(MAXP*MINP)<abs(MAXN*MINN)){
			cout<<( ( (abs(ans)*abs(MINN))%MOD + MOD )*power(MINP,MOD-2) )%MOD;
		}else{
			cout<<( ( (abs(ans)*MAXP)%MOD + MOD )*power(abs(MAXN),MOD-2) )%MOD;
		}
    }
}
signed main(){
    fastio
    int t=1;
    for(;t--;solve());
}