n = gets.to_i
p = gets.split.map &:to_i

idx = []


left = idx[-1]
right = idx[-1]
count = 0

if idx[-2] < left then
	left = idx[-2]
else
	right = idx[-2]
end


count += (idx.size - 1) * (right - left + 1) * (right - left) / 2

last_index = idx[-2]

(idx.size - 3).downto(0){ |i|
	if idx[i] < left then
		count += (right - last_index) * (right - idx[i]) * (i + 1)
		count += (last_index - left + 1) * (right - idx[i]) * (i + 2)
		left = idx[i]
	elsif idx[i] > right then
		count += (right - last_index + 1) * (idx[i] - left) * (i + 2)
		count += (last_index - left) * (idx[i] - left) * (i + 1)
		right = idx[i]
	end
	last_index = idx[i]
}

puts count


