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: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define F first
#define S second
#define ALL(v) v.begin(),v.end()
#define fast_cin() ios_base::sync_with_stdio(0);
#define smid ((i + j) >> 1)
#define lc (sidx * 2)
#define rc (lc + 1)

typedef long long ll;
typedef long double ld;
typedef pair <ll,ll> pll;
typedef pair <int,int> pii;

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

bool bit(int n, int i) { return (n >> i) & 1; }
int ceil(int a, int b) { return ceil(((ld)a) / b); }

int faltu;

const int mod = 1e9+7;
const ll inf = 4e18;
const ll ninf = -inf;
const int imax = 2e9 + 100;

ll power(ll m) {
	ll res = 1, a = 2;
	while(m > 0) {
		if(m & 1) {
			res = (res * a) % mod;
		}
		m /= 2;
		a = (a * a) % mod;
	}
	return res;
}

ll solve(ll h[], int n) {
	if(n <= 0) {
		return 1;
	}
	ll ans = power(h[1]);
	ll tmp[n + 1], alt[n + 1];
	alt[0] = 1;
	if(h[2] < h[1]) {
		alt[1] = (2ll * power(h[1] - h[2])) % mod;
	} else {
		alt[1] = (2) % mod;
	}
	tmp[0] = 1;
	tmp[1] = ans;
	for(int i = 2; i <= n; i++) {
		if(h[i] > h[i - 1]) {
			ans = (((ans + alt[i - 1]) % mod) * (power(h[i] - h[i - 1]))) % mod;
		} else {
			ans = (ans + alt[i - 1]) % mod;
		}
		tmp[i] = ans;
		if(i < n and h[i + 1] < h[i]) {
			alt[i] = (((alt[i - 1] * 2ll) % mod) * power(h[i] - h[i + 1])) % mod;
		} else {
			alt[i] = (alt[i - 1] * 2) % mod;
		}
		trace(ans);
	}
	return ans;
}

int main()
{
	time_t t1, t2;
	t1 = clock();
	srand(time(0));
	
	ll n;
	cin >> n;

	ll h[n + 1];
	vector <int> breakDowns;
	for(int i = 1; i <= n; i++) {
		cin >> h[i];
		if(h[i] == 1) {
			breakDowns.pb(i);
		}
	}
	breakDowns.pb(n + 1);
	vector <ll> ans;
	int last = 0;
	for(int i: breakDowns) {
		trace(i - last - 1);
		ans.pb(solve(h + last, i - last - 1));
		last = i;
	}
	ll fAns = power(breakDowns.size() - 1);
	trace(fAns);
	for(ll i: ans) {
		trace(i);
		fAns = (fAns * i) % mod;
	}
	fAns = (fAns + mod) % mod;
	cout << fAns << endl;

	t2 = clock();
	cerr << "time taken: " << t2 - t1 << endl;
	return 0;
}