Language: C++

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i,n) for(int i=0, i##_len=(int)(n); i<i##_len; i++)
#define reps(i,n) for(int i=1 , i##_len=(int)(n);i<=i##_len;i++)
#define rrep(i,n) for(int i=((int)(n)-1);i>=0;i--)
#define rreps(i,n) for(int i=((int)(n));i>0;i--)
#define repi(i,x) for(auto i=(x).begin(),i##_fin=(x).end();i!=i##_fin;i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define solve(a) ((a)?"Yes":"No")
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int , int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long , long long> P;
typedef vector<P> VP;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1;} return 0;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1;} return 0;}
const long long INFLL = 1LL<<60;
const int INF = 1<<30;
const double PI=acos(-1);
ll mod =1e9+7;
int main(){
	int n,k;
	V po;
	V ne;
	pair<ll,bool>up=mp(1LL,true),down(1LL,true);
	cin >>n>>k;
	ll ans=0;
	int zoro=0;
	V a(n);
	rep(i,n){
		cin>>a[i];
		if(a[i]>0LL){
			po.push_back(a[i]);
		}else if(a[i]<0LL){
			ne.push_back(a[i]);
		}else{
			zoro++;
		}
	}
//	clog<<"--"<<po.size()<<" "<<ne.size()<<endl;
	sort(all(po),greater<ll>());
	sort(all(ne));
	int ter1=0,ter2=0;
	ll bef1=0LL;
	ll bef2=0LL;
	//upper
	for(int i=0;i<k and (ter1<po.size() or ter2<ne.size());){
		bef1=0LL;
		bef2=0LL;
		if(i!=k-1){
		if(ter1 + 1 <po.size()){
			bef1=po[ter1]*po[ter1+1];
		}if(ter2+1 <ne.size()){
			bef2=ne[ter2]*ne[ter2+1];
		}
	//	printf("(%lld %lld)\n",bef1,bef2);
		if(bef1 < bef2){
			up.F*=bef2%mod;
			up.F%=mod;
			ter2+=2;
			i+=2;
		}else if (bef1 !=0LL){
			up.F*=bef1%mod;
			up.F%=mod;
			ter1+=2;
			i+=2;
		}else if(ter1 <po.size()){
			up.F*=po[ter1]%mod;
			up.F%=mod;
			ter1++;
			i++;
		}else{
			up.F*=ne[ter2]%mod;
			up.F %= mod;
			ter2++;
			up.S^=true;
			i++;
		}

		}else{
			if (ter1 < po.size())
			{
				up.F *= po[ter1] % mod;
				up.F %= mod;
				ter1++;
				i++;
			}
			else
			{
				up.F *= ne[ter2]%mod;
				up.F %= mod;
				ter2++;
				up.S ^= true;
				i++;
			}
	}}
	if(ter1+ter2<k){
		up.F=INFLL;
		up.S=false;
	}
	sort(all(ne), greater<ll>());
	sort(all(po));
	ter1=0;
	ter2=0;
	for (int i = 0; i < k and (ter1 < po.size() or ter2 < ne.size());)
	{
		bef1 = 0LL;
		bef2 = 0LL;
	//	clog<<" :: "<<down.F<<endl;
		if (i == 0 and ne.size() != 0){
			down.F *= ne[0];
			down.F %= mod;
			ter2++;
			i++;
			down.S ^= true;
		}else if (i != k - 1){
			if (ter1 + 1 < po.size()){
				bef1 = po[ter1] * po[ter1 + 1];
			}
			if (ter2 + 1 < ne.size())	{
				bef2 = ne[ter2] * ne[ter2 + 1];
			}
		//	printf("(%3d %3d)\n", bef1, bef2);
			if (bef1 > bef2 and bef2 != 0LL)
			{
				down.F *= bef2 % mod;
				down.F %= mod;
				ter2 += 2;
				i += 2;
			}else if(bef1!=0LL){
				down.F *= bef1 % mod;
				down.F %= mod;
				ter1 += 2;
				i += 2;
			}
			else if (ter1 < po.size())
			{
				down.F *= po[ter1] % mod;
				down.F %= mod;
				ter1++;
				i++;
			}
			else
			{
				down.F *= ne[ter2];
				down.F %= mod;
				ter2++;
				down.S ^= true;
				i++;
			}
		}
		else
		{
			if (ter1 < po.size())
			{
				down.F *= po[ter1] % mod;
				down.F %= mod;
				ter1++;
				i++;
			}
			else
			{
				down.F *= ne[ter2];
				down.F %= mod;
				ter2++;
				down.S ^= true;
				i++;
			}
		}
	}
	down.F+=mod;
	down.F%=mod;
	if (ter1 + ter2 < k)
	{
		
		down.S = true;
	}
	if(up.S==true){
		cout<<up.F%mod<<endl;
	}else if (zoro!=0){
		cout<<0<<endl;
	}else if(down.S==false){
		cout<<down.F%mod<<endl;
	}else{
		cout<<down.F%mod<<<endl;
	}
//	clog<<up.F<<" "<<up.S<<endl;
//	clog << down.F <<" "<< down.S << endl;
}
/*
1 2 -3 -4
-3 -4 2 -> 12
-4 -3 2 -> 24

1 -2 -3 -4
-2 -3 -4 -5 3
3 0 9 4 -> +
1 0 8 9 -> -
1 0 8 8 -> +
*/

Compiler output:
a.cc: In function 'int main()':
a.cc:193:35: error: expected primary-expression before '<' token
  193 |                 cout<<down.F%mod<<<endl;
      |                                   ^