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>
#define clr(x) memset(x,0,sizeof x)
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define Fod(i,b,a) for (int i=(b);i>=(a);i--)
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define fi first
#define se second
#define outval(x) cerr<<#x" = "<<x<<endl
#define outtag(x) cerr<<"-----------------"#x"-----------------\n"
#define outarr(a,L,R) cerr<<#a"["<<L<<".."<<R<<"] = ";\
                    For(_x,L,R) cerr<<a[_x]<<" ";cerr<<endl;
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int,int> pii;
LL read(){
    LL x=0,f=0;
    char ch=getchar();
    while (!isdigit(ch))
        f=ch=='-',ch=getchar();
    while (isdigit(ch))
        x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return f?-x:x;
}
const int N=105,mod=1e9+7,INF=mod+233;
int Pow(int x,int y){
	int ans=1;
	for (;y;y>>=1,x=(LL)x*x%mod)
		if (y&1)
			ans=(LL)ans*x%mod;
	return ans;
}
void Add(int &x,int y){
	if ((x+=y)>=mod)
		x-=mod;
}
void Del(int &x,int y){
	if ((x-=y)<0)
		x+=mod;
}
int Add(int x){
	return x>=mod?x-mod:x;
}
int Del(int x){
	return x<0?x+mod:x;
}
int n;
int a[N];
int g[N][N],f[N][N];
void dfs(int L,int R,int h){
	if (L==R){
		g[L][R]=Pow(2,a[L]-h);
		f[L][R]=0;
//		printf("dfs(%d,%d,%d): ",L,R,h);
//		printf("g = %d, f = %d\n",g[L][R],f[L][R]);
		return;
	}
	int mi=INF;
	For(i,L,R)
		mi=min(mi,a[i]);
	int las=L;
	For(i,L,R){
		if (a[i]==mi){
			if (las<i){
				dfs(las,i-1,mi);
				if (las==L){
					g[L][R]=g[las][i-1];
					f[L][R]=f[las][i-1];
					las=i;
				}
				else {
					int g0=g[L][R],g1=g[las][i-1];
					int f0=f[L][R],f1=f[las][i-1];
					g[L][R]=(LL)g0*g1%mod;
					f[L][R]=0;
					Add(f[L][R],(LL)g0*g1%mod);
					Add(f[L][R],(LL)f0*g1*2%mod);
					Add(f[L][R],(LL)g0*f1*2%mod);
					Add(f[L][R],(LL)f0*f1*2%mod);
				}
			}
			dfs(i,i,mi);
			if (las==L){
				g[L][R]=g[i][i];
				f[L][R]=f[i][i];
			}
			else {
				int g0=g[L][R],g1=g[i][i];
				int f0=f[L][R],f1=f[i][i];
				g[L][R]=(LL)g0*g1%mod;
				f[L][R]=0;
				Add(f[L][R],(LL)g0*g1%mod);
				Add(f[L][R],(LL)f0*g1*2%mod);
				Add(f[L][R],(LL)g0*f1*2%mod);
				Add(f[L][R],(LL)f0*f1*2%mod);
			}
			las=i+1;
		}
	}
	if (las<=R){
		dfs(las,R,mi);
		int g0=g[L][R],g1=g[las][R];
		int f0=f[L][R],f1=f[las][R];
		g[L][R]=(LL)g0*g1%mod;
		f[L][R]=0;
		Add(f[L][R],(LL)g0*g1%mod);
		Add(f[L][R],(LL)f0*g1*2%mod);
		Add(f[L][R],(LL)g0*f1*2%mod);
		Add(f[L][R],(LL)f0*f1*2%mod);
	}
	g[L][R]=(LL)g[L][R]*Pow(2,mi-h)%mod;
//	printf("dfs(%d,%d,%d): ",L,R,h);
//	printf("g = %d, f = %d\n",g[L][R],f[L][R]);
}
int main(){
	n=read();
	For(i,1,n)
		a[i]=read();
	dfs(1,n,1);
	int ans=Add(g[1][n]+f[1][n]);
	Add(ans,ans);
	cout<<ans<<endl;
	return 0;
}