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: Time Limit Exceeded)

#include <bits/stdc++.h>
// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i)

#define pb push_back
#define mp make_pair
#define st first
#define nd second

const int MOD = 1000000007;

LL powe(LL a, LL b) {
  LL r = 1;
  while (b) {
    if (b&1) {
      (r *= a) %= MOD;
    }
    a = a * a % MOD;
    b >>= 1;
  }
  return r;
}

int H[105];
// ababababab..., any
pair<LL, LL> go(int a, int b, int offset) {
  // printf("%d %d %d\n", a, b, offset);

  if (a == b) return { 1, 1 };

  int mini = 1e9;
  FOR(i,a,b) mini = min(mini, H[i] - offset);
  // assert(mini > 0);

  if (a + 1 == b) return { powe(2, mini-1), powe(2, mini) };

  int S = a;

  LL aba = 1, any = 1, aba_counted = 1;
  int minis = 0;
  FOR(i,a,b+1) {
    if (i == b || H[i]-offset == mini) {
      if (i > S) {
        auto rec = go(S, i, offset+mini);

        (aba *= 2 * rec.st) %= MOD;
        (any *= (rec.nd + 2 * rec.st)) %= MOD;
      }

      S = i + 1;

      if (i < b) {
        (any *= 2) %= MOD;
        ++minis;
      }
    }
  }

  // printf("%d %d %d %lld %lld(%d %lld)\n", a, b, offset, aba, any, mini, powe(2, mini)-2);
  (any += 2 * aba * (powe(2, mini-1)-1)) %= MOD;
  (aba *= powe(2, mini-1)) %= MOD;
  // printf("%d %d %d %lld %lld\n", a, b, offset, aba, any);

  return {aba, any};
}

int main() {
  // ios_base::sync_with_stdio(0);

  int N;
  scanf("%d", &N);
  REP(i,N) {
    scanf("%d", &H[i]);
  }

  LL any = 0;
  REP(i,N) {
    int h = 0;
    if (i > 0) h = max(h, H[i-1]);
    if (i < N-1) h = max(h, H[i+1]);
    h = min(h, H[i]);
    any += H[i] - h;
    H[i] = h;
  }

  LL result = powe(2, any);
  auto r = go(0, N, 0);
  (result *= r.nd) %= MOD;

  /*
  REP(i,N) {
    if (H[i] == 1) {
      ++any;
      (result *= go(S,i)) %= MOD;
      S = i + 1;
    }
  }

  (result *= powe(2, any)) %= MOD;
  */

  printf("%lld\n", result);
}