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 <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define pb(a) push_back(a)
using namespace std;

typedef long long ll;

const int N=1e4+5,Mo=1e9+7;

int pwr(int x,int y) {
	int z=1;
	for(;y;y>>=1,x=(ll)x*x%Mo)
		if (y&1) z=(ll)z*x%Mo;
	return z;
}

int n,h[N],row[N],column[N],tot;
vector<int> son[N];

int build(int l,int r,int d) {
	int mn=1e9,cnt=0,v=++tot;
	fo(i,l,r) mn=min(mn,h[i]);
	column[v]=r-l+1;row[v]=mn-d;
	fo(i,l,r) cnt+=h[i]==mn;
	if (cnt==r-l+1) return v;
	int la=l-1;
	fo(i,l,r) 
		if (h[i]==mn) {
			if (la!=i-1) son[v].pb(build(la+1,i-1,mn));
			la=i;
		}
	if (la!=r) son[v].pb(build(la+1,r,mn));
	return v;
}

ll f[N],g[N];

void dfs(int x) {
	f[x]=1;g[x]=0;int c=column[x]-1;
	for(int y:son[x]) {
		dfs(y);
		g[x]=(g[x]*(f[y]+g[y])+f[x]*g[y])%Mo;
		f[x]=f[x]*f[y]%Mo;
		c-=column[y]-1;
	}
	g[x]=(g[x]*pwr(2,c)+f[x]*(pwr(2,c)-1))%Mo;
	f[x]=f[x]*pwr(2,row[x]-(x==1))%Mo;
}

int main() {
	scanf("%d",&n);
	fo(i,1,n) scanf("%d",&h[i]);
	int ans=1;
	fo(i,2,n-1) 
		if (h[i]>h[i-1]&&h[i]>h[i+1]) {
			ans=(ll)ans*pwr(2,h[i]-max(h[i-1],h[i+1]))%Mo;
			h[i]=max(h[i-1],h[i+1]);
		}
	build(1,n,0);dfs(1);
	ans=(ll)ans*(f[1]+g[1])%Mo;
	printf("%d\n",ans*2%Mo);
	return 0;
}