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 Rust (1.42.0) (judge verdict: Accepted)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
prog_comp::init!();

unsafe fn solve(#[allow(unused_variables)] io: &mut Io) {
    let n = io.ru();
    let k = io.ru();
    let mut a = io.ci(n);
    a.sort_unstable_by_key(|i| i.abs());

    let mut sign = false;
    for i in 0..k {
        if a[n - 1 - i] < 0 {
            sign = !sign;
        }
    }

    if !sign {
        let mut prod = ModInt::<1000000007>::ONE;
        for i in 0..k {
            prod *= a[n - 1 - i].abs() as usize;
        }
        io.w(prod);
        return;
    }

    let x1 = {
        let mut i = n - k;

        loop {
            if a[i] < 0 {
                break Some(i);
            }

            if i != n - 1 {
                i += 1;
            } else {
                break None;
            }
        }
    };
    let x2 = {
        if n == k {
            None
        } else {
            let mut i = n - 1 - k;

            loop {
                if a[i] > 0 {
                    break Some(i);
                }

                if i != 0 {
                    i -= 1;
                } else {
                    break None;
                }
            }
        }
    };

    let y1 = {
        let mut i = n - k;

        loop {
            if a[i] > 0 {
                break Some(i);
            }

            if i != n - 1 {
                i += 1;
            } else {
                break None;
            }
        }
    };
    let y2 = {
        if n == k {
            None
        } else {
            let mut i = n - 1 - k;

            loop {
                if a[i] < 0 {
                    break Some(i);
                }

                if i != 0 {
                    i -= 1;
                } else {
                    break None;
                }
            }
        }
    };

    let x = if let (Some(x1), Some(x2)) = (x1, x2) {
        let mut x = ModInt::<1000000007>::ONE;
        for i in 0..k {
            if n - 1 - i != x1 {
                x *= a[n - 1 - i].abs() as usize;
            }
        }
        x *= a[x2].abs() as usize;
        Some(x)
    } else {
        None
    };

    let y = if let (Some(y1), Some(y2)) = (y1, y2) {
        let mut y = ModInt::<1000000007>::ONE;
        for i in 0..k {
            if n - 1 - i != y1 {
                y *= a[n - 1 - i].abs() as usize;
            }
        }
        y *= a[y2].abs() as usize;
        Some(y)
    } else {
        None
    };

    if let Some(x) = x {
        if let Some(y) = y {
            if a[x2.unwrap()].abs() * a[y1.unwrap()].abs()
                > a[y2.unwrap()].abs() * a[x1.unwrap()].abs()
            {
                io.w(x);
            } else {
                io.w(y);
            }
        } else {
            io.w(x);
        }
        return;
    } else {
        if let Some(y) = y {
            io.w(y);
            return;
        }
    }

    let mut prod = ModInt::<1000000007>::ONE;
    for i in 0..k {
        prod *= ModInt::from_signed(a[i]);
    }
    io.w(prod);
}
*/

fn main() {
    let exe = "/tmp/bin713A6B78";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";