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>.
from collections import deque
s='#'+input()+'#'
n=len(s)
dp=[0]*n
q=deque()
for i in range(n-1):
    if s[i]=='(' and s[i+1]==')':
        q.append((2,i+1))
        dp[i+1]=2
while q:
    le,ind=q.popleft()
    if s[ind-le]=='(' and s[ind+1]==')':
        q.appendleft((le+2,ind+1))
        dp[ind+1]=le+2
        dp[ind]=0
    else:
        if dp[ind-le]!=0:
            dp[ind]+=dp[ind-le]
            dp[ind-le]=0
            q.appendleft((dp[ind],ind))
print(max(dp),(dp.count(max(dp)),1)[max(dp)==0])
