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 sqr(x) ((x) * (x))
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define fin(name) freopen(name, "r", stdin)
#define fout(name) freopen(name, "w", stdout)
#define I(x, a) for(auto x : a)
#define F(i, l, r) for(auto i = l; i < r; ++i)
#define E(i, l, r) for(auto i = l; i <= r; ++i)
#define DF(i, l, r) for(auto i = l; i >= r; --i)
#define clean(a) memset((a),0,sizeof (a))
#define sync ios_base::sync_with_stdio(0);cin.tie(0)
#define all(x) (x).begin(),(x).end()
#define ret return
#define cont continue
#define brk break
#define ins insert
#define sz(a) ((int)(a).size())

typedef long long ll;
typedef unsigned long long ull;
typedef long double dbl;
typedef pair <int, int> pii;

const int inf = (int)1e9;
const ll linf = (ll)1e18;
const int mod = (int)1e9 + 7;
const dbl eps = (dbl)1e-8;
const int maxn = (int)1e5 + 5;
const dbl pi = acos(-1);

void add(ll a, ll& b) {
	b += a;
	if (b >= mod)
		b -= mod;
}

ll pw(ll x, ll y) {
	if (y == 0)
		return 1;
	if (y & 1)
		return pw(x, y - 1) * x % mod;
	x = pw(x, y / 2);
	return x * x % mod;
}

ll d[105][105];
int n, h[105], sh[105], pos[105];

int main() {
//	fin("t.in");
	sync;
	cin >> n;
	E(i, 1, n) {
		cin >> h[i];
//		h[i] = 100 * i;
		sh[i] = h[i];
	}
	sh[0] = h[0] = 1;
	sort(sh, sh + n + 1);
	E(i, 0, n) {
		pos[i] = 0;
		while (sh[pos[i]] < h[i])
			++pos[i];
	}

	clean(d);
	d[0][0] = 1;
	F(i, 0, n) {
		E(j, 0, n) {
			if (d[i][j] == 0)
				cont;
			if (h[i + 1] <= h[i]) {
				if (j < pos[i + 1])
					add(d[i][j], d[i + 1][j]);
				else
					add(2ll * d[i][j] % mod, d[i + 1][pos[i + 1]]);
			} else {
				if (j < pos[i]) {
					add(pw(2, h[i + 1] - h[i]) * d[i][j] % mod, d[i + 1][j]);
				} else {
					F(k, j, pos[i + 1]) {
						ll x = 2ll * (pw(2, h[i + 1] - sh[k]) - pw(2, h[i + 1] - sh[k + 1])) * d[i][j];
						x %= mod;
						x += mod;
						x %= mod;
						add(x, d[i + 1][k]);
					}
					add(2ll * d[i][j] % mod, d[i + 1][pos[i + 1]]);
				}
			}
		}

	}


	ll ans = 0;
	E(i, 0, n) {
//		cout << i << " " << d[2][i] << endl;
		add(d[n][i], ans);
	}
	cout << ans << endl;
	return 0;
}