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<iostream>
#include<vector>
#include<utility>
#include<algorithm>
using namespace std;

long long pow(long long a,long long b) {
  if(b == 0)return 1;
  else return pow(a*a%10000007,b/2)*(b%2==0?1:a)%1000000007;
}

int main() {
  int N,K;
  cin >> N >> K;
  vector<pair<int,int>> A(N);
  int i;
  int cnt = 0;
  for(i=0;i<N;i++) {
    cin >> A[i].first;
    if(A[i].first < 0) {
      A[i].first *= -1;
      A[i].second = 1;
      cnt++;
    }
    else A[i].second = 0;
  }
  sort(A.begin(),A.end(),greater<pair<int,int>>());
  long long ans = 1,Ans = 0;
  int sm,sp,bm,bp;
  sm = sp = bm = bp = -1;
  if(cnt == N) {
    for(i=0;i<K;i++)Ans = Ans*A[N-i-1].first%1000000007;
    if(K%2 == 1)Ans = (-Ans%1000000007+1000000007)%1000000007;
  }
  else {
    cnt = 0;
    for(i=0;i<K;i++) {
      if(A[i].second == 0)sp = max(sp,A[i].first);
      else {
        sm = max(sm,A[i].first);
        cnt++;
      }
      ans = ans*A[i].first%1000000007;
    }
    for(i=K;i<N;i++) {
      if(A[i].second == 0)bp = min(bp,A[i].first);
      else bm = min(bm,A[i].first);
    }
    if(cnt%2 == 0)Ans = ans;
    else {
      if(sp!=-1 && bm!=-1)Ans = ans*pow(sp,1000000005)%1000000007*bm%1000000007;
      if(sm!=-1 && bp!=-1)Ans = max(Ans,ans*pow(sm,1000000005)%1000000007*bp%1000000007);
    }
  }
  cout << Ans << endl;
}