Language: C++

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

#define int unsigned long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;

long long power(long long x, long long n){
	long long ans = 1;
	for(;n;n >>= 1, x *= x, ans %= MOD, x %= MOD)
		if(n&1)ans*=x;
	return ans % MOD;
}
	
	
signed main(){
	cout<<fixed<<setprecision(10);
	
	int N, K;
	vector<int> A;
	vector<pair<int,int>> B;
	int p = 0, m = 0, z = 0;
	int ans = 1;
	
	cin>>N>>K;
	
	A.resize(N);
	
	for(int i = 0; i < N; i++){
		cin>>A[i];
		
		B.push_back({abs(A[i]), A[i]});
		
		if(A[i] > 0) p++;
		else if(A[i] == 0) z++;
		else m++;
	}
	
	if(K == 1) {
		
		sort(A.begin(), A.end(), greater<int>());
		
		ans = A[0];
		
	} else if(p == 0) {
		if(K%2 == 0) {
			sort(A.begin(), A.end());
			for(int i = 0; i < K; i++){ 
				ans *= abs(A[i]);
				ans %= MOD;
			}
		} else { 
			
			sort(A.begin(), A.end(), greater<int>());
			
			if(z >= 1) {
				ans = 0;
			} else {
				for(int i = 0; i < K; i++){
					ans *= abs(A[i]);
					ans %= MOD;
				}
				
				ans = (MOD - ans)%MOD;
			}
		}
	} else {
		sort(B.begin(), B.end(), greater<pair<int,int>>());
		
		int conm = 0, conz = 0;
		int minmi = -1, minpi = -1;
		int maxmi = -1, maxpi = -1;
		
		for(int i = 0; i < K; i++){
			if(B[i].second < 0) conm++;
			if(B[i].second == 0) conz++;
			ans *= B[i].first;
			ans %= MOD;
			ans = (ans%MOD + MOD) % MOD;
			if(B[i].second < 0) minmi = i;
			if(B[i].second > 0) minpi = i;
		}
		
		if(conm%2 == 0){
			cout<<ans<<endl;
			return 0;
		}
		if(conz) {
			cout<<0<<endl;
			return 0;
		}
		
		if(K == N) {
			cout<<ans<<endl;
			return 0;
		}
		
		for(int i = K; i < N; i++){
			if(B[i].second > 0) {
				maxpi = i;
				break;
			}
		}
		
		for(int i = K; i < N; i++){
			if(B[i].second < 0) {
				maxmi = i;
				break;
			}
		}
		// exit(2);
		if(maxmi == -1) {
			// exit(2);
			if(maxpi == -1) {
				// exit(2);
				if(z == 0) ans =  0;
				else {
					ans = 1;
					for(int i = 0; i < K; i++){
						ans *= B[N-i-1].first;
						ans %= MOD;
					}
					
					ans = (MOD-ans%MOD)%MOD;
				}ans = (ans%MOD + MOD) % MOD;
				cout<<ans<<endl;
				return 0;
			} else { 
				ans *= B[maxpi].first;
				ans %= MOD;
				ans *= power(B[minmi].first, MOD-2);
				ans %= MOD;
				
			}ans = (ans%MOD + MOD) % MOD;
			cout<<ans<<endl;
			return 0;
			
		}
		
		if(maxpi == -1){
			
			if(maxmi == -1) {
				if(z == 0) ans =  0;
				else {
					ans = 1;
					for(int i = 0; i < K; i++){
						ans *= B[N-i-1].first;
						ans %= MOD;
					}
					
					ans = (MOD-ans)%MOD;
				}
			} else {
				ans *= B[maxmi].first;
				ans %= MOD;
				ans *= power(B[minpi].first, MOD-2);
				ans %= MOD;
			}
			ans = (ans%MOD + MOD) % MOD;
			cout<<ans<<endl;
			return 0;
		}
		
		if(maxpi != -1 && maxmi != -1) {// cout<<"<><>"<<endl;
			int A = B[minpi].first;
			int BB = B[minmi].first;
			
			int a = (-B[maxmi].first + B[minpi].first);
			int b = (-B[maxpi].first + B[minmi].first);
			// cout<<B[minpi].first<<" - "<<B[maxmi].first<<endl;
			
			// cout<<a<<" "<<b<<endl;
			// exit(2);
			if(!a || !b) {
				ans %= MOD;
			} else if(BB * a < A * b) { 
				ans *= B[maxmi].first;
				ans %= MOD;
				ans *= power(B[minpi].first, MOD-2);
				ans %= MOD;
			} else {
				ans *= B[maxpi].first;
				ans %= MOD;
				ans *= power(B[minmi].first, MOD-2);
				ans %= MOD;
			}
			ans = (ans%MOD + MOD) % MOD;
		}
	}
	
	ans = (ans%MOD + MOD) % MOD;
	
	cout<<ans<<endl;
	
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:48:33: error: call of overloaded 'abs(__gnu_cxx::__alloc_traits<std::allocator<long long unsigned int>, long long unsigned int>::value_type&)' is ambiguous
   48 |                 B.push_back({abs(A[i]), A[i]});
      |                              ~~~^~~~~~
In file included from /usr/include/c++/14/cstdlib:79,
                 from /usr/include/c++/14/ext/string_conversions.h:43,
                 from /usr/include/c++/14/bits/basic_string.h:4154,
                 from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
In file included from /usr/include/c++/14/cstdlib:81:
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~
a.cc:48:28: error: no matching function for call to 'std::vector<std::pair<long long unsigned int, long long unsigned int> >::push_back(<brace-enclosed initializer list>)'
   48 |                 B.push_back({abs(A[i]), A[i]});
      |                 ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/vector:66,
                 from a.cc:5:
/usr/include/c++/14/bits/stl_vector.h:1283:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::pair<long long unsigned int, long long unsigned int>; _Alloc = std::allocator<std::pair<long long unsigned int, long long unsigned int> >; value_type = std::pair<long long unsigned int, long long unsigned int>]'
 1283 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:35: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const std::vector<std::pair<long long unsigned int, long long unsigned int> >::value_type&' {aka 'const std::pair<long long unsigned int, long long unsigned int>&'}
 1283 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_vector.h:1300:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = std::pair<long long unsigned int, long long unsigned int>; _Alloc = std::allocator<std::pair<long long unsigned int, long long unsigned int> >; value_type = std::pair<long long unsigned int, long long unsigned int>]'
 1300 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1300:30: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::vector<std::pair<long long unsigned int, long long unsigned int> >::value_type&&' {aka 'std::pair<long long unsigned int, long long unsigned int>&&'}
 1300 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~
a.cc:65:43: error: call of overloaded 'abs(__gnu_cxx::__alloc_traits<std::allocator<long long unsigned int>, long long unsigned int>::value_type&)' is ambiguous
   65 |                                 ans *= abs(A[i]);
      |                                        ~~~^~~~~~
/usr/include/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~
a.cc:76:51: error: call of overloaded 'abs(__gnu_cxx::__alloc_traits<std::allocator<long long unsigned int>, long long unsigned int>::value_type&)' is ambiguous
   76 |                                         ans *= abs(A[i]);
      |                                                ~~~^~~~~~
/usr/include/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~