Problem:
Score : 500 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

We will choose exactly K of these elements. Find the maximum possible product of the chosen elements.

Then, print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Constraints

1 \leq K \leq N \leq 2\times 10^5

|A_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 \ldots A_N

Output

Print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Sample Input 1

4 2
1 2 -3 -4

Sample Output 1

12

The possible products of the two chosen elements are 2, -3, -4, -6, -8, and 12, so the maximum product is 12.

Sample Input 2

4 3
-1 -2 -3 -4

Sample Output 2

1000000001

The possible products of the three chosen elements are -24, -12, -8, and -6, so the maximum product is -6.

We print this value modulo (10^9+7), that is, 1000000001.

Sample Input 3

2 1
-1 1000000000

Sample Output 3

1000000000

The possible products of the one chosen element are -1 and 1000000000, so the maximum product is 1000000000.

Sample Input 4

10 10
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1

Sample Output 4

999983200

Be sure to print the product modulo (10^9+7).

Solution in C# (.NET Core 3.1.201) (judge verdict: Runtime Error)

using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Math;

class MainClass
{
    static void Main()
    {
        var NK = ReadLine().Split().Select(int.Parse).ToArray();
        var N = NK[0];
        var K = NK[1];
        var A = ReadLine().Split().Select(long.Parse).ToArray();
        Array.Sort(A,(x,y)=>Abs(x).CompareTo(Abs(y)));
        Array.Reverse(A);
        if (N == K)
        {
            WriteLine(CalcFront(A,K));
        }
        else
        {
            var d = new int[N+1];
            for (int i = 0; i < N; i++)
            {
                d[i + 1] = d[i];
                if (A[i] < 0) d[i + 1]++;
            }
            
            if (d[N] == N && K % 2 == 0)
            {
                WriteLine(CalcFront(A,K));
            }
            else if(d[N] ==N&& K % 2 == 1)
            {
                WriteLine(CalcBack(A,K));
            }
            else
            {
                if(d[K]%2==0)
                {
                    WriteLine(CalcFront(A,K));
                }
                else
                {
                    int v1 = -1, v2 = -1, v3 = -1, v4 = -1;
                    double value1 = -1, value2 = -1;
                    if (d[K] < K && (d[N] - d[K + 1]) >= 1)
                    {
                        for (int i = K - 1; i >= 0; i--)
                        {
                            if (A[i] >= 0)
                            {
                                v1 = i;
                                break;
                            }
                        }
                        for (int i = K; i < N; i++)
                        {
                            if (A[i] < 0)
                            {
                                v2 = i;
                                break;
                            }
                        }
                        if (v1 == 0) value1 = 0;
                        else value1 = (double)-A[v2] / A[v1];
                    }
                    if (d[K] != 0 && (d[N] - d[K + 1]) < N - K)
                    {
                        for (int i = K - 1; i >= 0; i--)
                        {
                            if (A[i] < 0)
                            {
                                v3 = i;
                                break;
                            }
                        }
                        for (int i = K; i < N; i++)
                        {
                            if (A[i] >= 0)
                            {
                                v4 = i;
                                break;
                            }
                        }
                        value2 = (double)-A[v4] / A[v3];
                    }
                    if (value1 == value2 && value2 == -1)
                    {
                        WriteLine("error");
                    }
                    if (value1 > value2)
                    {
                        A[v1] = A[v2];
                    }
                    else
                    {
                        A[v3] = A[v4];
                    }

                    WriteLine(CalcFront(A,K));
                }
                
            }
        }
        
    }
    static int CalcFront(long[] A,int K)
    {
        Modular ans = 1;
        for (int i = 0; i < K; i++)
        {
            ans *= A[i];
        }
        return (int)ans;
    }
    static int CalcBack(long[] A,int K)
    {
        Modular ans = 1;
        for (int i = A.Length - 1; i >= A.Length-K; i--)
        {
            ans *= A[i];
        }
        return (int)ans;
    }
}
class Modular
{
    private const int M = 1000000007;
    private long value;
    public Modular(long value = 0) { this.value = value; }
    public static implicit operator Modular(long a)
    {
        var m = a % M;
        return new Modular((m < 0) ? m + M : m);
    }
    public static Modular operator +(Modular a, Modular b)
    => a.value + b.value;
    public static Modular operator -(Modular a, Modular b)
    => a.value - b.value;
    public static Modular operator *(Modular a, Modular b)
    => a.value * b.value;
    public static Modular Pow(Modular a, int n)
    {
        switch (n)
        {
            case 0:
                return 1;
            case 1:
                return a;
            default:
                var p = Pow(a, n / 2);
                return p * p * Pow(a, n % 2);
        }
    }
    public static Modular operator /(Modular a, Modular b)
    {
        return a * Pow(b, M - 2);
    }
    private static readonly List<int> facs = new List<int> { 1 };
    public static Modular Fac(int n)   //階乗
    {
        for (int i = facs.Count; i <= n; ++i)
        {
            facs.Add((int)(Math.BigMul(facs.Last(), i) % M));
        }
        return facs[n];
    }
    public static Modular Fac(int r, int n)
    {
        int temp = 1;
        for (int i = r; i <= n; i++)
        {
            temp = (int)(Math.BigMul(temp, i) % M);
        }
        return temp;
    }
    public static Modular Ncr(int n, int r) //nCr
    {
        return (n < r) ? 0
             : (n == r) ? 1
                        : (n < 1000000) ? Fac(n) / (Fac(r) * Fac(n - r))
                            : Fac(n - r + 1, n) / Fac(r);
    }
    public static explicit operator int(Modular a)
    {
        return (int)a.value;
    }
}