Language: C++

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<n; i++)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
template<class A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}cout << "\n";}
ld PI=3.14159265358979323846;

const int mod = 1000000007;
class mint {
    long long x;
public:
    mint(long long x=0) : x((x%mod+mod)%mod) {}
    mint operator-() const { 
      return mint(-x);
    }
    mint& operator+=(const mint& a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint& a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const  mint& a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint& a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint& a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint& a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint& a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint& a) const {
        mint res(*this);
        return res/=a;
    }
 
    friend ostream& operator<<(ostream& os, const mint& m){
        os << m.x;
        return os;
    }
};

int main(){
    ll N,K;
    cin >> N >> K;
    deque<ll> A(N);
    ll cnt = 0;
    ll pn = 0;
    ll mn = 0;
    rep(i,N){
        cin >> A[i];
        if(A[i] == 0) cnt++;
        if(A[i] > 0) pn++;
        if(A[i] < 0) mn++;
    }
    deque<Pll> B(N);
    rep(i,N){
        B[i].first = abs(A[i]);
        B[i].second = ((A[i]<0)?1:0);
    }
    sort(B.rbegin(), B.rend());
    if(cnt > N-K){
        cout << 0 << endl;
        return 0;
    }
    sort(A.rbegin(), A.rend());
    ll mc = 0;
    mint tmp = 1;
    rep(i,K){
        tmp *= B[i].first;
        mc += B[i].second;
    }
    if(mc % 2 == 0){
        cout << tmp << endl;
        return 0;
    }else{
        ll l = 0;
        ll r = K;
        while(mc % 2 == 1 && r < N){
            tmp /= B[l].first;
            tmp *= B[r].first;
            mc -= B[l].second;
            mc += B[r].second;
        }
        if(mc % 2==0){
            cout << tmp << endl;
            return 0;
        }else{
            mint ans = 1;
            rep(i,K){
                ans *= mint(B[N-1-i]);
            }
            cout << ans << endl;
            return 0;
        }
    }
    /*
    if(mn % 2 == 0){
        if(A.front() >= A.back()){
            if(A.front() < 0){
                mc++;
                tmp *= A.front();
            }
            A.pop_front();
        }else{
            if(A.back() < 0){
                mc++;
                tmp *= A.back();
            }
            A.pop_back();
        }
    }
    /*
    vector<ll> v(2*N);
    rep(i,2*N){
        v[i] = A[i%N];
    }
    ll l = 0;
    ll r = K;
    mint tmp = 1;
    mint ans = 0;
    rep(i,r){
        tmp *= v[i];
    }
    ans = tmp;
    while(r<2*N){

        r++;
        l++;
    }
    mint ans = 0;
    mint tmp = 1;
    ll l = 0;
    ll r = K;
    if(mn % 2 == 0){

    }
    if(pn >= K){
        rep(i,K){
            tmp *= A[i];
        }
        cout << tmp << endl;
        return 0;
    }else if(pn < K && mn % 2 == 0){
        rep(i,ll(pn/2)*2){
            tmp *= A[i];
        }
        rep(i,K-(ll(pn/2)*2)){
            tmp *= A[N-1-i];
        }
        cout << tmp << endl;
    }else{
        if(cnt > 0){
            cout << 0 << endl;
        }else{
            
            sort(B.begin(), B.end());
            rep(i,K){
                tmp *= B[i];
            }
            tmp = mint(0) - tmp;
            cout << tmp << endl;
        }
    }

    cout << N << endl;*/
}


Compiler output:
a.cc: In function 'int main()':
a.cc:120:37: error: no matching function for call to 'mint::mint(__gnu_cxx::__alloc_traits<std::allocator<std::pair<long long int, long long int> >, std::pair<long long int, long long int> >::value_type&)'
  120 |                 ans *= mint(B[N-1-i]);
      |                                     ^
a.cc:19:5: note: candidate: 'mint::mint(long long int)'
   19 |     mint(long long x=0) : x((x%mod+mod)%mod) {}
      |     ^~~~
a.cc:19:20: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::pair<long long int, long long int> >, std::pair<long long int, long long int> >::value_type' {aka 'std::pair<long long int, long long int>'} to 'long long int'
   19 |     mint(long long x=0) : x((x%mod+mod)%mod) {}
      |          ~~~~~~~~~~^~~
a.cc:16:7: note: candidate: 'constexpr mint::mint(const mint&)'
   16 | class mint {
      |       ^~~~
a.cc:16:7: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::pair<long long int, long long int> >, std::pair<long long int, long long int> >::value_type' {aka 'std::pair<long long int, long long int>'} to 'const mint&'
a.cc:16:7: note: candidate: 'constexpr mint::mint(mint&&)'
a.cc:16:7: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::pair<long long int, long long int> >, std::pair<long long int, long long int> >::value_type' {aka 'std::pair<long long int, long long int>'} to 'mint&&'