Language: C++

#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <algorithm>
#include <map>
#include <utility>

using namespace std;
const long nPrime = 1000000007;

//素数の逆元
long Inverse(const long &n){
    //nPrime = 1000000007
    if (n % nPrime == 0){
        return 0;
    }
    long a = n, b = nPrime, u = 1, v = 0;
    while (b > 0) {
        long t = a / b;
        a -= t * b;
        swap(a, b);
        u -= t * v;
        swap(u, v);
    }
    u %= nPrime;
    if (u < 0){
        u += nPrime;
    }
    return u;    
}

int main(){
    long n,k;
    scanf("%ld %ld", &n, &k);
  
    if(n == k){
        long nMulti= 1;
        for(long i = 0; i < n; i++){
            long a;
            scanf("%ld", &a);
            nMulti *= a;
            nMulti %= nPrime;
            
        }
        if(nMulti < 0){
            nMulti += nPrime;
        }
        printf("%ld", nMulti);
        return 0;
    }
  
    multimap<long, int> mNum;
    vector<long> viNum,viNumPlus,viNumMinus;
    long nCount = 0;
    for(long i = 0; i < n; i++){
        long a;
        scanf("%ld", &a);
        if(a > 0){
            mNum.insert(make_pair(a,1));
        }
        else if(a < 0){
            mNum.insert(make_pair(-a,-1));
            nCount++;
        }
    }
    if(mNum.size() < k){
        printf("0");
        return 0;
    }
    long nAns = 1;
    if(nCount == n && k % 2 == 1){ //負確定 小さい順にかける
        multimap<long, int>::iterator itr1 = mNum.begin();
        for(long i = 0; i < k; i++){
            nAns *= itr1->first;
            nAns %= nPrime;
            itr1++;
        }
        nAns = nPrime - nAns;
        nAns %= nPrime;
        printf("%ld", nAns);
        return 0;
    }
        
    long nMinusCount = 0;
    long nkPlusMin = 0;   //かける奴の中で最小の正
    long nkMinusMin = 0;   //かける奴の中で最小の負
    multimap<long, int>::reverse_iterator itr = mNum.rbegin();
    for(long i = 0; i < k;i++){  //大きい順にかける
        nAns *= itr->first;
        nAns %= nPrime;
        if(itr->second == 1){
            nkPlusMin = itr->first;
        }
        else{
            nkMinusMin = itr->first;
            nMinusCount++;
        }
        itr++;
    }
    if(nAns < 0){
        nAns += nPrime;
    }
    
    if(nMinusCount % 2 == 0){
        printf("%ld", nAns);
        return 0;
    }
    long nOutPlusMax = 0;
    long nOutMinusMax = 0;
    for(long int i = 0; itr!=mNum.rend(); i++){
        if(itr->second == 1 && nOutPlusMax == 0){
            nOutPlusMax = itr->first;
        }
        if(itr->second == -1 && nOutMinusMax == 0){
            nOutMinusMax = itr->first;
        }
        itr++;        
    }
    
    if(nOutPlusMax == 0){
        if(nkPlusMin == 0){
            printf("0");
            return 0;
        }
        nAns *= Inverse(nkPlusMin);
        nAns %= nPrime;
        nAns *= nOutMinusMax;
        nAns %= nPrime;
        if(nAns < 0){
            nAns += nPrime;
        }
        printf("%ld", nAns);
        return 0;   
    }
    if(nOutMinusMax == 0){
        nAns *= Inverse(nkMinusMin);
        nAns %= nPrime;
        nAns *= nOutPlusMax;
        nAns %= nPrime;
        if(nAns < 0){
            nAns += nPrime;
        }
        printf("%ld", nAns);
        return 0;    
    }
  
    long double d1 =  long double(nkPlusMin) / long double(nOutMinusMax);
    long double d2 =  long double(nkMinusMin) / long double(nOutPlusMax);    
    if(d1 < d2){
        if(nkPlusMin == 0){
            nAns *= Inverse(nkMinusMin);
            nAns %= nPrime;
            nAns *= nOutPlusMax;
            nAns %= nPrime;
            if(nAns < 0){
                nAns += nPrime;
            }
            printf("%ld", nAns);
            return 0;
        }
        nAns *= Inverse(nkPlusMin);
        nAns %= nPrime;
        nAns *= nOutMinusMax;
        nAns %= nPrime;
        if(nAns < 0){
            nAns += nPrime;
        }
        printf("%ld", nAns);
        return 0; 
    }
    else{
        nAns *= Inverse(nkMinusMin);
        nAns %= nPrime;
        nAns *= nOutPlusMax;
        nAns %= nPrime;
        if(nAns < 0){
            nAns += nPrime;
        }
            printf("%ld", nAns);
            return 0;   
    }
}


Compiler output:
a.cc: In function 'int main()':
a.cc:148:23: error: expected primary-expression before 'long'
  148 |     long double d1 =  long double(nkPlusMin) / long double(nOutMinusMax);
      |                       ^~~~
a.cc:149:23: error: expected primary-expression before 'long'
  149 |     long double d2 =  long double(nkMinusMin) / long double(nOutPlusMax);
      |                       ^~~~