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 Kotlin (1.0.0) (judge verdict: Accepted)

const val P = 1000000007L

class FiniteField(xRaw: Long) {
  val x = if (xRaw >= 0) xRaw % P else (P - ((-xRaw) % P)) % P
  constructor(xi: Int): this(xi.toLong())
  operator fun plus(o: Long) = FiniteField((x + o) % P)
  operator fun plus(o: FiniteField) = this + o.x
  operator fun minus(o: Long) = FiniteField((x + P - o) % P)
  operator fun minus(o: FiniteField) = this - o.x
  operator fun times(o: Long) = FiniteField((x * o) % P)
  operator fun times(o: FiniteField) = this * o.x
  override fun toString() = x.toString()
}

fun powerOfTwo(p: Int): FiniteField {
  if (p == 0) return FiniteField(1)
  val a = powerOfTwo(p / 2)
  return a * a * if (p % 2 == 1) FiniteField(2) else FiniteField(1)
}

fun solve(hs: List<Int>): Pair<FiniteField, FiniteField> {
  // println(hs)
  val hMin = hs.min()!!
  if (hs.all { it == hMin }) {
    val w = hs.size
    val v1 = powerOfTwo(w) + powerOfTwo(hMin) - 2
    val v2 = powerOfTwo(hMin)
    return Pair(v1, v2)
  } else {
    // Fill this.
    val r = hs.count { it == hMin }

    var p1 = FiniteField(1)
    var p2 = FiniteField(1)
    val childHs = mutableListOf<Int>()
    for (h in hs) {
      if (h == hMin) {
        if (childHs.size > 0) {
          val (c1, c2) = solve(childHs)
          p1 *= c1 + c2
          p2 *= c2
          childHs.clear()
        }
      } else {
        childHs.add(h - hMin)
      }
    }
    if (childHs.size > 0) {
      val (c1, c2) = solve(childHs)
      p1 *= c1 + c2
      p2 *= c2
      childHs.clear()
    }

    val v1 = p1 * powerOfTwo(r) + (powerOfTwo(hMin) - 2) * p2
    val v2 = powerOfTwo(hMin) * p2
    return Pair(v1, v2)
  }
}

fun main(args: Array<String>) {
  val n = readLine()!!.toInt()
  val hs = readLine()!!.split(" ").map { it.toInt() }
  println(solve(hs).first)
}