Language: C++

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int mod = 1e9+7;
const int maxn = 2e5+5;
ll a[maxn];
int main() {
	int n,k;
	cin>>n>>k;
	int flag = 0;
	vector<pair<int,int>> v;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		if(a[i]>=0){
			if(a[i]==0) flag = 1;
			v.push_back({a[i],1});
		}else {
			v.push_back({-a[i],-1});
		}
	}
	sort(v.rbegin(),v.rend());
	vector<int> pos,neg;
	for(int i=0;i<v.size();i++){
		if(v[i].second==1){
			pos.push_back(i);
		}else {
			neg.push_back(i);
		}
	}
	if(k%2==1){
		if(pos.size()==0){
			int cnt = 0; 
			ll ans =1;
			for(int i=neg.size()-1;i>=0;i--){
				ans*=v[neg[i]].first*-1;
				cnt++;
				ans%=mod;
				if(cnt==k){
					break;
				}
			}
			if(flag){
				cout<<0<<endl;
			}else {
				cout<<(ans+mod)%mod<<endl;
			}
			
		}else {
			int cnt = 1;
			for(int i=1;i+1<pos.size();i+=2){
				if(pos[i]<k&&pos[i+1]<k){
					cnt+=2;
				}
			}
			k-=cnt;
			if(k<2){
				for(int i=0;i<k;i++){
					ans*=v[neg[i]].first;
					ans%=mod;
				}
				for(int i=0;i<cnt;i++){
					ans*=v[pos[i]].first;
					ans%=mod;
				}
				cout<<(ans+mod)%mod<<endl;
			}
			else if(cnt+1>=pos.size()||(k>=2&&cnt+1<=pos.size()&&v[neg[k-2]].first*v[neg[k-1]].first>v[pos[cnt]].first*v[pos[cnt+1]].first)){
				ll ans = 1;
				for(int i=0;i<k;i++){
					ans*=v[neg[i]].first;
					ans%=mod;
				}
				for(int i=0;i<cnt;i++){
					ans*=v[pos[i]].first;
					ans%=mod;
				}
				cout<<(ans+mod)%mod<<endl;
			}else {
				ll ans = 1;
				if(k>=2&&cnt+2<=pos.size()){
					for(int i=0;i<k-2;i++){
					ans*=v[neg[i]].first;
					ans%=mod;
				}
				for(int i=0;i<cnt+2;i++){
					ans*=v[pos[i]].first;
					ans%=mod;
				}
				}else {
					for(int i=0;i<cnt;i++){
					ans*=v[pos[i]].first;
					ans%=mod;
				}
				}
				
				cout<<(ans+mod)%mod<<endl;
			}
		}
	}else {
		int cnt = 0;
			for(int i=0;i+1<pos.size();i+=2){
				if(pos[i]<k&&pos[i+1]<k){
					cnt+=2;
				}
			}
			k-=cnt;
			if(k<2){
				for(int i=0;i<k;i++){
					ans*=v[neg[i]].first;
					ans%=mod;
				}
				for(int i=0;i<cnt;i++){
					ans*=v[pos[i]].first;
					ans%=mod;
				}
				cout<<(ans+mod)%mod<<endl;
			}
			else if(cnt+1>=pos.size()||(k>=2&&cnt+1<=pos.size()&&v[neg[k-2]].first*v[neg[k-1]].first>v[pos[cnt]].first*v[pos[cnt+1]].first)){
				ll ans = 1;
				for(int i=0;i<k;i++){
					ans*=v[neg[i]].first;
					ans%=mod;
				}
				for(int i=0;i<cnt;i++){
					ans*=v[pos[i]].first;
					ans%=mod;
				}
				cout<<(ans+mod)%mod<<endl;
			}else {
				ll ans = 0;
				if(k>=2&&cnt+2<=pos.size()){
					for(int i=0;i<k-2;i++){
					ans*=v[neg[i]].first;
					ans%=mod;
				}
				for(int i=0;i<cnt+2;i++){
					ans*=v[pos[i]].first;
					ans%=mod;
				}
				}else {
					for(int i=0;i<cnt;i++){
					ans*=v[pos[i]].first;
					ans%=mod;
				}
				}
				
				cout<<(ans+mod)%mod<<endl;
			}
	}
    return 0;
}



Compiler output:
a.cc: In function 'int main()':
a.cc:75:41: error: 'ans' was not declared in this scope; did you mean 'abs'?
   75 |                                         ans*=v[neg[i]].first;
      |                                         ^~~
      |                                         abs
a.cc:79:41: error: 'ans' was not declared in this scope; did you mean 'abs'?
   79 |                                         ans*=v[pos[i]].first;
      |                                         ^~~
      |                                         abs
a.cc:82:40: error: 'ans' was not declared in this scope; did you mean 'abs'?
   82 |                                 cout<<(ans+mod)%mod<<endl;
      |                                        ^~~
      |                                        abs
a.cc:126:41: error: 'ans' was not declared in this scope; did you mean 'abs'?
  126 |                                         ans*=v[neg[i]].first;
      |                                         ^~~
      |                                         abs
a.cc:130:41: error: 'ans' was not declared in this scope; did you mean 'abs'?
  130 |                                         ans*=v[pos[i]].first;
      |                                         ^~~
      |                                         abs
a.cc:133:40: error: 'ans' was not declared in this scope; did you mean 'abs'?
  133 |                                 cout<<(ans+mod)%mod<<endl;
      |                                        ^~~
      |                                        abs