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 Go (1.14.1) (judge verdict: Wrong Answer)

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

var mod int64 = 1000000007

func main() {
	_, K := getStdinInt64_2()
	A := getStdinIntArr64()

	sort.Slice(A, func(i, j int) bool {
		var tmp1 = A[i]
		var tmp2 = A[j]
		if tmp1 < 0 {
			tmp1 = -tmp1
		}
		if tmp2 < 0 {
			tmp2 = -tmp2
		}
		return tmp1 > tmp2
	})

	var i int64 = 0
	var ans int64 = 1
	for i = 0; i < K; i++ {
		var tmp = A[i] * ans
		if tmp < 0 {
			ans = (A[i]*ans + mod) % mod
		} else {

			ans = A[i] * ans % mod
		}
	}

	sort.Slice(A, func(i, j int) bool {
		var tmp1 = A[i]
		var tmp2 = A[j]
		if tmp1 < 0 {
			tmp1 = -tmp1
		}
		if tmp2 < 0 {
			tmp2 = -tmp2
		}
		return tmp1 < tmp2
	})

	var ans2 int64 = 1
	for i = 0; i < K; i++ {
		var tmp = A[i] * ans2
		if tmp < 0 {
			ans2 = (A[i]*ans2 + mod) % mod
		} else {
			ans2 = A[i] * ans2 % mod
		}
	}

	if ans > ans2 {
		fmt.Printf("%d\n", ans)
	} else {
		fmt.Printf("%d\n", ans2)
	}
}

var sc = bufio.NewReaderSize(os.Stdin, 1024*1024*10)

func getStdin() string {
	return readLine()
}
func getStdinInt() int {
	str := getStdin()
	rtn, _ := strconv.Atoi(str)
	return rtn
}
func getStdinInt2() (int, int) {
	list := getStdinIntArr()
	return list[0], list[1]
}
func getStdinInt3() (int, int, int) {
	list := getStdinIntArr()
	return list[0], list[1], list[2]
}
func getStdinInt64() int64 {
	str := getStdin()
	rtn, _ := strconv.ParseInt(str, 10, 64)
	return rtn
}
func getStdinInt64_2() (int64, int64) {
	list := getStdinIntArr64()
	return list[0], list[1]
}
func getStdinInt64_3() (int64, int64, int64) {
	list := getStdinIntArr64()
	return list[0], list[1], list[2]
}
func getStdinIntArr() []int {
	str := getStdin()
	list := strings.Split(str, " ")
	rtn := make([]int, len(list))
	for idx, val := range list {
		rtn[idx], _ = strconv.Atoi(val)
	}
	return rtn
}
func getStdinIntArr64() []int64 {
	str := getStdin()
	list := strings.Split(str, " ")
	rtn := make([]int64, len(list))
	for idx, val := range list {
		rtn[idx], _ = strconv.ParseInt(val, 10, 64)
	}
	return rtn
}
func readLine() string {
	buf := make([]byte, 0, 0)
	for {
		l, p, _ := sc.ReadLine()
		buf = append(buf, l...)
		if !p {
			break
		}
	}
	return string(buf)
}