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<bits/stdc++.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
long long int power(long long int a,unsigned long long int b,long long  int mod)  
{  
   long long int ans = 1;     // Initialize result  
    a = a % mod; // Update a if it is more than or  
                // equal to mod
    if (a == 0) return 0; // In case a is divisible by mod; 
    while (b > 0)  
    {  
        // If b is odd, multiply a with result  
        if (b & 1)  
            ans = (ans*a) % mod;  
        // b must be even now  
        b = b>>1; // b = b/2  
        a = (a*a)%mod;  
    }  
    return ans;  
}
//the above power function has been taken from https://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/ and has been modified a bit as per the question
#define mod 1000000007
#define ll long long int 
int prime(long long int a)
{
    if(a<=1)
    return 0;//not prime
    for(long long int i=2;i<a;i++)
    {
        if(a%i==0)
        {
            return 0;//not prime
        }
    }
    return 1;//prime
}
long long int hcf(long long int a,long long int b)
{
    if(a==0)
    {
        return b;
    }
    else return hcf(b%a,a);
}
ll lcm(ll a,ll b)
{
    return (a*b)/hcf(a,b);
}
void swap(long long int* a,long long int* b,long long int* c) 
{ 
    long long int temp = *b; 
    *b = *a; 
    *a = *c; 
    *c = temp; 
} 
// the below functions are strictly taken from https://www.geeksforgeeks.org/modular-division/
long long int gcdExtended(long long int a,long long  int b,long long  int *x, long long int *y); 
long long int modInverse(long long int b,long long  int m) 
{ 
   long long  int x, y;
   long long int g = gcdExtended(b, m, &x, &y); 
    if (g != 1) 
        return -1; 
    return (x%m + m) % m; 
} 
void modDivide(long long int a,long long int b,long long int m) 
{ 
    a = a % m; 
   long long int inv = modInverse(b, m); 
    if (inv == -1) 
       cout << "-1"; 
    else
       cout <<(inv * a) % m; 
} 
long long int gcdExtended(long long int a,long long int b,long long int *x,long long int *y) 
{ 
    if (a == 0) 
    { 
        *x = 0, *y = 1; 
        return b; 
    } 
  
   long long int x1, y1; 
   long long int gcd = gcdExtended(b%a, a, &x1, &y1); 
    *x = y1 - (b/a) * x1; 
    *y = x1; 
  
    return gcd; 
} 
//the above functions have been taken from https://www.geeksforgeeks.org/modular-division/
//they r dealing with the modulo part
// the above swap function is taken from https://www.geeksforgeeks.org/swap-three-numbers-cycle/
//code by shivam pandey
//code goes here
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    long long int t;
    cin>>t;
    while(t--)
    {
      long long int n,k;
      cin>>n>>k;
      long long int a[n];
      long long int b[n];
      for(long long int i=0;i<n;i++)
      {
      cin>>a[i];
      b[i]=abs(a[i]);
      }
      sort(a,a+n,greater<long long int>());
      sort(b,b+n);
      long long int i=0,j=n-1;
      long long int pro=1;
       long long int cnt=0;
       while(cnt<k)
       {
           if(abs(a[i])>=abs(a[j]))
           {
               cnt++;
               pro=(pro*a[i])%mod;
               i++;
           }
           else
           {
               cnt++;
               pro=(pro*a[j])%mod;
               j--;
           }
       }
       if(pro>=0)
       cout<<pro%mod<<endl;
       else
       {
          if(abs(i-j)==0)
          {
              long long int pro1=1;
          for(long long int i=0;i<k;i++)
          {
              pro1=pro1*b[i]%mod;
          }
          pro1=-pro1;
          cout<<(pro1%mod+mod)<<endl;
          }
          else
          {
              long long int max1;
              long long int pr2=(pro/a[i-1])%mod;
              pr2=pr2*a[j]%mod;
              long long int pr3=(pro/a[j+1])%mod;
              pr3=pr3*a[i]%mod;
              max1=max(pr2,pr3);
              cout<<max1%mod<<endl;
          }
       }
      
     
    }
    return 0;
}