This is yet another problem dealing with regular bracket sequences.

We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. 

You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 10<sup class="upper-index">6</sup>.
def solve(st):
    start = [0]*len(st)
    extension = [0]*len(st)
    frequency = [0]*len(st)
    stack =[]
    ans = 0
    for i in range(len(st)):
        char = st[i]
        if char == '(':
            stack.append(i)
        else:
            if len(stack) == 0:
                start[i] = extension[i] = -1
            else:
                idx = stack[-1]
                stack.pop()
                start[i] = extension[i] = idx
                if idx > 0 and st[idx - 1] ==')' and start[idx - 1] >= 0:
                    extension[i] = extension[idx - 1]
                length = i - extension[i] + 1
                frequency[length] += 1
                ans = max(ans, length)
    frequency[0] = 1

    print(ans, frequency[ans])

solve(input())