def calc_cost(A, item, remain)
  s = item
  while remain.include?(s)
    s -= 1
  end
  e = item
  while remain.include?(e)
    e += 1
  end
  cost = 0
  (s..e).each do |i|
    cost += A[i]
  end
  return cost
end

N = gets.to_i
A = gets.split.map(&:to_i)
D = 10 ** 9 + 7

block_idx = (0..N-1).to_a
all_combi = block_idx.permutation(N).to_a
sum_cost = 0

all_combi.each do |combi|
  dist = combi.dup
  remain = []
  while combi.empty? != true
    item = combi.pop
    remain << item
    sum_cost += calc_cost(A, item, remain)
  end
end

puts sum_cost % D
