Bracket Paths

Given a tree with '(' or ')' on each node, count ordered pairs (a,b) whose path string w_{a,b} is a properly matched bracket expression.

Hard9TreeDivide and conquerPrefix sumDFSNo attempts yetTime limit3sMemory limit1024 MB

Problem

An expression is a string consisting only of properly paired brackets. For example, "()()" and "(()())" are expressions, whereas ")(" and "()(" are not. Expressions can be defined inductively as follows:

  • "()" is an expression.
  • If aa is an expression, then "(aa)" is also an expression.
  • If aa and bb are expressions, then "abab" is also an expression.

A tree is a structure of nn nodes numbered 11 to nn and n1n - 1 edges, placed so that there is exactly one path between any two nodes. A single character is written in each node, and that character is either an open bracket "(" or a closed bracket ")". For different nodes aa and bb, wa,bw_{a,b} is the string obtained by walking the unique path from aa to bb and appending, one by one, the character written in each node you pass through. The string wa,bw_{a,b} also contains the character of node aa (at the first position) and the character of node bb (at the last position).

Find the number of ordered pairs (a,b)(a, b) of different nodes such that wa,bw_{a,b} is a correct expression. The string wb,aw_{b,a} is the reverse of wa,bw_{a,b}, so (a,b)(a, b) and (b,a)(b, a) are counted separately.

Input

The first line contains an integer nn (1n3000001 \le n \le 300\,000), the number of nodes in the tree.

The second line contains a string of nn characters, each of which is "(" or ")". The jj-th character is the character written in node jj.

Each of the next n1n - 1 lines contains two different integers xx and yy (1x,yn1 \le x, y \le n), the labels of two nodes joined directly by an edge.

Output

Print the number of such ordered pairs.