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

#include<stdio.h>
#include<iostream>
#include<vector>
#include<math.h>
#include<queue>
#include<map>
#include<algorithm>
#include<string.h>
#include<functional>
#include<limits.h>
#include<stdlib.h>
#include<string>
#include<unordered_map>
#include<set>
using namespace std;

#define intmax INT_MAX
#define lmax LONG_MAX
#define uintmax UINT_MAX
#define ulmax ULONG_MAX
#define llmax LLONG_MAX
#define ll long long
#define rep(i,a,N) for((i)=(a);(i)<(N);(i)++)
#define rrp(i,N,a) for((i)=(N)-1;(i)>=(a);(i)--)
#define llfor ll i,j,k
#define sc(a) cin>>a
#define pr(a) cout<<a<<endl
#define pY puts("YES")
#define pN puts("NO")
#define py puts("Yes")
#define pn puts("No")
#define pnn printf("\n")
//#define sort(a) sort(a.begin(),a.end())
#define push(a,b) (a).push_back(b)
#define llvec vector<vector<ll>>
#define charvec vector<vector<char>>
#define sizeoof(a,b) (a,vector<ll>(b))
#define llpvec vector<pair<ll,ll>>
/*繰り上げ除算*/ll cei(ll x,ll y){ll ans=x/y;if(x%y!=0)ans++;return ans;}
/*最大公約数*/ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
/*最小公倍数*/ll lcm(ll x,ll y){return x/gcd(x,y)*y;}
/*n乗*/ll llpow(ll x,ll n){ll i,ans=1;rep(i,0,n)ans*=x;return ans;}
/*階乗*/ll fact(ll x){ll i,ans=1;rep(i,0,x)ans*=(x-i);return ans;}
/*nCr*/ll ncr(ll n,ll r){return fact(n)/fact(r)/fact(n-r);}
/*nPr*/ll npr(ll n,ll r){return fact(n)/fact(n-r);}
/*primejudge*/bool prime(ll a){if(a<=1)return false;ll i;for(i=2;i*i<=a;i++){if(a%i==0)return false;}return true;}
llfor;///////////////////////////////////////////////////////////
bool less_(ll a,ll b){
 return abs(a)>abs(b);
}
int main(){
 ll n,k;
 sc(n);sc(k);
 vector<ll>a;
 ll b;
 ll ct=0,ct2=0;
 rep(i,0,n){
   sc(b);
   if(b!=0){
    push(a,b);
    ct++;
   }
  ct2++;
 }
 ll ans=1;
 if(ct==ct2&&k%2==1){
  sort(a.begin(),a.end());
  reverse(a.begin(),a.end());
  rep(i,0,k)ans=(ans*a[i])%(llpow(10,9)+7);
  if(ans<0)ans+=llpow(10,9)+7;
  pr(ans);return 0;
 }
 sort(a.begin(),a.end(),less_);
 //rep(i,0,n)pr(a[i]);
 bool flag=false;
 ll cnt=0;
 rep(i,0,k){
  if(a[i]<0)cnt++;
  ans=(ans*a[i])%(llpow(10,9)+7);
  //cout<<i<<" "<<ans<<endl;
 }
 if(cnt%2==1)flag=true;
  
 if(flag){
  rep(i,k,n){
   if(a[i]<0){
    ans=(ans*a[i])%(llpow(10,9)+7);
    //cout<<ans<<" "<<i<<" "<<a[i]<<endl;
    break;
   } 
  }
 }
  
 else {
  rrp(i,k,n){
   if(a[i]>0){
    ans=(ans*a[i])%(llpow(10,9)+7);
    //cout<<ans<<" "<<i<<" "<<a[i]<<endl;
    break;
   } 
  }
 }
 pr(ans); 
  
return 0;}