Language: C++

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>

typedef unsigned long long ULLONG;
typedef long long LLONG;
static const LLONG MOD_NUM = 1000000007;//998244353;

template<class _T> static void getval(_T& a) {
	std::cin >> a;
}
template<class _T> static void getval(_T& a, _T& b) {
	std::cin >> a >> b;
}
template<class _T> static void getval(_T& a, _T& b, _T& c) {
	std::cin >> a >> b >> c;
}

static void E();

int main()
{
	E();
	fflush(stdout);
	return 0;
}

static void E()
{
	int N, K;
	getval(N, K);

	std::vector<LLONG> ai(N);
	for (int i = 0; i < N; i++) {
		getval(ai[i]);
	}

	std::sort(ai.begin(), ai.end(), [](const LLONG& a, const LLONG& b) {
		return tp_abs(a) > tp_abs(b);
	});  //std::greater<LLONG>());

	int negCnt = 0;
	for (int i = 0; i < K; i++) {
		if (ai[i] < 0) {
			negCnt++;
		}
	}

	LLONG ans1 = 1;
	if (negCnt % 2) {
		int restNegIdx = -1;
		for (int i = K; i < N; i++) {
			if (ai[i] < 0) {
				restNegIdx = i;
			}
		}
		int chgNegIdx = -1;
		for (int i = K - 1; i >= 0; i--) {
			if (ai[i] < 0) {
				chgNegIdx = i;
			}
		}
		int chgPosIdx = -1;
		for (int i = K; i < N; i++) {
			if (ai[i] >= 0) {
				chgPosIdx = i;
			}
		}
		if (chgPosIdx == -1) {
			//	全部負の数が確定
			std::sort(ai.begin(), ai.end(), std::greater<LLONG>());
			//	少しでも大きな数になるよう，絶対値の小さいほうから使う
			for (int i = 0; i < K; i++) {
				ans1 = (ans1 * ai[i]) % MOD_NUM;
				if (ans1 < 0) ans1 += MOD_NUM;
			}
		}
		else {
			for (int i = 0; i < K; i++) {
				if (i == chgNegIdx) {
					ans1 = ans1 * tp_abs(ai[chgPosIdx]) % MOD_NUM;
				}
				else {
					ans1 = ans1 * tp_abs(ai[i]) % MOD_NUM;
				}
			}

			if (restNegIdx != -1) {
				if (ai[chgPosIdx] * ai[K - 1] < ai[chgNegIdx] * ai[restNegIdx]) {
					ans1 = 1;
					for (int i = 0; i < K - 1; i++) {
						ans1 = ans1 * tp_abs(ai[i]) % MOD_NUM;
					}
					ans1 = ans1 * tp_abs(ai[restNegIdx]) % MOD_NUM;
				}
			}
		}
	}
	else {
		for (int i = 0; i < K; i++) {
			ans1 = ans1 * tp_abs(ai[i]) % MOD_NUM;
		}
	}
	printf("%lld\n", ans1);
}


Compiler output:
a.cc: In lambda function:
a.cc:48:24: error: 'tp_abs' was not declared in this scope
   48 |                 return tp_abs(a) > tp_abs(b);
      |                        ^~~~~~
a.cc: In function 'void E()':
a.cc:90:55: error: 'tp_abs' was not declared in this scope
   90 |                                         ans1 = ans1 * tp_abs(ai[chgPosIdx]) % MOD_NUM;
      |                                                       ^~~~~~
a.cc:93:55: error: 'tp_abs' was not declared in this scope
   93 |                                         ans1 = ans1 * tp_abs(ai[i]) % MOD_NUM;
      |                                                       ^~~~~~
a.cc:101:63: error: 'tp_abs' was not declared in this scope
  101 |                                                 ans1 = ans1 * tp_abs(ai[i]) % MOD_NUM;
      |                                                               ^~~~~~
a.cc:103:55: error: 'tp_abs' was not declared in this scope
  103 |                                         ans1 = ans1 * tp_abs(ai[restNegIdx]) % MOD_NUM;
      |                                                       ^~~~~~
a.cc:110:39: error: 'tp_abs' was not declared in this scope
  110 |                         ans1 = ans1 * tp_abs(ai[i]) % MOD_NUM;
      |                                       ^~~~~~