class Blocks
  def initialize(weight, position = nil)
    @count = weight.length
    @position = position || Array.new(@count, TRUE)
    @weight = weight
  end

  def calculation
    total_weight = 0
    @count.times do |i|
      next unless @position[i]
      total_weight += current_weight(i)*((1..@position.true_count-1).inject(:*) || 1)
      new_position = @position.dup
      new_position[i] = FALSE
      total_weight += Blocks.new(@weight, new_position).calculation
      if @position == Array.new(@count, TRUE)
      end
    end
    return total_weight
  end

  def current_weight(i)
    @weight[sequence(i)].inject(:+)
  end

  def sequence(i)
    return nil unless @position[i]
    start = i
    finish = i
    (0..i-1).reverse_each do |j|
      break unless @position[j]
      start = j
    end
    (i+1..@count).each do |j|
      break unless @position[j]
      finish = j
    end
    Range.new(start, finish)
  end
end

class Array
  def true_count
    count{|i| i }
  end
end

def main(*args)
  n = args[0]
  arr = args[1].split(' ').map(&:to_i)
  Blocks.new(arr).calculation % (10**9 + 7)
end

if __FILE__ == $0
  n = gets.chomp!
  arr = gets.chomp!
  print main(n,arr)
end
