Language: C++

#include <bits/stdc++.h>
using namespace std;

#define mod 1'000'000'007
typedef long long ll;

template<typename T>
void deb(initializer_list<T> l)
{
	for (auto &e : l)
		cout << e << ' ';
	cout << endl;
}

ll multiply(vector<ll>& a, ll l, ll r)
{
	ll prod = 1;
	for ( ; l <= r; ++l)
	{
		prod = (prod * a[l]) % mod;
		if (prod < 0)
			prod += mod;
	}

	return prod;
}

bool comp(int& a, int& b)
{
	return abs(a) > abs(b);
}

void solve()
{
	ll n, k;
	cin >> n >> k;
	vector<ll> neg, pos;
	neg.reserve(n);
	pos.reserve(n);

	ll z = 0;
	vector<ll> a;
	a.reserve(n);
	while (n--)
	{
		ll e;
		cin >> e;
		if (e > 0)
			pos.push_back(e);
		else if (e < 0)
			neg.push_back(e);
		else
			++z;

		if (e)
			a.push_back(e);
	}

	sort(pos.begin(), pos.end(), greater<ll>());
	sort(neg.begin(), neg.end());
	sort(a.begin(), a.end(), comp);

	if ((int) pos.size() == 0)
	{
		if (k % 2 == 1)
		{
			if (z)
				cout << 0 << '\n';
			else
			{
				reverse(neg.begin(), neg.end());
				cout << multiply(neg, 0, k - 1) << '\n';
			}
			return;
		}

		if ((int) neg.size() < k)
			cout << 0 << '\n';
		else
			cout << multiply(neg, 0, k - 1) << '\n';
		return;
	}

	if ((int) neg.size() == 0)
	{
		if ((int) pos.size() < k)
			cout << 0;
		else
			cout << multiply(pos, 0, k - 1);
		cout << '\n';
		return;
	}

	if ((int) pos.size() + (int) neg.size() < k)
	{
		cout << 0 << '\n';
		return;
	}

	if ((int) pos.size() + (int) neg.size() == k)
	{
		// if ((int) neg.size() % 2 == 0)
		// {
			ll prod = 1;
			if ((int) neg.size())
				prod = (prod * multiply(neg, 0, (int) neg.size() - 1)) % mod;
			if ((int) pos.size())
				prod = (prod * multiply(pos, 0, (int) pos.size() - 1)) % mod;
			// cout << prod << '\n';
		// 	return;
		// }

		if ((int) neg.size() % 2 == 0)
			cout << prod << '\n';
		else if (z)
			cout << 0 << '\n';
		else
			cout << prod << '\n';
		return;
	}

	// if ((int) pos.size() >= k)
	// {
	// 	cout << multiply(pos, 0, k - 1) << '\n';
	// 	return;
	// }

	ll pi = 0;
	ll ni = 0;
	for (ll i = 0; i < k; ++i)
	{
		if (a[i] < 0)
			++ni;
		else
			++pi;
	}

	if (ni % 2 == 0)
	{
		ll prod = 1;
		if (ni)
			prod = (prod * multiply(neg, 0, ni - 1)) % mod;
		if (pi)
			prod = (prod * multiply(pos, 0, pi - 1)) % mod;

		cout << prod << '\n';
		return;
	}

	ll p1 = -4e18;
	ll p2 = -4e18;

	if ((int) pos.size())
	{
		if (pi < (int) pos.size() && pi)
			p1 = (pos[pi] * pos[pi - 1]) % mod;
	}

	if (ni < (int) neg.size())
	{
		if (pi)
			p2 = (neg[ni] * neg[ni - 1]) % mod;
	}

	ll prod = 1;
	if (p1 > p2)
	{
		prod = multiply(pos,0, pi);
		prod = (prod * multiply(neg,0, ni - 2)) % mod;
	}
	else
	{
		prod = multiply(neg,0, ni);
		prod = (prod * multiply(pos,0, pi - 2)) % mod;
	}
	cout << prod;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	int t = 1;
	// cin >> t;
	while (t--)
		solve();

	return 0;
}


Compiler output:
In file included from /usr/include/c++/14/bits/stl_algobase.h:71,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = __gnu_cxx::__normal_iterator<long long int*, std::vector<long long int> >; _Iterator2 = __gnu_cxx::__normal_iterator<long long int*, std::vector<long long int> >; _Compare = bool (*)(int&, int&)]':
/usr/include/c++/14/bits/stl_algo.h:1777:14:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(int&, int&)>]'
 1777 |           if (__comp(__i, __first))
      |               ~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1817:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(int&, int&)>]'
 1817 |           std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1908:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(int&, int&)>]'
 1908 |           std::__final_insertion_sort(__first, __last, __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4805:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Compare = bool (*)(int&, int&)]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:61:6:   required from here
   61 |         sort(a.begin(), a.end(), comp);
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:158:30: error: cannot bind non-const lvalue reference of type 'int&' to a value of type 'long long int'
  158 |         { return bool(_M_comp(*__it1, *__it2)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Val_comp_iter<_Compare>::operator()(_Value&, _Iterator) [with _Value = long long int; _Iterator = __gnu_cxx::__normal_iterator<long long int*, std::vector<long long int> >; _Compare = bool (*)(int&, int&)]':
/usr/include/c++/14/bits/stl_algo.h:1757:20:   required from 'void std::__unguarded_linear_insert(_RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Compare = __gnu_cxx::__ops::_Val_comp_iter<bool (*)(int&, int&)>]'
 1757 |       while (__comp(__val, __next))
      |              ~~~~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1785:36:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(int&, int&)>]'
 1785 |             std::__unguarded_linear_insert(__i,
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
 1786 |                                 __gnu_cxx::__ops::__val_comp_iter(__comp));
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1817:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(int&, int&)>]'
 1817 |           std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1908:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(int&, int&)>]'
 1908 |           std::__final_insertion_sort(__first, __last, __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4805:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Compare = bool (*)(int&, int&)]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:61:6:   required from here
   61 |         sort(a.begin(), a.end(), comp);
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:240:30: error: cannot bind non-const lvalue reference of type 'int&' to a value of type 'long long int'
  240 |         { return bool(_M_comp(__val, *__it)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_comp_val<_Compare>::operator()(_Iterator, _Value&) [with _Iterator = __gnu_cxx::__normal_iterator<long long int*, std::vector<long long int> >; _Value = long long int; _Compare = bool (*)(int&, int&)]':
/usr/include/c++/14/bits/stl_heap.h:140:48:   required from 'void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Distance = long int; _Tp = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_val<bool (*)(int&, int&)>]'
  140 |       while (__holeIndex > __topIndex && __comp(__first + __parent, __value))
      |                                          ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_heap.h:247:23:   required from 'void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Distance = long int; _Tp = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(int&, int&)>]'
  247 |       std::__push_heap(__first, __holeIndex, __topIndex,
      |       ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  248 |                        _GLIBCXX_MOVE(__value), __cmp);
      |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_heap.h:356:22:   required from 'void std::__make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(int&, int&)>]'
  356 |           std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value),
      |           ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  357 |                              __comp);
      |                              ~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1593:23:   required from 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(int&, int&)>]'
 1593 |       std::__make_heap(__first, __middle, __comp);
      |       ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1868:25:   required from 'void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(int&, int&)>]'
 1868 |       std::__heap_select(__first, __middle, __last, __comp);
      |       ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1884:27:   required from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(int&, int&)>]'
 1884 |               std::__partial_sort(__first, __last, __last, __comp);
      |               ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1905:25:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(int&, int&)>]'
 1905 |           std::__introsort_loop(__first, __last,
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
 1906 |                                 std::__lg(__last - __first) * 2,
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1907 |                                 __comp);
      |                                 ~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4805:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _Compare = bool (*)(int&, int&)]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:61:6:   required from here