Problem:
Score : 1100 points

Problem Statement

Let us consider a grid of squares with 10^9 rows and N columns. Let (i, j) be the square at the i-th column (1 \leq i \leq N) from the left and j-th row (1 \leq j \leq 10^9) from the bottom.

Snuke has cut out some part of the grid so that, for each i = 1, 2, ..., N, the bottom-most h_i squares are remaining in the i-th column from the left.
Now, he will paint the remaining squares in red and blue.
Find the number of the ways to paint the squares so that the following condition is satisfied:

Every remaining square is painted either red or blue.

For all 1 \leq i \leq N-1 and 1 \leq j \leq min(h_i, h_{i+1})-1, there are exactly two squares painted red and two squares painted blue among the following four squares: (i, j), (i, j+1), (i+1, j) and (i+1, j+1).

Since the number of ways can be extremely large, print the count modulo 10^9+7.

Constraints

1 \leq N \leq 100

1 \leq h_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 ... h_N

Output

Print the number of the ways to paint the squares, modulo 10^9+7.

Sample Input 1

9
2 3 5 4 1 2 4 2 1

Sample Output 1

12800

One of the ways to paint the squares is shown below:

#
## #
### #
#### ###
#########

Sample Input 2

2
2 2

Sample Output 2

6

There are six ways to paint the squares, as follows:

## ## ## ## ## ##
## ## ## ## ## ##

Sample Input 3

5
2 1 2 1 2

Sample Output 3

256

Every way to paint the squares satisfies the condition.

Sample Input 4

9
27 18 28 18 28 45 90 45 23

Sample Output 4

844733013

Remember to print the number of ways modulo 10^9 + 7.

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

// https://atcoder.jp/contests/agc026/tasks/agc026_d
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

const MOD: i64 = 1e9 as i64 + 7;

fn powmod(a: i64, p: i64, m: i64) -> i64 {
    let mut ret = 1i64;
    let mut aa = a;
    let mut pp = p;
    while pp >= 1 {
        if pp & 1 == 1 {
            ret *= aa;
            ret %= m;
        }
        aa = aa * aa % m;
        pp >>= 1;
    }
    ret
}

fn pow2(p: i64) -> i64 {
    powmod(2, p, MOD)
}

fn main() {
    input! {
        n: usize,
        a: [i64; n]
    };


    let mut aw = a.clone();
    aw.sort();

    let mut blocks = vec![];
    {
        let mut last = 0;
        for h in aw {
            if last < h {
                blocks.push(h - last);
            }
            last = h;
        }
    }
    let mut heights = vec![0; n];
    for i in 0..n {
        let mut cnt = 0;
        let mut sum = 0;
        for &b in &blocks {
            sum += b;
            cnt += 1;
            if sum == a[i] {
                heights[i] = cnt;
                break;
            }
        }
    }

    let hn = blocks.len();
    let mut dp = vec![vec![vec![0; 2]; hn+1]; n+1];
    dp[0][0][0] = 1;

    println!("{:?}", blocks);
    println!("{:?}", heights);

    for i in 0..n {
        for h in 0..hn+1 {
            for f in 0..2 {
                let base = dp[i][h][f];
                if base == 0 {
                    continue;
                }



                // [from, to]
                let mut fromh = 1;
                let mut toh = 0;

                let mut sameh = h;
                if i == 0 {
                    fromh = 1;
                    toh = heights[i];
                } else {
                    fromh = heights[i-1]+1;
                    toh = heights[i];
                    if (f == 0 && h > heights[i]) || (f == 1 && h >= heights[i]) {
                        sameh = 0;
                    }
                }

                // println!("{} {} {} {} / {} {} {}", i, h, f, base, fromh, toh, sameh);

                let prevways = if sameh == 0 { 2 } else { 1 };
                let mut totalh = 0;
                for hi in fromh..toh+1 {
                    totalh += blocks[hi-1];
                }

                if sameh == 0 {
                    for hi in fromh..toh+1 {
                        if hi >= 2 {
                            // between
                            dp[i+1][hi-1][1] += base * prevways % MOD * pow2(totalh-1) % MOD;
                            dp[i+1][hi-1][1] %= MOD;

                            // else
                            dp[i+1][hi][0] += base * prevways % MOD * (pow2(blocks[hi-1]-1) + MOD - 1) % MOD * pow2(totalh - blocks[hi-1]) % MOD;
                            dp[i+1][hi][0] %= MOD;
                        } else {
                            // else
                            dp[i+1][hi][0] += base % MOD * (pow2(blocks[hi-1]) + MOD - 2) % MOD * pow2(totalh - blocks[hi-1]) % MOD;
                            dp[i+1][hi][0] %= MOD;
                        }
                        totalh -= blocks[hi-1];
                    }

                    dp[i+1][0][0] += base * prevways % MOD;
                    dp[i+1][0][0] %= MOD;
                } else {
                    dp[i+1][h][f] += base * pow2(totalh);
                    dp[i+1][h][f] %= MOD;
                }
           }
        }
    }

    let mut total = 0;
    for h in 0..hn+1 {
        for f in 0..2 {
            total += dp[n][h][f];
            total %= MOD;
        }
    }
    println!("{}", total);
}