Language: C++

//2020年4月17日現在

#include<stdio.h>
#include<cmath>
#include<math.h>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<complex>
#include<queue>
#include<set>
#include<utility>
#include<iomanip>
#include<limits>
#include<thread>
#include<map>
#include<stack>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
#include<list>
#include<functional>
#include<bitset>
#include<tuple>
#include <cstdio>

using namespace std;

template <class... A>
void prints() { cout << "\n"; }
template <class... A>
void prints_rest() { cout << "\n"; }
template <class T, class... A>
void prints_rest(const T &first, const A &... rest) { cout << " " << first; prints_rest(rest...); }
template <class T, class... A>
void prints(const T &first, const A &... rest) { cout << first; prints_rest(rest...); }
template <class T, class... A>
void printd(const T &first, const A &... rest) { cout << fixed << setprecision(15) << first; prints_rest(rest...); }


namespace {
	// printElems(&a[0], &a[n]);　みたいな感じで使う。
	void printElems(const int* begin, const int* end)
	{
		for (const int* p = begin; p != end; ++p) {
			cout << *p << ' ';
		}
		cout << endl;
	}
}

#define rep(i,n) for(int i=0; i<n; i++)
#define rep1(i,n) for(int i=1; i<n+1; i++)
#define repr(i,n) for(int i=n-1; i>=0; i--)
#define repr1(i,n) for(int i=n; i>0; i--)
#define FOR(i,j,n) for(int i=j; i < n; i++)
#define souatari(i,j,n) rep1(i,n-1)rep(j,i)
#define souatari1(i,j,n) for(int i=2; i<n+1; i++)rep1(j,i-1)
#define mp make_pair
#define F first
#define S second
#define elif else if

typedef long long ll;
typedef vector<int>  vi;
typedef vector<vi>  vvi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef array<int,3> arr3;
typedef long double D;


const ll llinf =  numeric_limits<ll>::max();
const ll _llinf = numeric_limits<ll>::lowest();
const int inf = numeric_limits<int>::max();
const int _inf = numeric_limits<int>::lowest();
const double dinf = numeric_limits<double>::infinity();
const ll MOD = 1000000007;
//const ll MOD = 998244353;
const ll BIG = 1LL<<60;

//----------------------------------------------

template <unsigned P>
struct ModInt
{
		using M = ModInt;
		unsigned v;
		ModInt() : v(0) {}
		ModInt(auto x) : v(x >= 0 ? x % P : (P - -x % P) % P) {}
		constexpr ModInt(unsigned v, int) : v(v) {}
		static constexpr unsigned p() { return P; }
		M operator+() const { return *this; }
		M operator-() const { return {v ? P - v : 0, 0}; }
		explicit operator bool() const noexcept { return v; }
		bool operator!() const noexcept { return !(bool)*this; }
		M operator*(M r) const { return M(*this) *= r; }
		M operator/(M r) const { return M(*this) /= r; }
		M operator+(M r) const { return M(*this) += r; }
		M operator-(M r) const { return M(*this) -= r; }
		bool operator==(M r) const { return v == r.v; }
		bool operator!=(M r) const { return !(*this == r); }
		M &operator*=(M r)
		{
				v = (uint64_t)v * r.v % P;
				return *this;
		}
		M &operator/=(M r) { return *this *= r.inv(); }
		M &operator+=(M r)
		{
				if ((v += r.v) >= P)
						v -= P;
				return *this;
		}
		M &operator-=(M r)
		{
				if ((v += P - r.v) >= P)
						v -= P;
				return *this;
		}
		M inv() const
		{
				int a = v, b = P, x = 1, u = 0;
				while (b)
				{
						int q = a / b;
						swap(a -= q * b, b);
						swap(x -= q * u, u);
				}
				assert(a == 1);
				return x;
		}
		M pow(auto n) const
		{
				if (n < 0)
						return pow(-n).inv();
				M res = 1;
				for (M a = *this; n; a *= a, n >>= 1)
						if (n & 1)
								res *= a;
				return res;
		}
		friend M operator*(auto l, M r) { return M(l) *= r; }
		friend M operator/(auto l, M r) { return M(l) /= r; }
		friend M operator+(auto l, M r) { return M(l) += r; }
		friend M operator-(auto l, M r) { return M(l) -= r; }
		friend ostream &operator<<(ostream &os, M r) { return os << r.v; }
		friend istream &operator>>(istream &is, M &r)
		{
				ll x;
				is >> x;
				r = x;
				return is;
		}
		friend bool operator==(auto l, M r) { return M(l) == r; }
		friend bool operator!=(auto l, M r) { return !(l == r); }
};


ll alist[200005];

ll apos[200005];
ll aneg[200005];

int main(){
	int n,k;
	cin >> n >> k;
	rep(i,n) cin >> alist[i];

	int negcnt = 0;




	ModInt<MOD> ans = 1;
	pll a[200005] = {};
	rep(i,n) {
		if(alist[i]>=0) {a[i] = mp(alist[i],0);}
		else{a[i] = mp(-alist[i],1); negcnt++;}
	}

	sort(a, a+n, greater<>());
	a[n] = mp(0,1);
	a[n+1] = mp(0,1);
	bool flag = false;
	bool negflag = false;
	int nowi = 0;
	if(k == n){
		rep(i,n) ans*= a[i];
		prints(ans);
		return 0;
	}
	int negcnttmp = 0;
	rep(i,k) if(a[i].S == 1) negcnttmp++;


	if(negcnt <= 1){
		bool flagtmp = false;
		rep(i,k){
			if(a[i].S == 1){flagtmp = true; continue;}
			ans*=a[i];
		}
		if(flagtmp) ans*= a[k];
		prints(ans);
		return 0;
	}

	rep(i,k){
		if(negcnttmp <= 2){flag = true; nowi = i; break;}
//		if(i == k-2 && !negflag && (a[i].S == 1 || a[i+1].S == 1)){flag = true; nowi = i; break;}
//		if(i == k-2 && negflag && ((a[i].S == 1 && a[i+1].S == 1) || (a[i].S == 0 && a[i+1].S == 0))){flag = true; nowi = i; break;}
		ans*=a[i].F;
		if(a[i].S == 1) {negflag = !negflag; negcnt--;negcnttmp--;}

	}

	if(!flag) {prints(ans);return 0;}

	if(negflag){
			while(a[nowi].S!=1) {ans*= a[nowi].F; nowi++;}
			ans*=a[nowi];nowi++;
			negcnttmp--;negcnt--;
			negflag = false;
	}
	int nowtmp = nowi;
	ll negtmp = 1;
	int cnttmp = 0;
	while(cnttmp<2){
		if(a[nowtmp].S == 1){
			negtmp*=a[nowtmp].F;
			cnttmp++;
		}
		nowtmp++;
	}
	nowtmp = nowi;
	cnttmp = 0;
	while(nowi < k-2){
		if(a[nowtmp].S == 0){
			ans*=a[nowtmp].F;
			nowi++;
		}
		nowtmp++;
	}
	ll postmp = 1;
	while(nowi <k){
		if(a[nowtmp].S == 0){
			postmp*=a[nowtmp].F;
			nowi++;
		}
		nowtmp++;
	}
	ModInt<MOD> tmptmp = max(postmp,negtmp);
	ans*=tmptmp;
	prints(tmptmp);


/*

	int poscnt = 0;
	int negcnt = 0;

	rep(i,n) {
		if(a[i]>=0) {apos[poscnt] = a[i]; poscnt++;}
		else{aneg[negcnt] = -a[i]; negcnt++;}
	}
	sort(apos, apos+poscnt, greater<>());
	sort(aneg, aneg+negcnt, greater<>());

	ModInt<MOD> posrui[200005];
	ModInt<MOD> negrui[200005];


	posrui[0] = 1;
	negrui[0] = 1;

	rep1(i,poscnt){
		posrui[i] = posrui[i-1];
		posrui[i] *= apos[i-1];
	}
	rep1(i,negcnt){
		negrui[i] = negrui[i-1];
		negrui[i] *= aneg[i-1];
	}

	rep(i,negcnt/2){
		ModInt<MOD> anstmp = negrui[i*2];
		anstmp*=posrui[k-i*2];
		ans = max
	}




	if(negcnt <= 1) ans = posrui[k];
*/



	return 0;
}




Compiler output:
a.cc:97:24: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
   97 |                 ModInt(auto x) : v(x >= 0 ? x % P : (P - -x % P) % P) {}
      |                        ^~~~
a.cc:140:23: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
  140 |                 M pow(auto n) const
      |                       ^~~~
a.cc:150:36: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
  150 |                 friend M operator*(auto l, M r) { return M(l) *= r; }
      |                                    ^~~~
a.cc:151:36: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
  151 |                 friend M operator/(auto l, M r) { return M(l) /= r; }
      |                                    ^~~~
a.cc:152:36: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
  152 |                 friend M operator+(auto l, M r) { return M(l) += r; }
      |                                    ^~~~
a.cc:153:36: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
  153 |                 friend M operator-(auto l, M r) { return M(l) -= r; }
      |                                    ^~~~
a.cc:162:40: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
  162 |                 friend bool operator==(auto l, M r) { return M(l) == r; }
      |                                        ^~~~
a.cc:163:40: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
  163 |                 friend bool operator!=(auto l, M r) { return !(l == r); }
      |                                        ^~~~
a.cc: In instantiation of 'ModInt<P>::ModInt(auto:1) [with auto:1 = std::pair<long long int, long long int>; unsigned int P = 1000000007]':
a.cc:196:21:   required from here
  196 |                 rep(i,n) ans*= a[i];
      |                                   ^
a.cc:97:38: error: no match for 'operator>=' (operand types are 'std::pair<long long int, long long int>' and 'int')
   97 |                 ModInt(auto x) : v(x >= 0 ? x % P : (P - -x % P) % P) {}
      |                                    ~~^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/bits/specfun.h:43,
                 from /usr/include/c++/14/cmath:3906,
                 from a.cc:4:
/usr/include/c++/14/bits/stl_pair.h:1070:5: note: candidate: 'template<class _T1, class _T2> constexpr bool std::operator>=(const pair<_T1, _T2>&, const pair<_T1, _T2>&)'
 1070 |     operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:1070:5: note:   template argument deduction/substitution failed:
a.cc:97:38: note:   mismatched types 'const std::pair<_T1, _T2>' and 'int'
   97 |                 ModInt(auto x) : v(x >= 0 ? x % P : (P - -x % P) % P) {}
      |                                    ~~^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:67:
/usr/include/c++/14/bits/stl_iterator.h:476:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator>=(const reverse_iterator<_Iterator>&, const reverse_iterator<_Iterator>&)'
  476 |     operator>=(const reverse_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:476:5: note:   template argument deduction/substitution failed:
a.cc:97:38: note:   'std::pair<long long int, long long int>' is not derived from 'const std::reverse_iterator<_Iterator>'
   97 |                 ModInt(auto x) : v(x >= 0 ? x % P : (P - -x % P) % P) {}
      |                                    ~~^~~~
/usr/include/c++/14/bits/stl_iterator.h:520:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator>=(const reverse_iterator<_Iterator>&, const reverse_iterator<_IteratorR>&)'
  520 |     operator>=(const reverse_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:520:5: note:   template argument deduction/substitution failed:
a.cc:97:38: note:   'std::pair<long long int, long long int>' is not derived from 'const std::reverse_iterator<_Iterator>'
   97 |                 ModInt(auto x) : v(x >= 0 ? x % P : (P - -x % P) % P) {}
      |                                    ~~^~~~
/usr/include/c++/14/bits/stl_iterator.h:1724:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator>=(const move_iterator<_IteratorL>&, const move_iterator<_IteratorR>&)'
 1724 |     operator>=(const move_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1724:5: note:   template argument deduction/substitution failed:
a.cc:97:38: note:   'std::pair<long long int, long long int>' is not derived from 'const std::move_iterator<_IteratorL>'
   97 |                 ModInt(auto x) : v(x >= 0 ? x % P : (P - -x % P) % P) {}
      |                                    ~~^~~~
/usr/include/c++/14/bits/stl_iterator.h:1781:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator>=(const move_iterator<_IteratorL>&, const move_iterator<_IteratorL>&)'
 1781 |     operator>=(const move_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1781:5: note:   template argument deduction/substitution failed:
a.cc:97:38: note:   'std::pair<long long int, long long int>' is not derived from 'const std::move_iterator<_IteratorL>'
   97 |                 ModInt(auto x) : v(x >= 0 ? x % P : (P - -x % P) % P) {}
      |                                    ~~^~~~
In file included from /usr/include/c++/14/bits/basic_string.h:47,
                 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:6:
/usr/include/c++/14/string_view:739:5: note: candidate: 'template<class _CharT, class _Traits> constexpr bool std::operator>=(basic_string_view<_CharT, _Traits>, basic_string_view<_CharT, _Traits>)'
  739 |     operator>=(basic_string_view<_CharT, _Traits> __x,
      |     ^~~~~~~~
/usr/include/c++/14/string_view:739:5: note:   template argument deduction/substitution failed:
a.cc:97:38: note:   'std::pair<long long int, long long int>' is not derived from 'std::basic_string_view<_CharT, _Traits>'
   97 |                 ModInt(auto x) : v(x >= 0 ? x % P : (P - -x % P) % P) {}
      |                                    ~~^~~~
/usr/include/c++/14/string_view:746:5: note: candidate: 'template<class _CharT, class _Traits> constexpr bool std::operator>=(basic_string_view<_CharT, _Traits>, __type_identity_t<basic_string_view<_CharT, _Traits> >)'
  746 |     operator>=(basic_string_view<_CharT, _Traits> __x,
      |     ^~~~~~~~
/usr/include/c++/14/string_view:746:5: note:   template argument deduction/substitution failed:
a.cc:97:38: note:   'std::pair<long long int, long long int>' is not derived from 'std::basic_string_view<_CharT, _Traits>'
   97 |                 ModInt(auto x) : v(x >= 0 ? x % P : (P - -x % P) % P) {}
      |                                    ~~^~~~
/usr/include/c++/14/string_view:754:5: note: candidate: 'template<class _CharT, class _Traits> constexpr bool std::operator>=(__type_identity_t<basic_string_view<_CharT, _Traits> >, basic_string_view<_CharT, _Traits>)'
  754 |     operator>=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
      |     ^~~~~~~~
/usr/include/c++/14/string_view:754:5: note:   template argument deduction/substitution failed:
a.cc:97:38: note:   mismatched types 'std::basic_string_view<_CharT, _Traits>' and 'int'
   97 |                 ModInt(auto x) : v(x >= 0 ? x % P : (P - -x % P) % P) {}
      |                                    ~~^~~~
/usr/include/c++/14/bits/basic_string.h:3997:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> bool std::operator>=(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3997 |     operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3997:5: note:   template argument deduction/substitution failed:
a.cc:97:38: note:   'std::pair<long long int, long long int>' is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
   97 |                 ModInt(auto x) : v(x >= 0 ? x % P : (P - -x % P) % P) {}
      |                                    ~~^~~~
/usr/include/c++/14/bits/basic_string.h:4011:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> bool std::operator>=(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const _CharT*)'
 4011 |     operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:4011:5: note:   template argument deduction/substitution failed:
a.cc:97:38: note:   'std::pair<long long int, long long int>' is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
   97 |                 ModInt(auto x) : v(x >= 0 ? x % P : (P - -x % P) % P) {}
      |                                    ~~^~~~
/usr/include/c++/14/bits/basic_string.h:4024:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> bool std::operator>=(const _CharT*, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 4024 |     operator>=(const _CharT* __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:4024:5: note:   template argument deduction/substitution failed:
a.cc:97:38: note:   mismatched types 'const _CharT*' and 'std::pair<long long int, long long int>'
   97 |                 ModInt(auto x) : v(x >= 0 ? x % P : (P - -x % P) % P) {}
      |                                    ~~^~~~
In file included from /usr/include/c++/14/bits/me