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>

const int MAXN = 100 + 5;

const int Mod = 1e9 + 7;

int n, h[MAXN];

int sorted[MAXN];

int qpow(int a, int x) {
    int res = 1;
    for (; x > 0; x >>= 1) {
        if (x & 1) res = 1LL * res * a % Mod;
        a = 1LL * a * a % Mod;
    }
    return res;
}

std::set<std::pair<int, int>> intervals;

std::map<int, int> L_R, R_L;

int dp[MAXN][MAXN][2][2][2];

void Merge(int dp_l[2][2][2], int dp_r[2][2][2], int res[2][2][2]) {
    static int tmp[2][2][2];
    
    memset(tmp, 0, sizeof tmp);
    
    for (int l = 0; l < 2; ++l)
    for (int r = 0; r < 2; ++r) {
        for (int ll = 0; ll < 2; ++ll)
        for (int rr = 0; rr < 2; ++rr)
        for (int sl = 0; sl < 2; ++sl)
        for (int sr = 0; sr < 2; ++sr) {
            int t = sl | sr | (ll == rr);
            tmp[l][r][t] = (tmp[l][r][t] + 1LL * dp_l[l][ll][sl] * dp_r[rr][r][sr]) % Mod;
        }
    }
    
    memcpy(res, tmp, sizeof tmp);
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", h + i);
        sorted[i] = i;
    }
    
    std::sort(sorted + 1, sorted + n + 1, [] (int a, int b) { return h[a] > h[b]; });
    
    for (int i = 1; i <= n; ++i) {
        int j = sorted[i], k = sorted[i - 1], t = (h[k] - h[j]) & 1;
        
        if (h[k] > h[j]) {
            for (auto e: intervals) {
                int L = e.first, R = e.second;
                
                static int tmp[2][2][2];
                for (int l = 0; l < 2; ++l)
                for (int r = 0; r < 2; ++r) {
                    tmp[l][r][1] = dp[L][R][l ^ t][r ^ t][1];
                    tmp[l][r][0] = 1LL * (dp[L][R][l][r][0] + dp[L][R][l ^ 1][r ^ 1][0]) * qpow(2, h[k] - h[j] - 1) % Mod;
                }
                
                memcpy(dp[L][R], tmp, sizeof tmp);
            }
        }
        
        static int tmp[2][2][2];
        int L = j, R = j;
        
        memset(tmp, 0, sizeof tmp);
        tmp[0][0][0] = tmp[1][1][0] = 1;
        
        if (R_L.count(j - 1)) {
            int l = R_L[j - 1], r = j - 1;
            intervals.erase(intervals.find(std::make_pair(l, r)));
            L_R.erase(L_R.find(l));
            R_L.erase(R_L.find(r));
            Merge(dp[l][r], tmp, tmp);
            L = l;
        }
        
        if (L_R.count(j + 1)) {
            int l = j + 1, r = L_R[j + 1];
            intervals.erase(intervals.find(std::make_pair(l, r)));
            L_R.erase(L_R.find(l));
            R_L.erase(R_L.find(r));
            Merge(tmp, dp[l][r], tmp);
            R = r;
        }
        
        memcpy(dp[L][R], tmp, sizeof tmp);
        intervals.insert({L, R});
        L_R.insert({L, R});
        R_L.insert({R, L});
    }
    
    if (h[sorted[n]] > 1) {
        int t = (h[sorted[n]] - 1) & 1;
        int tmp[2][2][2];
        for (int l = 0; l < 2; ++l)
        for (int r = 0; r < 2; ++r) {
            tmp[l][r][1] = dp[1][n][l ^ t][r ^ t][1];
            tmp[l][r][0] = 1LL * (dp[1][n][l][r][0] + dp[1][n][l ^ 1][r ^ 1][0]) * qpow(2, h[sorted[n]] - 1 - 1) % Mod;
        }
        
        memcpy(dp[1][n], tmp, sizeof tmp);
    }
    
    int ans = 0;
    for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j)
    for (int k = 0; k < 2; ++k) {
        ans = (ans + dp[1][n][i][j][k]) % Mod;
    }
    printf("%d\n", ans);
    
    return 0;
}