Gats and the Single Wobble
Gats practices jumping between platforms connected by branches. The platforms and their branches form a tree, so there is a unique route between every two platforms. Platform has height , and platforms joined directly by a branch always have different heights.
For two distinct platforms and , write their route as
An internal platform , where , is a wobbly landing if Gats continues in the same vertical direction after landing. More precisely, it is wobbly if either
or
Thus, a wobbly landing occurs when two consecutive jumps are both upward or both downward.
A route is called a one-wobble route if it contains exactly one wobbly landing. Routes are considered without direction, so the pair is the same as the pair .
Count the unordered pairs of distinct platforms whose route is a one-wobble route.
Input
The first line contains an integer (), the number of platforms.
The second line contains integers (), where is the height of platform .
Output
Print one integer: the number of unordered pairs of distinct platforms whose route is a one-wobble route.
Samples
Sample 1
Input
5 3 5 1 4 2 1 2 1 3 1 4 1 5
Output
4
The one-wobble routes are the four high-to-low leaf pairs , , , and .