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

#include<bits/stdc++.h>
#define ll long long
#define ljc 1000000007
#define inv2 500000004
using namespace std;
#define gc getchar
inline ll read(){
    register ll x=0,f=1;char ch=gc();
    while (!isdigit(ch)){if (ch=='-') f=-1;ch=gc();}
    while (isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=gc();}
    return (f==1)?x:-x;
}
inline ll fast_pow(ll a,ll b,ll p){
	ll t=1;
	if (b==-1) return 0;
	while (b){
		if (b&1ll) t=t*a%p;
		b>>=1ll;a=a*a%p;
	}
	return t;
}
int h[201010],n;
namespace ST{
	int f[201010][20],id[201010][20];
	inline pair<int,int> query(int l,int r){
		if (l>r) return make_pair(1e9+1,1e9+1);
		int k=log2(r-l+1);
		if (f[l][k]<=f[r-(1<<k)+1][k]){
			return make_pair(f[l][k],id[l][k]);
		}else{
			return make_pair(f[r-(1<<k)+1][k],id[r-(1<<k)+1][k]);
		}
	}
	inline void init(){
		for (int i=1;i<=n;i++) f[i][0]=h[i],id[i][0]=i;
		for (int j=1;j<=19;j++){
			for (int i=1;i+(1<<j)-1<=n;i++){
				int l=i,r=i+(1<<(j-1));
				if (f[l][j-1]<=f[r][j-1]){
					id[l][j]=id[l][j-1];
					f[l][j]=f[l][j-1];
				}else{
					id[l][j]=id[r][j-1];
					f[l][j]=f[r][j-1];
				}
			}
		}
		return;
	}
}
struct node{
	ll x,y;
};
inline node init(ll xx,ll yy){
	node x;x.x=xx,x.y=yy;return x;
}
inline node dp(int l,int r,int H){
	if (l>r) return init(0,0);
//	if (l>r) return init(0,0,0);
//	if (l==r) return init(0,fast_pow(2,h[l]-H-1,ljc),fast_pow(2,h[l]-H-1,ljc));
//	ll resr=0,resb=0,res1=0;
//	int id=ST::query(l,r).second;
//	node ls=dp(l,id-1,h[id]),rs=dp(id+1,r,h[id]);
//	if (id==l){
//		res1=rs.x*2ll%ljc;
//		resb=rs.R;
//		resr=rs.B;
//	}else if (id==r){
//		res1=ls.x*2ll%ljc;
//		resb=ls.B;
//		resr=ls.R;
//	}else{
//		if ((id-l)%2==0){
//			ll tmp=ls.R*rs.B%ljc+ls.B*rs.R%ljc;
//			if (h[id]==H) resb=resr=tmp;
//			else resr=resb=tmp*fast_pow(2ll,h[id]-H-1,ljc)%ljc;
//		}else{
//			ll tmp=ls.B*rs.B%ljc+ls.R*rs.R%ljc;
//			if (h[id]==H) resb=resr=tmp;
//			else resr=resb=tmp*fast_pow(2ll,h[id]-H-1,ljc)%ljc;
//		}
////	}
//	return init(res1,resr,resb);
	vector<int> now;now.clear();
	pair<ll,ll> tmp=ST::query(l,r);
	ll xxx=tmp.first;
	for (int id=tmp.second;id<=r&&h[id]==xxx;id=ST::query(id+1,r).second) 
		now.push_back(id);
	ll resx=2,resy=0;
	/*if (tmp.second==l) resx=2,resy=0;
	else */if (tmp.second!=l){
		node son=dp(l,tmp.second-1,xxx);
		resx=son.x*2%ljc;
		resy=son.y;
		resy=(resy*2%ljc+resx)%ljc;
	}
	for (int i=1;i<(int)now.size();i++){
		int L=now[i-1],R=now[i];
		if (R-L>1){
			node son=dp(L+1,R-1,xxx);
		resy=(resx*(son.x+son.y)%ljc+resy*(2*son.x%ljc+son.y%ljc)%ljc)%ljc;
		resx=resx*son.x%ljc;
		}
		resy=(resy*2%ljc+resx)%ljc;
	}
	if (r-now.back()>=1){
		node son=dp(now.back()+1,r,xxx);
		resy=(resx*(son.x+son.y)%ljc+resy*(2*son.x%ljc+son.y%ljc)%ljc)%ljc;
		resx=resx*son.x%ljc;
	} 
	return init(resx*fast_pow(2,xxx-H-1,ljc)%ljc,resy);
}
/*
4
2 3 5 4

3
1 3 2
*/
signed main(){
	n=read();
	for (int i=1;i<=n;i++) h[i]=read();
	ST::init();
	node tmp=dp(1,n,0);
	printf("%lld\n",(tmp.x+tmp.y)%ljc);
	return 0;
}