Language: C++

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>




using namespace std;



#define MAX 10000
#define INF 1e100
#define isset(n,i) ((n>>i)&1LL)

#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define sor(A) sort(A.begin(),A.end())
#define rsor(A) sort(A.rbegin(),A.rend())
#define all(A) A.begin(),A.end()

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long ,long long> pll;
typedef pair<long long ,int> pli;
#define VI  vector<int>
#define VLL  vector<long long>
#define VVI vector<vector<int>>
#define VS  vector<string>
#define VVS vector<vector<string>>
#define X first
#define Y second



#define ASSERT(X) {if (!(X)) {printf("\n assertion failed at line %d\n",__LINE__);exit(0);}}



//BEGINTEMPLATE_BY_ACRUSH_TOPCODER

#define SZ(X) ((int)(X.size()))//NOTES:SIZE(

#define LENGTH(X) ((int)(X.length()))//NOTES:LENGTH(

#define two(X) (1<<(X))//NOTES:two(

#define twoL(X) (((unsigned long long)(1))<<(X))//NOTES:twoL(

#define contain(S,X) (((S)&two(X))!=0)//NOTES:contain(

#define containL(S,X) (((S)&twoL(X))!=0)//NOTES:containL(

const double pi=acos(-1.0);//NOTES:pi

const double eps=1e-11;//NOTES:eps

template<class T> inline void checkmin(T &a,T b){if(b<a) a=b;}//NOTES:checkmin(

template<class T> inline void checkmax(T &a,T b){if(b>a) a=b;}//NOTES:checkmax(

template<class T> inline T sqr(T x){return x*x;}//NOTES:sqr

template<class T> inline string bin(T n,int pos=62) { string s; T i,one=1,two=2; for (i = (one << (pos-1)); i > 0; i = (i>>1) ) s += ((n & i)?"1":"0"); return s; }


int lsb(unsigned int v){
	int r;           // result goes here
	static const int MultiplyDeBruijnBitPosition[32] =
	{
	  0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
	  31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
	};
	r = MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];
	return r+1;
}


template<class T> inline int msb(T n){return (n==0)?0:(1+msb(n>>1));}//NOTES:msb(
template<class T> inline T getbit(T n,int pos){return ((n&(1<<pos))?1:0);}//NOTES:getbit(
template<class T> inline int countbit(T n){return (n==0)?0:(1+countbit(n&(n-1)));}//NOTES:countbit(
template<class T> inline void setbit(T &n,int p){ n = n | (1LL<<p); }


// Can also use popcount for CPU access
// int  __builtin_popcount(unsigned int)
// int  __builtin_popcountll(long long)

//Numberic Functions

template<class T> inline T gcd(T a,T b)//NOTES:gcd(
  {if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}

bool isequal(vector<int> a,vector<int> b){
	REP(i,a.size()) if(a[i]!=b[i]) return false;
	return true;
}

template<class T>
bool allequal(vector<T> a){
	sor(a);
	return a[0]==a[a.size()-1];
}



template<class T1,class T2>
void printpairs(vector< pair<T1,T2> > p){
    for(auto t:p){
        cout << "(" << t.first << " " << t.second << ")  ";
    }
    cout << endl;
}


template<class T>
void printvec(vector<T> a){
	REP(i,a.size())cout << a[i] << " ";
	cout<< endl;
}

void printarray(int *a,int n){
	REP(i,n)cout << *(a+i) << " ";
	cout<< endl;
}



template<typename T>
void printmatrix(vector< vector<T> > a,int n,int m,int width=5){
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cout << setw(width) << a[i][j] ;
		}
		cout << endl;
	}
}


template<typename T>
void readmatrix(vector< vector<T> > &a,int n,int m){
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin >> a[i][j];
		}
	}
}


template<class T>
void printsubarray(vector<T> a,int start ,int end){
	if(start<0 or end>a.size()-1) cout << "Out of bounds" << endl;
	for(int i=start;i<=end;i++)cout << a[i] << " ";
	cout<< endl;
}

bool isprime(int n){
	if(n<2) return false;
	if(n==2) return true;
	for(int i=2;i*i<=n;i++){
		if(n%i==0) return false;
	}
	return true;
}


// Error checking

#define error(args...) { vector<string> _v; split(#args, ',', _v); err(_v.begin(), args); cerr<< endl; }

void split(const string& s, char c, vector<string>& result) {
    stringstream ss(s);
    string x;
    while (getline(ss, x, c))
        result.push_back(x);
}

void err(vector<string>::iterator it) {}
template<typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
    cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << ' ';
    err(++it, args...);
}


/*
vector<vector<ll>> a(n, vector<ll>(m,0) );
readmatrix(a,n,m);
printmatrix(a,n,m);
*/

void yup(){ cout << "Yes" << endl; }
void nope(){ cout << "No" << endl; }
void Yup(){ cout << "YES" << endl; }
void Nope(){ cout << "NO" << endl; }




template<typename T>
void readvec(vector<T>& a,int size){
	T tmp;
	a.clear();
	REP(i,size){
		cin>>tmp;
		a.push_back(tmp);
	}
}

//ENDTEMPLATE_BY_ACRUSH_TOPCODER
//

bool fun(int i,int j)
{
   return abs(i)>abs(j);
}

const long long MOD=1e9+7;

void solvecase(){
    int n;
    int k;
    bool allneg=true;
    VI a;
    cin >> n >> k;
    readvec(a,n);
    sort(a.begin(),a.end(), fun);
    for(int i=0;i<n;i++){
        if(a[i]>=0)allneg=false;
    }
    ll s=1LL;
    if( allneg ){
        if(k%2==0) {
           for(int i=0;i<k;i++) {
               s*=(ll)a[i];
               s%=MOD;
           }
        } else {
           for(int i=0;i<k;i++) {
               s*=a[n-1-i];
               s%=MOD;
           }
        }
       if(s<0LL)s+=MOD;
        cout << s << endl;
        return;
    }
    int parity=0;

    for(int i=0;i<k;i++)if(a[i]<0)parity++;
    for(int i=0;i<k;i++)if(a[i]==0)parity=0;

    if(parity%2==0){
           for(int i=0;i<k;i++) {
               s*=a[i];
               s%=MOD;
           }
        if(s<0LL)s+=MOD;
        cout << s << endl;
        return;
    } else {
        int MAGIC=n+1;
        int lastneg=MAGIC,lastpos=MAGIC;
        int firstpos=MAGIC,firstneg=MAGIC;

        for(int i=0;i<k;i++) if(a[i]<0)lastneg = i;
        for(int i=0;i<k;i++) if(a[i]>0)lastpos = i;
        for(int i=n-1;i>=k;i--) if(a[i]>=0)firstpos = i;
        for(int i=n-1;i>=k;i--) if(a[i]<0)firstneg = i;

        ll option1=1LL;
        ll option2=1LL;
        bool opt1=false,opt2=false;
        if(lastneg!=MAGIC and firstpos!=MAGIC) {
            opt1=true;
            s=1LL;
            for(int i=0;i<k;i++) {
                if(i!=lastneg) s*=a[i];
                s%=MOD;
            }
            s*=a[firstpos];
            s%=MOD;
            if(s<0LL)s+=MOD;
            option1 = s;
        }

        if(firstneg!=MAGIC and lastpos!=MAGIC) {
            opt2=true;
            s=1LL;
            for(int i=0;i<k;i++) {
                if(i!=lastpos) s*=a[i];
                s%=MOD;
            }
            s*=a[firstneg];
            s%=MOD;
            if(s<0LL)s+=MOD;
            option2 = s;
        }

        if(opt1 and opt2){
            //Which is bigger?
            if((ll)a[lastpos]*(ll)a[firstpos] >= (ll)a[firstneg]*(ll)a[lastneg]) {
                cout << option1 << endl;
            } else
                cout << option2 << endl;
            return;
        }

        if(opt1){
            cout << option1 << endl;
            return;
        } else{
                cout << option2 << endl;
                return;
            }

        assert(1==0);
    }

    assert(1==0);

}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int cases=1;
  // cin >> cases;
  while(cases--){
	  solvecase();
  }
  return 0;
}













Compiler output:
a.cc: In function 'void solvecase()':
a.cc:346:9: error: 'assert' was not declared in this scope
  346 |         assert(1==0);
      |         ^~~~~~
a.cc:32:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
   31 | #include <valarray>
  +++ |+#include <cassert>
   32 | #include <vector>
a.cc:349:5: error: 'assert' was not declared in this scope
  349 |     assert(1==0);
      |     ^~~~~~
a.cc:349:5: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'