Bracket Substrings

Count how many distinct balanced bracket sequences appear as non-empty substrings of a given bracket string of length up to 500,000.

Hard9StringHash mapStackString matchingNo attempts yetTime limit2sMemory limit512 MB

Problem

A bracket string is defined as follows.

  1. The empty string is a bracket string.
  2. If SS is a bracket string, then (S)(S) is a bracket string.
  3. If SS and TT are bracket strings, then STST is a bracket string.
  4. Every bracket string is built by these three rules alone.

For example, ()() and ((()))(()) are bracket strings, while )((), (((((, and ()) are not.

Given a string SS, write a program that counts how many different bracket strings appear among the substrings of SS. A substring is a contiguous block cut out of SS, the empty string is not counted, and a string that occurs at several positions is counted once.

Input

The first line contains the length of the string, NN (1N500,0001 \le N \le 500{,}000).

The second line contains the string SS. Its length is NN and it consists only of the opening bracket ( and the closing bracket ).

Output

Print the number of different bracket strings among the substrings of SS.