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;

#define rep(i,n) for(ll i=0;i<(n);++i)
using ll = int_fast64_t;
using pll = pair<ll,ll>;
constexpr ll INF = 1LL<<60;
constexpr ll MOD = 1e9+7;
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}return 0;}
#if loc||local
template<class T>void dump(T&& t){cerr<<t<<endl;}
template<class T,class... Ts> void dump(T&& h, Ts&&... t){cerr<<h<<", ";dump(forward<Ts>(t)...);}
#else
void dump(){}
template<class T,class... Ts> void dump(T&& h, Ts&&... t){}
#endif
template<class T> istream &operator>>(istream&is,vector<T>&v){for(auto &elemnt:v)is>>elemnt;return is;}
template<class T,class U> istream &operator>>(istream&is,pair<T,U>&p){is>>p.first>>p.second;return is;}
template<class T> ostream &operator<<(ostream& os,vector<T>const& v){for(auto const& vi:v)os<<vi<<" ";return os;}
template<class T,class U> ostream &operator<<(ostream& os,pair<T,U>const& p){os<<p.first<<","<<p.second;return os;}
template<class T>vector<T> vec(size_t a){return vector<T>(a);}
template<class T, class... Ts>auto vec(size_t a, Ts... ts){return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...));}
template<int64_t MOD>
struct mod_int{
  int64_t val;
  constexpr mod_int():val(0){}
  constexpr mod_int(int64_t x)noexcept:val(x>=0?x%MOD:(MOD-(-x)%MOD)%MOD){}
  constexpr int64_t value()const noexcept{return val;}
  constexpr int64_t get_MOD()const noexcept{return MOD;}
  constexpr mod_int operator+(mod_int const& rhs){
    return mod_int(*this)+=rhs;
  }
  constexpr mod_int operator-(mod_int const& rhs){
    return mod_int(*this)-=rhs;
  }
  constexpr mod_int operator*(mod_int const& rhs){
    return mod_int(*this)*=rhs;
  }
  constexpr mod_int operator/(mod_int const& rhs){
    return mod_int(*this)/=rhs;
  }
  constexpr mod_int &operator+=(mod_int const& rhs)noexcept{
    val += rhs.val;
    if(val>=MOD)val-=MOD;
    return *this;
  }
  constexpr mod_int &operator-=(mod_int const& rhs)noexcept{
    if(val<rhs.val)val+=MOD;
    val-=rhs.val;
    return *this;
  }
  constexpr mod_int &operator*=(mod_int const& rhs)noexcept{
    (val*=rhs.val)%=MOD;
    return *this;
  }
  constexpr mod_int &operator/=(mod_int rhs)noexcept{
    *this*=rhs.inverse();
    return *this;
  }
  constexpr bool operator==(mod_int const& rhs)const{
    return val==rhs.val;
  }
  constexpr bool operator!=(mod_int const& rhs)const{
    return !(val==rhs.val);
  }
  constexpr bool operator<(mod_int const& rhs)const{
    return val<rhs.val;
  }
  constexpr bool operator>(mod_int const& rhs)const{
    return val>rhs.val;
  }
  constexpr bool operator<=(mod_int const& rhs)const{
    return !(val>rhs.val);
  }
  constexpr bool operator>=(mod_int const& rhs)const{
    return !(val<rhs.val);
  }
  constexpr friend std::ostream &operator<<(std::ostream& os, mod_int const& mi){
    return os<<mi.val;
  }
  constexpr friend std::istream &operator>>(std::istream& is, mod_int & mi){
    int64_t t=0;is>>t;
    mi = mod_int<MOD>(t);
    return is;
  }
  constexpr mod_int inverse(){
    int64_t a=val,b=MOD,u=1,v=0,t=0;
    while(b>0){
      t=a/b;
      std::swap(a-=t*b,b);
      std::swap(u-=t*v,v);
    }
    return mod_int(u);
  }
  constexpr mod_int mpow(int64_t n)const{
    mod_int res(1),e(*this);
    while(n){
      if(n&1)res*=e;
      e*=e;
      n>>=1;
    }
    return res;
  }
};
using mint = mod_int<1000000007>;
mint mpow(int64_t p,int64_t n){
  mint res=1;
  mint e = p;
  while(n){
    if(n&1)res*=e;
    e*=e;
    n>>=1;
  }
  return res;
}

struct combination{
  using i64 = int64_t;
  static constexpr i64 COM_MAX = 2e6;
  static constexpr i64 MOD = 1e9+7;
  vector<i64> fac,inv,finv;
  combination(){
    fac.resize(COM_MAX);
    inv.resize(COM_MAX);
    finv.resize(COM_MAX);
    fac[0] = fac[1] = 1;
    inv[1] = 1;
    finv[0] = finv[1] = 1;
    for(int i=2;i<COM_MAX;++i){
      fac[i]=fac[i-1]*i%MOD;
      inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;
      finv[i]=finv[i-1]*inv[i]%MOD;
    }
  }
  template<class T>
  mint nCk(T n,T k){
    if(n<k)return 0;
    if(n<0||k<0)return 0;
    if(n>=COM_MAX){
      mint res1 = mint(1);
      mint res2 = mint(fac[k]);
      for(int i=1;i<=k;++i){
        res1 *= n-(i-1);
      }
      return res1/res2;
    }
    return mint(fac[n]*(finv[k]*finv[n-k]%MOD));
  }
  template<class T>
  mint nHk(T n,T k){
    return nCk(n+k-1,k-1);
  }
  template<class T>
  mint operator()(T n,T k){
    return nCk(n,k);
  }
};


main(){
  int n,k;
  cin>>n>>k;
  vector<ll> a(n);
  cin>>a;

  sort(begin(a),end(a),[](auto a,auto b){return abs(a)>abs(b);});
  int t = 0;
  mint ans = 1;
  rep(i,k){
    ans *= abs(a[i]);
    if(a[i]<0)t^=1;
  }
  if(t==0){
    cout<< ans <<endl;
    return 0;
  }
  /*
  ll aa,bb,cc,dd;
  aa = bb = cc = dd = 0;
  bool fa,fb,fc,fd;
  fa = fb = fc = fd = false;
  for(int i=k-1;i>=0;--i){
    if(a[i]<0){
      aa = a[i];
      fa = true;
      break;
    }
  }
  for(int i=k-1;i>=0;--i){
    if(a[i]>0){
      bb = a[i];
      fb = true;
      break;
    }
  }
  for(int i=k;i<n;++i)if(a[i]<0){
    cc = a[i];
    fc = true;
    break;
  }
  for(int i=k;i<n;++i)if(a[i]>0){
    dd = a[i];
    fd = true;
    break;
  }
  auto evl = [](ll a,ll b)->double{
    return abs(double(a)/double(b));
  };
  if(evl(dd,aa)>evl(cc,bb)){
    ans /= aa;
    ans *= dd;
  }else{
    ans /= bb;
    ans *= cc;
  }
  if(fa&&fb&&fc&&fd){
    cout<<(ans)<<endl;
    return 0;
  }
  */
  sort(begin(a),end(a),[](auto a,auto b){return abs(a)<abs(b);});
  ans = accumulate(begin(a),begin(a)+k,1ll,[](auto a,auto b){return (a*b%MOD+MOD)%MOD;});
  cout<<(ans)<<endl;
  

}

signed _main(){

  int n,k;
  cin>>n>>k;
  vector<ll> a(n);
  cin>>a;

  sort(begin(a),end(a));
  vector<pll> b;
  rep(i,n-1)if(a[i]<0&&a[i+1]<0){
    b.emplace_back(a[i]*a[i+1],-2);
    i++;
  }
  rep(i,n)if(a[i]>0)b.emplace_back(a[i],-1);

  sort(rbegin(b),rend(b));
  dump(b);
  int tmp = 0;
  ll ans = 1;
  for(auto [v,c]:b){
    if(v==0)continue;
    if(tmp-c<=k){
      ans *= (v%MOD);
      ans %= MOD;
      tmp -= c;
    }
  }
  if(tmp==k){
    cout<< ans <<endl;
    return 0;
  }

  sort(begin(a),end(a),[](auto a,auto b){return abs(a)<abs(b);});
  ans = accumulate(begin(a),begin(a)+k,1ll,[](auto a,auto b){return (a*b+MOD)%MOD;});
  cout<<(ans)<<endl;

}