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: Wrong Answer)

using System;
using System.Numerics;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace debug
{
    class main
    {
        static void Main(string[] args)
        {
            //問題クラスを展開
            ProgramE a = new ProgramE();
            a.main();//実行する

        }

    }
    //ABC170
    class ProgramA
    {
        public void main()
        {
            //入力
            int n = int.Parse(Console.ReadLine());

            if(n < 1000)
            {
                Console.WriteLine(1000 - n);
            }
            else
            {
                int m = n / 1000 + 1;
                if (n % 1000 == 0)
                    Console.WriteLine(0);
                else
                    Console.WriteLine(m * 1000 - n);

            }

        }
    }

    class ProgramB
    {
        public void main()
        {
            //入力
            int n = int.Parse(Console.ReadLine());
            int[] ans = new int[4];

            for(int i = 0; i < n;i++)
            {
                string s = Console.ReadLine();
                if (s == "AC")
                    ans[0]++;
                else if (s == "WA")
                    ans[1]++;
                else if (s == "TLE")
                    ans[2]++;
                else
                    ans[3]++;
            }

            Console.WriteLine("AC x " + ans[0]);
            Console.WriteLine("WA x " + ans[1]);
            Console.WriteLine("TLE x " + ans[2]);
            Console.WriteLine("RE x " + ans[3]);

        }
    }

    class ProgramC
    {
        public void main()
        {

            //入力
            string[] s = Console.ReadLine().Split(' ');
            int h = int.Parse(s[0]);
            int w = int.Parse(s[1]);
            int k = int.Parse(s[2]);

            char[,] maze = new char[h, w];

            for(int i = 0;i < h;i++)
            {
                string t = Console.ReadLine();
                for (int j = 0; j < w; j++)
                    maze[i, j] = t[j];
            }
            int ans = 0;

            for(int i = 0;i < (1 << h);i++)
                for (int j = 0; j < (1 << w); j++)
                {
                    int count = 0;
                    for(int x = 0; x < h;x++)
                    {
                        if (((i >> x) & 1) == 1)
                            continue;

                        for(int y = 0;y < w;y++)
                        {
                            if (((j >> y) & 1) == 1)
                                continue;

                            if (maze[x, y] == '#')
                                count++;
                        }
                    }

                    if (count == k)
                        ans++;
                }

            Console.WriteLine(ans);



        }
    }

    class ProgramD
    {
        public void main()
        {

            //入力
            long n = long.Parse(Console.ReadLine());
            string[] s = Console.ReadLine().Split(' ');
            long[] a = new long[n];


            for (int i = 0; i < n; i++)
                a[i] = long.Parse(s[i]);

            Array.Sort(a);

            long sum = 0;

            for (int i = 0; i < n -1; i++)
                sum += a[n - 1 - (i + 1)/2];

            
            Console.WriteLine(sum);
        }
    }

    class ProgramE
    {

        public void main()
        {
            //入力
            string[] t = Console.ReadLine().Split(' ');
            int n = int.Parse(t[0]);
            int k = int.Parse(t[1]);
            string[] s = Console.ReadLine().Split(' ');
            long[] a = new long[n];

            for (int i = 0; i < n; i++)
                a[i] = long.Parse(s[i]);

            long MOD = 1000000000 + 7;

            Array.Sort(a);

            //0,-,+の数を数える
            long minus = 0;
            long plus = 0;
            long zero = 0;

            for (int i = 0; i < n;i++)
            {
                if (a[i] < 0)
                    minus++;
                else if (a[i] == 0)
                    zero++;
                else
                    plus++;
            }

            //もしゼロが必ず含まれるならゼロ
            if (n - zero < k)
            {
                Console.WriteLine("0");
                return;
            }

            //マイナスのみ
            if(plus == 0)
            {
                long ans = 1;
                if(k % 2 == 0)
                {
                    for (int i = 0; i < k; i++)
                    {
                        ans *= a[i];
                        ans %= MOD;
                    }
                }
                else
                {
                    for (int i = 0; i < k; i++)
                    {
                        ans *= a[minus - 1 - i];
                        ans %= MOD;
                    }

                }

                if (ans < 0)
                    ans += MOD;

                Console.WriteLine(ans);
                return;
            }

            //プラスのみ
            if (minus == 0)
            {
                long ans = 1;
                for (int i = 0; i < k; i++)
                {
                    ans *= a[n - 1 -i];
                    ans %= MOD;
                }
                
                Console.WriteLine(ans);
                return;
            }

            //プラスだけでもどうにかなるとき
            if (k <= plus)
            {
                int num_m = 0;
                for (int i = 0; i < k; i++)
                {
                    if (a[n - 1 - k + i] < Math.Abs(a[i]))
                        num_m++;
                }

                if (num_m % 2 == 1)
                    num_m--;

                long ans = 1;
                for (int i = 0; i < num_m; i++)
                {
                    ans *= a[i];
                    ans %= MOD;
                }

                for (int i = 0; i < k - num_m; i++)
                {
                    ans *= a[n - 1 -i];
                    ans %= MOD;
                }

                Console.WriteLine(ans);
                return;

            }

            //プラスマイナス混合で考えるとき
            if (k > plus)
            {
                long num_m = k - plus;
                long num_p =plus;

                if (num_m % 2 == 1)
                {
                    num_m++;
                    num_p--;

                }
                long tp = num_p;
                long tm = num_m;
                for (int i = 0;i < tp;i++)
                {
                    if (i + tm > minus)
                        break;

                    if (a[n - 1 - tp + i] < Math.Abs(a[i + tm]))
                    {
                        num_m++;
                        num_p--;
                    }
                    
                }

                if(num_m % 2 == 1)
                {
                    num_m--;
                    num_p++;
                }

                long ans = 1;
                for (int i = 0; i < num_m; i++)
                {
                    ans *= a[i];
                    ans %= MOD;
                }

                for (int i = 0; i < num_p; i++)
                {
                    ans *= a[n - 1 - i];
                    ans %= MOD;
                }

                Console.WriteLine(ans);
                return;

            }







        }


    }

}