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: Accepted)

const MOD: i64 = 1_000_000_007;

fn pow(a: i64, b: i64) -> i64 {
    if b == 0 {
        1
    } else {
        let t = pow(a, b/2);
        let t = t * t % MOD;
        if b % 2 == 0 {
            t
        } else {
            t * a % MOD
        }
    }
}

fn solve(h: Vec<i64>) -> (i64, i64) {
    //println!("{:?}", h);
    let hmin = *h.iter().min().unwrap();
    let mut dp0 = 1;
    let mut dp1 = 1;
    let mut k = Vec::new();
    let mut cnt = 0;
    for i in 0..(h.len()) {
        let hi = h[i];
        if i + 1 < h.len() && (hi == hmin || h[i+1] == hmin) {
            cnt += 1;
        }
        if hi == hmin {
            if k.len() >= 1 {
                let (mut tmp0, mut tmp1) = solve(k.clone());
                tmp0 += tmp1;
                tmp1 *= 2;
                dp0 *= tmp0;
                dp0 %= MOD;
                dp1 *= tmp1;
                dp1 %= MOD;
                k.clear();
            }
        } else {
            k.push(hi - hmin);
        }
    }
            if k.len() >= 1 {
                let (mut tmp0, mut tmp1) = solve(k.clone());
                tmp0 += tmp1;
                tmp1 *= 2;
                dp0 *= tmp0;
                dp0 %= MOD;
                dp1 *= tmp1;
                dp1 %= MOD;
                k.clear();
            }

    //println!("{:?} {:?}", dp0, dp1);
    let ans1 = dp1 * pow(2, hmin - 1) % MOD;
    let ans0 = dp0 * pow(2, cnt) - dp1 + ans1;
    let ans0 = (ans0 % MOD + MOD) % MOD;
    //println!("{:?} {:?}", ans0, ans1);
    return (ans0, ans1);
}

fn main() {
    let cin = stdin();
    let mut sc = Scanner::new(cin.lock());
    let n = sc.read();
    let h = (0..n).map(|_| sc.read()).collect();
    println!("{}", solve(h).0 * 2 % MOD);
}


// thanks, ir5
use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;

struct Scanner<'a> {
    cin: StdinLock<'a>,
}

impl<'a> Scanner<'a> {
    fn new(cin: StdinLock<'a>) -> Scanner<'a> {
        Scanner { cin: cin }
    }

    fn read1<T: FromStr>(&mut self) -> Option<T> {
        let token = self.cin
            .by_ref()
            .bytes()
            .map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        token.parse::<T>().ok()
    }

    fn read<T: FromStr>(&mut self) -> T {
        self.read1().unwrap()
    }
}