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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.Scanner;
class Main{
  
  static int[] h;
  static long mod = 1000000007;
  
  static long pow(long a, long p, long mod){
    long res = 1;
    for(int i=0;i<32;++i){
      if(((p>>i)&1)==1)res=(res*a)%mod;
      a = (a*a)%mod;
    }
    return res;
  }
  
  static long culc(int l, int r){
    if(r-l <=1)return 1;
    int minIndex=l;
    for(int i=l+1;i<r;++i)if(h[i]<h[minIndex])minIndex=i;
    long dp1 = pow(2, h[minIndex], mod)-2;
    long dp2 = 2;
    for(int i=minIndex+1;i<r;++i){
      dp2 = (dp2*2)%mod;
      long dh = Math.max(0, h[i]-h[i-1]);
      dp1 = ((dp1 * pow(2, dh, mod))%mod + (dp2 * (pow(2, dh, mod)-1+mod)%mod)%mod)%mod;
    }
    for(int i=minIndex-1;i>=l;--i){
      dp2 = (dp2*2)%mod;
      long dh = Math.max(0, h[i]-h[i+1]);
      dp1 = ((dp1 * pow(2, dh, mod))%mod + (dp2 * (pow(2, dh, mod)-1+mod)%mod)%mod)%mod;
    }
    return (dp1+dp2)%mod;
  }
  
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    h = new int[n];
    for(int i=0;i<n;++i)h[i]=scan.nextInt();
    long totu = 0;
    if(n>1 && h[0]>h[1]){
      totu += h[0]-h[1];h[0]=h[1];
    }
    if(n>1 && h[n-1]>h[n-2]){
      totu += h[n-1]-h[n-2];h[n-2]=h[n-1];
    }
    for(int i=1;i<n-1;++i){
      if(h[i]>h[i-1] && h[i]>h[i+1]){
        totu += h[i] - Math.max(h[i-1], h[i+1]);
        h[i] = Math.max(h[i-1], h[i+1]);
      }
    }
    int l=0;
    long ans = 1;
    for(int i=0;i<n;++i){
      if(h[i]==1){
        totu+=1;
        ans = (ans * culc(l,i))%mod;
    	l=i+1;
      }
    }
    ans = (ans * pow(2, totu, mod))%mod;
    System.out.println(ans);
  }
}