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: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define PI 3.14159265
#define pb push_back
#define en cout<<'\n'
#define vec vector<ll>
#define vecs vector<string>
#define pii pair<ll,ll>
#define VP vector< pii >
#define setbits(x)  __builtin_popcountll(x)
#define zrobits(x) ud __builtin_ctzll(x)
#define all(v) (v).begin(),(v).end()
#define ff first
#define ss second
#define mm " "
#define MM " "
#define f(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rf(i,a,b) for(ll i=(ll)a;i>=(ll)b;i--)
#define fa(x,v) for(auto x:v)
#define INF (ll)(9e18)
#define Sort(v) sort(v.begin(),v.end())
#define test ll T;cin>>T;while(T--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define max4(a,b,c,d) max(a,max3(b,c,d))
#define min4(a,b,c,d) min(a,min3(b,c,d))
#define lb(v,val)  (lower_bound(v.begin(),v.end(),val)-v.begin())
#define ub(v,val)  (upper_bound(v.begin(),v.end(),val)-v.begin())

    const int MOD=1e9+7;
    ll mod(ll x){
        return (x%MOD + MOD)%MOD;
    }
    ll mul(ll a , ll b){
        return mod(mod(a)*mod(b));
    }
string itos(ll s)
{
string str=to_string(s);
return str;
}
ll stoi2(string s)
{
    ll a= atoi(s.c_str());
    return a;
}
ll powerm(ll a, ll b, ll MOD) {
    a %= MOD;
    ll res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return res;
}

ll power(ll a, ll b) {

  ll res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a ;
        a = a * a ;
        b >>= 1;
    }
    return res;
}

bool isSubSequence(string str1, string str2, int m, int n)
{

    if (m == 0) return true;
    if (n == 0) return false;

    if (str1[m-1] == str2[n-1])
        return isSubSequence(str1, str2, m-1, n-1);

    return isSubSequence(str1, str2, m, n-1);
}


bool ispalindrome(ll len,string str)
{

    for(int i = 0; i < len/2; i++){
        if(str[i] != str[len-1-i]){
            return false;
        }
    }
    return true;
}
ll binarySearch(ll a[],ll low,ll high,ll key){
    while(low<=high)
    {
        ll mid=(low+high)/2;
        if(a[mid]<key)
        {
            low=mid+1;
        }
        else if(a[mid]>key)
        {
            high=mid-1;
        }
        else
        {
            return mid;
        }
    }
    return -1;
}
ll isprime(ll N){
    if(N<2 || (!(N&1) && N!=2))
        return 0;
    for(ll i=3; i*i<=N; i+=2){
        if(!(N%i))
            return 0;
    }
    return 1;
}
vec factor;
void factors(ll n)
{

for(ll i=1;i<=sqrt(n);i++)
{
    if(n%i==0)
    {
        if(i==n/i)
        factor.pb(i);
        else
        {
            factor.pb(i);
            factor.pb(n/i);
        }

    }
}

sort(all(factor));
}
ll gcd(ll a, ll b) {
   if (b == 0)
      return a;
   return gcd(b, a % b);
}
ll lcm(ll a,ll b)
{
	return (a/gcd(a,b))*b;
}
bool isvalid(ll i,ll j,ll n)
{
    if(i>=0&&i<n&&j>=0&&j<n)return true;
    return false;
}
ll dx[8]={0,1,1,1,0,-1,-1,-1};
ll dy[8]={-1,-1,0,1,1,1,0,-1};


ll binarytodecimal(string n)
{
    string num = n;
    ll dec_value = 0;

   ll base = 1;

   ll len = num.length();
    for (int i = len - 1; i >= 0; i--) {
        if (num[i] == '1')
            dec_value += base;
        base = base * 2;
    }

    return dec_value;
}
void tester_said_wrong_code___Me_mat_maan_m_a_ch_da()
{

}

string dtb(ll  n )
{
//    ,ll siz
    string s="";

    while(n>0){

    if(n%2==0)s.pb('0');
    else s.pb('1');
    n/=2;

    }

    reverse(all(s));
//       while(s.size()<siz)
//    {
//
//        s='0'+s;
//
//    }
    return s;
}

vec seive;
void Seive() {
    const ll maxn = 1e6 + 5;
    seive.resize(maxn);
    f(i,0, maxn) seive[i] = i;
    for(ll i=2;i<=maxn;i+=2)
    seive[i] = 2;
    seive[1] = -1;
    seive[0] = -1;
    for (ll i = 3; i <= maxn; i += 2)
        if (i == seive[i])
        for (ll j = i * i ; j < maxn ; j += i)
        if (seive[j] == j)
        seive[j] = i;
}
// ain't afraid of rating



int main()
{
fast;
tester_said_wrong_code___Me_mat_maan_m_a_ch_da;

ll n,k;
cin>>n>>k;
ll a[n];
f(i,0,n)
cin>>a[i];
vec pos,neg,ex;
f(i,0,n)
{
    if(a[i]>0)pos.pb(a[i]);
    else neg.pb(a[i]);
}
VP ok;
 Sort(pos);//
 Sort(neg);
//  if(k%2)
//  {
//      if(neg.size()%2)
//      {
//          ex.pb(neg.back());
//          neg.pop_back();
//      }

//  }
 ll ans=1;

 if(k%2)
 {
     if(pos.size())
     {
        // ans*=pos.back();
        //ans%=MOD;
        k--;
         ans=mul(ans,pos.back());
         pos.pop_back();

     }
     else
     {reverse(all(neg));
         f(i,0,k)
         {
             ans=mul(ans,neg[i]);
         }

cout<<ans;en;return 0;
     }
 }
 reverse(all(pos));
     f(i,0,pos.size())
     {
         if(i+1<=pos.size()-1)
         {
             ok.pb({mul(pos[i],pos[i+1]),1});
             i++;
         }
     }
      f(i,0,neg.size())
     {
         if(i+1<=neg.size()-1)
         {
             ok.pb({mul(neg[i],neg[i+1]),1});
             i++;
         }
     }

    if(pos.size()%2&&neg.size()%2){ok.pb({mul(pos.back(),neg.back()),0});pos.pop_back();neg.pop_back();}
     Sort(ok);

     ll p=0,ne=0;
     reverse(all(ok));
   //  fa(c,ok)cout<<c.ff<<MM;en;
     f(i,0,k/2)
     {
         //ans=ans*ok[i].ff;
         ans=mul(ans,ok[i].ff);

        // ans%=MOD;
     }
  //  cout<<ans;en;


//if(ans<0)
//{
//    ans=MOD+ans%MOD;
//}
cout<<(ans);














}