So…After trying this problem, here’s few points:

1.) there are 2 cases, 1st check if result is gonna be neagtive(i.,e max is negative and k is odd), then simply pick the greatest values(negative, so absolute is less).

2.) in 2nd case, you need to see if you pick two negatives or two positives which will give better result.

3.) You can simply use two pointers, for left and right part of the sorted array.

4.) It’s better to pick raw inputs and run through the code. you’ll understand it better.

I have tried to code concisely, So that you could understand it better:

```
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int main() {
int t;cin>>t;while(t--){
int n,k;cin>>n>>k;
vector<long long> arr(n);
for(int i=0;i<n;i++)
cin>>arr[i];
sort(arr.begin(),arr.end()); //sort here, increasing order
long long res=1;int left=0,right=n-1; //initialise res, and left part, right part
if(arr[n-1]<0 && k%2==1) //1st case where result will be negative
{
for(int i=n-1;i>=n-k;i--) //multiply till k values
res=(res*arr[i])%MOD;
}
else //2nd case
{
for(int i=left,j=right;i<n && j>=0 && k>0;) //pretty straightforward
{
long long x=(arr[i]*arr[i+1]); //first two (2 negatives)
long long y=(arr[j]*arr[j-1]); // or last two (2 positives)
if(k>1) //if(k is 1, then only pick positive)
{
if(x>y) //if negatives gives better result
{
res=((x%MOD)*(res%MOD))%MOD;
i+=2; //since you picked two negatives +2
k-=2;
}
else
{
res=((arr[j]%MOD)*(res%MOD))%MOD; //choose only 1 positive at a time
j--;
k--;
}
}
else
{
res=(res*arr[j])%MOD; //told above
j--;
k--;
}
}
}
res=(res+MOD)%MOD; //you need to do only this, to get both positive or negative values correct
cout<<res<<"\n";
}
return 0;
}
```