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<stdio.h>
int R=1,C=1,H[2000010],N[2000010];
//評価関数（いまはMIN）
int hyouka(int a,int b){
  if(C<b)return 1;
  if(C<a||b==0)return 0;
  return N[H[a]]>N[H[b]]?1:0;
}
//挿入関数
void hin(int a){
  int i=C++;
  for(N[H[0]=R]=a;hyouka(0,i/2);i/=2)H[i]=H[i/2];
  H[i]=R++;
}
//取り出す関数
int hout(){
  int rt=H[1],i,j=2,k=H[--C];
  for(i=1;hyouka(i,C);i=j)H[i]=H[j=i*2+1-hyouka(i*2,i*2+1)];
  H[j/2]=k;
  return rt;
}
 long long n,k,M=1e9+7,ans=1,maxs=0,i,j,d[200010],p[200020],m[200020],pr=0,mr=0;
int main(){
  long long s=0,sm[200010]={};
  long long sp[200020]={};
  long long pp[200020]={1};
  long long pm[200020]={1};
 scanf("%lld %lld",&n,&k);
  for(i=0;i<n;i++)scanf("%lld",&d[i]);
  for(i=0;i<n;i++){
    if(d[i]<=0)hin(-d[i]);
  }
  for(mr=0;C-1;mr++)m[mr]=N[hout()];
  //for(i=0;i<mr;i++)printf("%lld ",m[i]); printf("!!\n");
  for(i=0;i<k;i++)ans=(ans*(M-m[n-i-1]))%M;
  //for(i=k;i--;)ans=(ans*(M-m[i]))%M;
  //for(i=k;i--;)printf("%lld\n",m[i]);
  for(i=0;i<mr;i++)sm[i+1]=m[i]+sm[i];
  for(i=0;i<mr;i++)pm[i+1]=m[i]*pm[i]%M;
  R=C=1;
  for(i=0;i<n;i++){
    if(d[i]>0)hin(d[i]);
  }
  for(pr=0;C-1;pr++)p[pr]=N[hout()];
  for(i=0;i<pr;i++)sp[i+1]=p[i]+sp[i];
  for(i=0;i<pr;i++)pp[i+1]=p[i]*pp[i]%M;
  //printf("%lld,%lld\n",pr,mr);
  if(pr==0){
    printf("%lld\n",ans);
    return 0;
  }
  //for(i=0;i<=mr;i++)printf("%lld ",pm[i]);printf("!\n");
  //for(i=0;i<=pr;i++)printf("%lld ",pp[i]);printf("!\n");
  for(i=0;i<=k;i++){
    j=k-i;
    if(pr<i||mr<j)continue;
    s=sp[i]+sm[j];//printf("%lld %lld %lld;\n",i,j,s);
    if(j%2)continue;//printf("%lld %lld:\n",pp[i],pm[j]);
    if(s>maxs){
      maxs=s;
      ans=pp[i]*pm[j]%M;
    }
  }
  printf("%lld\n",ans);
  return 0;
}