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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

map<pair<VI,int>,ll> hs;
int n,h[110];
ll solve(VI x,int ty) {
	if (x.empty()) return 1;
	if (hs.count(mp(x,ty))) return hs[mp(x,ty)];
	int v=*min_element(all(x));
	if (ty==0) {
		ll ans=1;
		if (v==1) {
			VI c;
			for (auto p:x) {
				if (p==1) {
					ans=ans*2%mod;
					ans=ans*solve(c,0)%mod;
					c.clear();
				} else {
					c.pb(p);
				}
			}
			ans=ans*solve(c,0)%mod;
			return hs[mp(x,ty)]=ans;
		} else {
			VI y=x;
			rep(i,0,SZ(y)) y[i]-=v-1;
			ll all=solve(y,0);
			ll dif=solve(y,1)*2%mod;
			all=(all-dif+mod)%mod;
			return hs[mp(x,ty)]=(all+dif*powmod(2,v-1))%mod;
		}
	} else {
		// RBRB
		ll ans=1;
		if (v==1) {
			VI c;
			for (auto p:x) {
				if (p==1) {
					ans=ans*solve(c,1)%mod;
					c.clear();
				} else {
					c.pb(p);
				}
			}
			ans=ans*solve(c,1)%mod;
			return hs[mp(x,ty)]=ans;
		} else {
			VI y=x;
			rep(i,0,SZ(y)) y[i]-=v-1;
			ll dif=solve(y,1);
			return hs[mp(x,ty)]=dif*powmod(2,v-1)%mod;	
		}
	}
}

int main() {
	scanf("%d",&n);
	rep(i,0,n) scanf("%d",h+i);
	printf("%lld\n",solve(VI(h,h+n),0));
}