#include <bits/stdc++.h>
using namespace std;

#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vii vector<pair<ll,ll> >
#define pll pair<ll,ll>
#define f(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,a,b) for(ll i=a;i>=b;i--)
#define all(c) c.begin(),c.end()
#define sz(c) (ll)c.size()
#define lcm(a,b) (a*b)/(__gcd(a,b))
#define init(x,a) memset(x,a,sizeof(x))
#define lbd(a,val) lower_bound(all(a),val)
#define ubd(a,val) upper_bound(all(a),val)
#define pb push_back
#define mp1 make_pair
#define F first
#define S second
#define en '\n'
#define mod (ll)1000000007
#define M (ll)998244353
#define N 100005
#define PI 3.14159265358979323846

int main()
{
    IOS;
    ll t=1;
    //cin >> t;
    while(t--)
    {
        ll i,j,k=0,l=0,z=0;
        ll n,m=0;
        cin >> n;
        string s[n];
        vector<int> a(0,0);
        for(i=0;i<n;i++)
        {
            cin >> s[i];
            vector<int> x;
            k=0,l=0;
            for(j=0;j<s[i].length();)
            {
                if(s[i][j]=='(')
                {
                    k++;
                }
                else if(s[i][j]==')')
                {
                    l++;
                }
                if(s[i][j]=='(')
                {
                    x.push_back(j);
                    j++;
                }
                else if(!x.empty() && s[i][j]==')')
                {
                    z=x.back();
                    x.pop_back();
                    s[i].erase(s[i].begin()+j);
                    s[i].erase(s[i].begin()+z);
                    k--;
                    l--;
                    j=j-1;
                }
                else
                {
                    j++;
                }
            }
            if(!s[i].empty())
            {
                //cout<<s[i]<<endl;
                if(k!=0 && l!=0)
                {
                    i--;
                    n--;
                }
                else
                {
                    //cout<<s[i]<<endl;
                    a.push_back(k-l);
                    //cout <<(k-l)<<endl;
                }

            }
            else
            {
                m++;
                i--;
                n--;
            }
        }
        m=m+m*(m-1);
        sort(a.begin(),a.end());
        z=0;
        for(i=0;i<n;i++)
        {
            //cout <<a[i]<<endl;
            if(a[i]!=a[i-1])
            {
                z=0;
            for(j=n-1;j>=i && j>=0;j--)
            {
                if(a[i]+a[j]==0 && a[i] * a[j] !=0 )
                {
                    m++;
                    z++;
                }
                if(a[i]+a[j]<0)
                {
                    break;
                }
            }
            }
            else
            {
                m+=z;
            }
        }
        cout << m << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vii vector<pair<ll,ll> >
#define pll pair<ll,ll>
#define f(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,a,b) for(ll i=a;i>=b;i--)
#define all(c) c.begin(),c.end()
#define sz(c) (ll)c.size()
#define lcm(a,b) (a*b)/(__gcd(a,b))
#define init(x,a) memset(x,a,sizeof(x))
#define lbd(a,val) lower_bound(all(a),val)
#define ubd(a,val) upper_bound(all(a),val)
#define pb push_back
#define mp1 make_pair
#define F first
#define S second
#define en '\n'
#define mod (ll)1000000007
#define M (ll)998244353
#define N 100005
#define PI 3.14159265358979323846

int main()
{
    IOS;
    ll t=1;
    //cin >> t;
    while(t--)
    {
        ll i,j,k=0,l=0,z=0;
        ll n,m=0;
        cin >> n;
        string s[n];
        vector<int> a(0,0);
        for(i=0;i<n;i++)
        {
            cin >> s[i];
            vector<int> x;
            k=0,l=0;
            for(j=0;j<s[i].length();)
            {
                if(s[i][j]=='(')
                {
                    k++;
                }
                else if(s[i][j]==')')
                {
                    l++;
                }
                if(s[i][j]=='(')
                {
                    x.push_back(j);
                    j++;
                }
                else if(!x.empty() && s[i][j]==')')
                {
                    z=x.back();
                    x.pop_back();
                    s[i].erase(s[i].begin()+j);
                    s[i].erase(s[i].begin()+z);
                    k--;
                    l--;
                    j=j-1;
                }
                else
                {
                    j++;
                }
            }
            if(!s[i].empty())
            {
                //cout<<s[i]<<endl;
                if(k!=0 && l!=0)
                {
                    i--;
                    n--;
                }
                else
                {
                    //cout<<s[i]<<endl;
                    a.push_back(k-l);
                    //cout <<(k-l)<<endl;
                }

            }
            else
            {
                m++;
                i--;
                n--;
            }
        }
        m=m+m*(m-1);
        sort(a.begin(),a.end());
        for(i=0;i<n;i++)
        {
            //cout <<a[i]<<endl;
            for(j=n-1;j>=i && j>=0;j--)
            {
                if(a[i]+a[j]==0 && a[i] * a[j] !=0 )
                {
                    m++;
                }
                if(a[i]+a[j]<0)
                {
                    break;
                }
            }
        }
        cout << m << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a ){
	int cont = 0;
	if(a.size()%2) return false;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}
	return cont==0;
}

bool verifica( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}

	for( char c : b ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}
	return cont==0;
}

unordered_map<string,int> conteggi;
int main(){
//	freopen("input.txt","r",stdin);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int N; cin >> N;

	vector<string> iniziali;
	vector<string> finali;

	long long sol = 0;
	long long giuste = 0;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;

		if( check(str) ) giuste++;
		else if( str[0] == ')' && str.back() == '(' ) continue;
		else{
			if( str.front() == '(' ) iniziali.push_back( str );
			else finali.push_back( str );
		}
		conteggi[str] ++;
	}

	sol = giuste+(giuste-1)*giuste;
	for( int i=0; i<iniziali.size(); i++ ){
		auto& it = iniziali[i];
		for( auto& j : finali ){
			if( verifica(it,j) ){
				sol += conteggi[j];
				break;
			}
		}
		for( int j=i+1; j<iniziali.size(); j++ ){
			if( verifica(it,iniziali[j]) ){
				sol+=conteggi[iniziali[j]];
				break;
			}
		}
	}
	cout << sol;
}


#include <bits/stdc++.h>
using namespace std;

int check( const string& a ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
	}
	return cont;
}

int conteggi[310000];
int main(){
//	freopen("input.txt","r",stdin);
	//ios_base::sync_with_stdio(false);
	//cin.tie(0);
	int N; cin >> N;

	long long sol = 0;
	vector<int> numeri(N,0);
	for( int i=0; i<N; i++ ){
		string str; cin >> str;

		int num = check(str);
		if( num == 0 ) sol++;

		sol += conteggi[-num];
		conteggi[num]++;
	}
	cout << sol;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont < 0 ) return false;
	}
	for( char c : b ){
		if( c == '(' ) cont++;
		else if( c == ')' ) cont --;
		if( cont < 0 ) return false;
	}
	if( cont != 0 ) return false;
	return true;
}

vector<string> corrette;

char esiti[310000];
bool controlla( int i ){
	if( esiti[i] ) return esiti[i] == 1;

	if( check(corrette[i],"") ){
		esiti[i] = 1;
	}else{
		esiti[i] = -1;
	}
	return esiti[i] == 1;
}

int main(){
	//freopen("input.txt","r",stdin);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int N; cin >> N;

	long long giuste = 0;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;
		if( check(str,"")){
			giuste++;
			continue;
		}
		corrette.push_back(str);
	}
	long long pairs=giuste+(giuste-1)*giuste;

	for( int i=0; i<corrette.size(); i++ ){
		for( int j=i+1;j<corrette.size();j++ ){
			if( corrette[i][0] == '(' )
			if( check(corrette[i],corrette[j])) pairs++;

			if( corrette[j][0] == '(' )
			if( check(corrette[j],corrette[i])) pairs++;
		}
	}

	cout << pairs;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont < 0 ) return false;
	}
	for( char c : b ){
		if( c == '(' ) cont++;
		else if( c == ')' ) cont --;
		if( cont < 0 ) return false;
	}
	if( cont != 0 ) return false;
	return true;
}

int main(){
	int N; cin >> N;
	vector<string> brackets(N,"");
	for( int i=0; i<N; i++ ){
		cin >> brackets[i];
	}

	int pairs = 0;
	for( int i=0; i<N; i++ ){
		for( int j=i+1; j<N; j++ ){
			if( check(brackets[i],brackets[j]) ) pairs++;
			if( check(brackets[j],brackets[i]) ) pairs++;
		}
		if( check(brackets[i],brackets[i]) ) pairs++;
	}
	cout << pairs;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont < 0 ) return false;
	}
	for( char c : b ){
		if( c == '(' ) cont++;
		else if( c == ')' ) cont --;
		if( cont < 0 ) return false;
	}
	if( cont != 0 ) return false;
	return true;
}

vector<string> corrette;

int main(){
	//freopen("input.txt","r",stdin);
	//ios_base::sync_with_stdio(false);
	//cin.tie(0);
	int N; cin >> N;

	long long giuste = 0;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;
		if( check(str,"")){
			giuste++;
			continue;
		}
		corrette.push_back(str);
	}
	long long pairs=giuste+(giuste-1)*giuste;

	for( int i=0; i<corrette.size(); i++ ){
		for( int j=i+1;j<corrette.size();j++ ){
			if( corrette[i][0] == '(' && corrette[j].back() == ')' )
			if( check(corrette[i],corrette[j])) pairs++;

			if( corrette[j][0] == '(' && corrette[i].back() == ')' )
			if( check(corrette[j],corrette[i])) pairs++;
		}
	}

	cout << pairs;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a ){
	int cont = 0;
	if(a.size()%2) return false;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}
	return cont==0;
}

bool verifica( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}

	for( char c : b ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}
	return cont==0;
}

unordered_map<string,int> conteggi;
int main(){
	freopen("input.txt","r",stdin);
	//ios_base::sync_with_stdio(false);
	//cin.tie(0);
	int N; cin >> N;

	vector<string> iniziali;
	vector<string> finali;

	long long sol = 0;
	long long giuste = 0;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;

		if( check(str) ) giuste++;
		else if( str[0] == ')' && str.back() == '(' ) continue;
		else{
			if( str.front() == '(' ) iniziali.push_back( str );
			else finali.push_back( str );
		}
		conteggi[str] ++;
	}

	sol = giuste+(giuste-1)*giuste;
	for( int i=0; i<iniziali.size(); i++ ){
		auto& it = iniziali[i];
		for( auto& j : finali ){
			if( verifica(it,j) ){
				sol += conteggi[j];
				break;
			}
		}
		for( int j=i+1; j<iniziali.size(); j++ ){
			if( verifica(it,iniziali[j]) ) sol+=1;
			if( verifica(iniziali[j],it) ) sol+=1;
		}
	}
	cout << sol;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a ){
	int cont = 0;
	if(a.size()%2) return false;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}
	return cont==0;
}

bool verifica( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}

	for( char c : b ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}
	return cont==0;
}

int main(){
//	freopen("input.txt","r",stdin);
	//ios_base::sync_with_stdio(false);
	//cin.tie(0);
	int N; cin >> N;

	vector<string> iniziali;
	vector<string> finali;

	long long sol = 0;
	long long giuste = 0;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;

		if( check(str) ) giuste++;
		else if( str[0] == ')' && str.back() == '(' ) continue;
		else{
			if( str.front() == '(' ) iniziali.push_back( str );
			else finali.push_back( str );
		}
	}

	sol = giuste+(giuste-1)*giuste;
	for( int i=0; i<iniziali.size(); i++ ){
		auto& it = iniziali[i];
		for( auto& j : finali ){
			if( verifica(it,j) ){
				sol += 1;
			}
		}
		for( int j=i+1; j<iniziali.size(); j++ ){
			if( verifica(it,iniziali[j]) ) sol+=1;
			if( verifica(iniziali[j],it) ) sol+=1;
		}
	}
	cout << sol;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont < 0 ) return false;
	}
	for( char c : b ){
		if( c == '(' ) cont++;
		else if( c == ')' ) cont --;
		if( cont < 0 ) return false;
	}
	if( cont != 0 ) return false;
	return true;
}

vector<string> corrette;

char esiti[310000];
bool controlla( int i ){
	if( esiti[i] ) return esiti[i] == 1;

	if( check(corrette[i],"") ){
		esiti[i] = 1;
	}else{
		esiti[i] = -1;
	}
	return esiti[i] == 1;
}

int main(){
//	freopen("input.txt","r",stdin);
	int N; cin >> N;

	long long giuste = 0;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;
		if( check(str,"")){
			giuste++;
			continue;
		}
		corrette.push_back(str);
	}
	long long pairs=giuste+(giuste-1)*giuste;

	for( int i=0; i<corrette.size(); i++ ){
		for( int j=i+1;j<corrette.size();j++ ){
			if( corrette[i][0] == '(' )
			if( check(corrette[i],corrette[j])) pairs++;

			if( corrette[j][0] == '(' )
			if( check(corrette[j],corrette[i])) pairs++;
		}
	}

	cout << pairs;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont < 0 ) return false;
	}
	for( char c : b ){
		if( c == '(' ) cont++;
		else if( c == ')' ) cont --;
		if( cont < 0 ) return false;
	}
	if( cont != 0 ) return false;
	return true;
}

vector<string> corrette;

char esiti[310000];
bool controlla( int i ){
	if( esiti[i] ) return esiti[i] == 1;

	if( check(corrette[i],"") ){
		esiti[i] = 1;
	}else{
		esiti[i] = -1;
	}
	return esiti[i] == 1;
}

int main(){
//	freopen("input.txt","r",stdin);
	int N; cin >> N;

	int giuste = 0;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;
		if( check(str,"")){
			giuste++;
			continue;
		}
		corrette.push_back(str);
	}
	long long pairs=giuste+(giuste-1)*giuste;

	for( int i=0; i<corrette.size(); i++ ){
		if( controlla(i) ) pairs++;

		if( controlla(i) ){
			for( int j=i+1; j<corrette.size(); j++ ){
				if( controlla(j) ) pairs+=2;
			}
		}else{
			for( int j=i+1;j<corrette.size();j++ ){
				if( check(corrette[i],corrette[j])) pairs++;
				if( check(corrette[j],corrette[i])) pairs++;
			}
		}
	}

	cout << pairs;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont < 0 ) return false;
	}
	for( char c : b ){
		if( c == '(' ) cont++;
		else if( c == ')' ) cont --;
		if( cont < 0 ) return false;
	}
	if( cont != 0 ) return false;
	return true;
}

vector<string> corrette;

char esiti[310000];
bool controlla( int i ){
	if( esiti[i] ) return esiti[i] == 1;

	if( check(corrette[i],"") ){
		esiti[i] = 1;
	}else{
		esiti[i] = -1;
	}
	return esiti[i] == 1;
}

int main(){
	//freopen("input.txt","r",stdin);
	int N; cin >> N;

	long long giuste = 0;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;
		if( check(str,"")){
			giuste++;
			continue;
		}
		corrette.push_back(str);
	}
	long long pairs=giuste+(giuste-1)*giuste;

	for( int i=0; i<corrette.size(); i++ ){
		for( int j=i+1;j<corrette.size();j++ ){
			if( check(corrette[i],corrette[j])) pairs++;
			if( check(corrette[j],corrette[i])) pairs++;
		}
	}

	cout << pairs;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 300500

int aperte[MAXN];
int chiuse[MAXN];

int main(){
//	freopen("input.txt","r",stdin);
	int N; cin >> N;

	while( N-- ){
		string str; cin >> str;
		int open = 0;
		int close = 0;
		if( str.back() == '(' && str.front() == ')' ) continue;

		for( char c : str ){
			if( c == '(' ){
				open++;
			}else{
				if( open == 0 ){
					close++;
				}else{
					open--;
				}
			}
		}
		if( open==0 && close==0 ){
			aperte[0]++;
			chiuse[0]++;
		}else if( open==0 ){
			chiuse[close]++;
		}else{
			aperte[open]++;
		}
	}

	long long sol=0;
	for( int i=0; i<300010; i++ ){
		sol += aperte[i]*chiuse[i];
	}
	cout << sol;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont < 0 ) return false;
	}
	for( char c : b ){
		if( c == '(' ) cont++;
		else if( c == ')' ) cont --;
		if( cont < 0 ) return false;
	}
	if( cont != 0 ) return false;
	return true;
}

void fast_read( string& str ){
	char c = 0;
	while( c != '(' && c != ')' ) c = getchar_unlocked();
	while( c == '(' || c == ')' ){
		str += c;
		c = getchar_unlocked();
	}
}

int main(){
	int N; cin >> N;
	vector<string> brackets(N,"");
	for( int i=0; i<N; i++ ){
		fast_read( brackets[i] );
	}

	int pairs = 0;
	for( int i=0; i<N; i++ ){
		for( int j=i+1; j<N; j++ ){
			if( check(brackets[i],brackets[j]) ) pairs++;
			if( check(brackets[j],brackets[i]) ) pairs++;
		}
		if( check(brackets[i],brackets[i]) ) pairs++;
	}
	cout << pairs;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont < 0 ) return false;
	}
	for( char c : b ){
		if( c == '(' ) cont++;
		else if( c == ')' ) cont --;
		if( cont < 0 ) return false;
	}
	if( cont != 0 ) return false;
	return true;
}

void fast_read( string& str ){
	char c = 0;
	while( c != '(' && c != ')' ) c = _getchar_nolock();
	while( c == '(' || c == ')' ){
		str += c;
		c = _getchar_nolock();
	}
}

int main(){
//	freopen("input.txt","r",stdin);
	int N; cin >> N;
	vector<string> brackets(N,"");

	vector<string> corrette;
	vector<string> inizio_apro;
	vector<string> inizio_chiudo;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;
		if( check(str,"") == true ) corrette.push_back( str );
		else if( str.front()==')' && str.back()=='(' ) continue;
		else if( str.front() == '(' && str.back() == ')' ) continue;
		else if( str.front() == '(' ) inizio_apro.push_back(str);
		else inizio_chiudo.push_back(str);
	}

	long long pairs = corrette.size() + (corrette.size()-1)*corrette.size();
	for( int i=0;i<inizio_apro.size(); i++ ){
		for( int j=i+1; j<inizio_apro.size(); j++ ){
			if( check(inizio_apro[i],inizio_apro[j]) ) pairs++;
			if( check(inizio_apro[j],inizio_apro[i]) ) pairs++;
		}
		for( int j=0; j<inizio_chiudo.size(); j++ ){
			if( check(inizio_apro[i],inizio_chiudo[j]) ) pairs++;
		}
	}
	cout << pairs;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 300500

long long aperte[MAXN];
long long chiuse[MAXN];

int main(){
//	freopen("input.txt","r",stdin);
	int N; cin >> N;

	while( N-- ){
		string str; cin >> str;
		int open = 0;
		int close = 0;
		if( str.back() == '(' && str.front() == ')' ) continue;

		for( char c : str ){
			if( c == '(' ){
				open++;
			}else{
				if( open == 0 ){
					close++;
				}else{
					open--;
				}
			}
		}
		if( open==0 && close==0 ){
			aperte[0]++;
			chiuse[0]++;
		}else if( open==0 ){
			chiuse[close]++;
		}else{
			aperte[open]++;
		}
	}

	unsigned long long sol=0;
	for( int i=0; i<300010; i++ ){
		sol += aperte[i]*chiuse[i];
	}
	cout << sol;
}


#include <bits/stdc++.h>
using namespace std;

int check( const string& a ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
	}
	return cont;
}

int conteggi[310000];
int main(){
//	freopen("input.txt","r",stdin);
	//ios_base::sync_with_stdio(false);
	//cin.tie(0);
	int N; cin >> N;

	long long sol = 0;
	vector<int> numeri(N,0);
	for( int i=0; i<N; i++ ){
		string str; cin >> str;
		if( str.front() == ')' && str.back() == '(' ) continue;

		int num = check(str);
		if( num == 0 ) sol++;

		if( str.front() == '(' )
		conteggi[num]++;
	}
	cout << sol;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a ){
	int cont = 0;
	if(a.size()%2) return false;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}
	return cont==0;
}

bool verifica( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}

	for( char c : b ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}
	return cont==0;
}

unordered_map<string,int> conteggi;
int main(){
	freopen("input.txt","r",stdin);
	//ios_base::sync_with_stdio(false);
	//cin.tie(0);
	int N; cin >> N;

	vector<string> iniziali;
	vector<string> finali;

	long long sol = 0;
	long long giuste = 0;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;

		if( check(str) ) giuste++;
		else if( str[0] == ')' && str.back() == '(' ) continue;
		else{
			if( str.front() == '(' ) iniziali.push_back( str );
			else finali.push_back( str );
		}
		conteggi[str] ++;
	}

	sol = giuste+(giuste-1)*giuste;
	for( int i=0; i<iniziali.size(); i++ ){
		auto& it = iniziali[i];
		for( auto& j : finali ){
			if( verifica(it,j) ){
				sol += conteggi[j];
				break;
			}
		}
		for( int j=i+1; j<iniziali.size(); j++ ){
			if( verifica(it,iniziali[j]) ){
				sol+=conteggi[iniziali[j]];
				break;
			}
		}
	}
	cout << sol;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a ){
	int cont = 0;
	if(a.size()%2) return false;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}
	return cont==0;
}

bool verifica( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}

	for( char c : b ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}
	return cont==0;
}

unordered_map<string,int> conteggi;
int main(){
//	freopen("input.txt","r",stdin);
	//ios_base::sync_with_stdio(false);
	//cin.tie(0);
	int N; cin >> N;

	vector<string> iniziali;
	vector<string> finali;

	long long sol = 0;
	long long giuste = 0;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;

		if( check(str) ) giuste++;
		else if( str[0] == ')' && str.back() == '(' ) continue;
		else{
			if( str.front() == '(' ) iniziali.push_back( str );
			else finali.push_back( str );
		}
		conteggi[str] ++;
	}

	sol = giuste+(giuste-1)*giuste;
	for( int i=0; i<iniziali.size(); i++ ){
		auto& it = iniziali[i];
		for( auto& j : finali ){
			if( verifica(it,j) ){
				sol += conteggi[j];
				break;
			}
		}
	}
	cout << sol;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a ){
	int cont = 0;
	if(a.size()%2) return false;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
	}
	return cont==0;
}

bool verifica( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}

	for( char c : b ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}
	return cont==0;
}

int conta( const string& a ){
	int cont = 0;
	for( char c : a ){
		if( c == '(') cont++;
		else cont--;
	}
	return cont;
}

unordered_map<string,int> conteggi;

int main(){
	//freopen("input.txt","r",stdin);
	//ios_base::sync_with_stdio(false);
	//cin.tie(0);
	int N; cin >> N;

	vector<string> iniziali;
	vector<string> finali;

	long long sol = 0;
	long long giuste = 0;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;

		if( check(str) ) giuste++;
		else if( str[0] == ')' && str.back() == '(' ) continue;
		else{
			int cont = conta(str);
			if( cont > 0 ) iniziali.push_back( str );
			else finali.push_back( str );
			conteggi[str] ++;
		}
	}

	sol = giuste+(giuste-1)*giuste;
	for( auto& it : iniziali ){
		for( auto& j : finali ){
			if( verifica(it,j) ){
				sol += 1;
			}
			if( verifica(j,it) ){
				sol += 1;
			}
		}
	}
	cout << sol;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont < 0 ) return false;
	}
	for( char c : b ){
		if( c == '(' ) cont++;
		else if( c == ')' ) cont --;
		if( cont < 0 ) return false;
	}
	if( cont != 0 ) return false;
	return true;
}

void fast_read( string& str ){
	char c = 0;
	while( c != '(' && c != ')' ) c = _getchar_nolock();
	while( c == '(' || c == ')' ){
		str += c;
		c = _getchar_nolock();
	}
}

int main(){
//	freopen("input.txt","r",stdin);
	int N; cin >> N;
	vector<string> corrette;
	vector<string> inizio_apro;
	vector<string> inizio_chiudo;

	for( int i=0; i<N; i++ ){
		string str; cin >> str;
		if( check(str,"") == true ) corrette.push_back( str );
		else if( str.front() == '(' ) inizio_apro.push_back(str);
		else inizio_chiudo.push_back(str);
	}

	long long pairs = 0;
	for( int i=0; i<corrette.size(); i++ ){
		pairs++;
		for( int j=i+1;j<corrette.size();j++ ){
			pairs += 2;
		}
	}

	for( int i=0;i<inizio_apro.size(); i++ ){
		for( int j=0; j<inizio_chiudo.size(); j++ ){
			if( check(inizio_apro[i],inizio_chiudo[j]) ) pairs++;
		}
	}
	cout << pairs;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 300500

long long aperte[MAXN];
long long chiuse[MAXN];

int main(){
	//freopen("input.txt","r",stdin);
	int N; cin >> N;

	while( N-- ){
		string str; cin >> str;
		long long open = 0;
		long long close = 0;
		if( str.back() == '(' && str.front() == ')' ) continue;

		for( char c : str ){
			if( c == '(' ){
				open++;
			}else{
				if( open == 0 ){
					close++;
				}else{
					open--;
				}
			}
		}
		if( open==0 && close==0 ){
			aperte[0]++;
			chiuse[0]++;
		}else if( open==0 ){
			chiuse[close]++;
		}else if( chiuse==0 ){
			aperte[open]++;
		}
	}

	unsigned long long sol=0;
	for( int i=0; i<300400; i++ ){
		sol += aperte[i]*chiuse[i];
	}
	cout << sol;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a ){
	int cont = 0;
	if(a.size()%2) return false;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
	}
	return cont==0;
}

bool verifica( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
	}

	for( char c : b ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
	}
	return cont==0;
}

int conta( const string& a ){
	int cont = 0;
	for( char c : a ){
		if( c == '(') cont++;
		else cont--;
	}
	return cont;
}

unordered_map<string,int> conteggi;

int main(){
//	freopen("input.txt","r",stdin);
	//ios_base::sync_with_stdio(false);
	//cin.tie(0);
	int N; cin >> N;

	vector<string> iniziali;
	vector<string> finali;

	long long sol = 0;
	long long giuste = 0;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;

		if( check(str) ) giuste++;
		else if( str[0] == ')' && str.back() == '(' ) continue;
		else{
			int cont = conta(str);
			if( cont > 0 ) iniziali.push_back( str );
			else finali.push_back( str );
			conteggi[str] ++;
		}
	}

	sol = giuste+(giuste-1)*giuste;
	for( auto& it : iniziali ){
		for( auto& j : finali ){
			if( verifica(it,j) ){
				sol += conteggi[j];
			}
		}
	}
	cout << sol;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont < 0 ) return false;
	}
	for( char c : b ){
		if( c == '(' ) cont++;
		else if( c == ')' ) cont --;
		if( cont < 0 ) return false;
	}
	if( cont != 0 ) return false;
	return true;
}

vector<string> corrette;

char esiti[450000];
bool controlla( int i ){
	if( esiti[i] ) return esiti[i] == 1;

	if( check(corrette[i],"") ){
		esiti[i] = 1;
	}else{
		esiti[i] = -1;
	}
	return esiti[i] == 1;
}

int main(){
//	freopen("input.txt","r",stdin);
	int N; cin >> N;

	for( int i=0; i<N; i++ ){
		string str; cin >> str;
		corrette.push_back(str);
	}

	long long pairs=0;
	for( int i=0; i<N; i++ ){
		if( controlla(i) ) pairs++;

		if( controlla(i) ){
			for( int j=i+1; j<N; j++ ){
				if( controlla(j) ) pairs+=2;
			}
		}else{
			for( int j=i+1;j<N;j++ ){
				if( check(corrette[i],corrette[j])) pairs++;
				if( check(corrette[j],corrette[i])) pairs++;
			}
		}
	}

	cout << pairs;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a ){
	int cont = 0;
	if(a.size()%2) return false;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}
	return cont==0;
}

bool verifica( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}

	for( char c : b ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}
	return cont==0;
}

unordered_map<string,int> conteggi;
int main(){
//	freopen("input.txt","r",stdin);
	//ios_base::sync_with_stdio(false);
	//cin.tie(0);
	int N; cin >> N;

	vector<string> iniziali;
	vector<string> finali;

	long long sol = 0;
	long long giuste = 0;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;

		if( check(str) ) giuste++;
		else if( str[0] == ')' && str.back() == '(' ) continue;
		else{
			if( str.front() == '(' ) iniziali.push_back( str );
			else finali.push_back( str );
		}
		conteggi[str] ++;
	}

	sol = giuste+(giuste-1)*giuste;
	for( int i=0; i<iniziali.size(); i++ ){
		auto& it = iniziali[i];
		for( auto& j : finali ){
			if( verifica(it,j) ){
				sol += conteggi[j];
				break;
			}
		}
		for( int j=i+1; j<iniziali.size(); j++ ){
			if( verifica(it,iniziali[j]) ){
				sol+=conteggi[iniziali[j]];
				break;
			}
		}
		for( int j=i+1; j<iniziali.size(); j++ ){
			if( verifica(iniziali[j],it) ){
				sol+=conteggi[it];
				break;
			}
		}
	}
	cout << sol;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont < 0 ) return false;
	}
	for( char c : b ){
		if( c == '(' ) cont++;
		else if( c == ')' ) cont --;
		if( cont < 0 ) return false;
	}
	if( cont != 0 ) return false;
	return true;
}

void fast_read( string& str ){
	char c = 0;
	while( c != '(' && c != ')' ) c = _getchar_nolock();
	while( c == '(' || c == ')' ){
		str += c;
		c = _getchar_nolock();
	}
}

int main(){
	//freopen("input.txt","r",stdin);
	int N; cin >> N;
	vector<string> corrette;
	vector<string> inizio_apro;
	vector<string> inizio_chiudo;

	for( int i=0; i<N; i++ ){
		string str; cin >> str;
		if( check(str,"") == true ) corrette.push_back( str );
		else if( str.front()==')' && str.back()=='(' ) continue;
		else if( str.front() == '(' && str.back() == ')' ) continue;
		else if( str.front() == '(' ) inizio_apro.push_back(str);
		else inizio_chiudo.push_back(str);
	}

	long long pairs = 0;
	for( int i=0; i<corrette.size(); i++ ){
		pairs++;
		for( int j=i+1;j<corrette.size();j++ ){
			pairs += 2;
		}
	}

	for( int i=0;i<inizio_apro.size(); i++ ){
		for( int j=0; j<inizio_chiudo.size(); j++ ){
			if( check(inizio_apro[i],inizio_chiudo[j]) ) pairs++;
		}
	}
	cout << pairs;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont < 0 ) return false;
	}
	for( char c : b ){
		if( c == '(' ) cont++;
		else if( c == ')' ) cont --;
		if( cont < 0 ) return false;
	}
	if( cont != 0 ) return false;
	return true;
}

vector<string> corrette;

int main(){
	//freopen("input.txt","r",stdin);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int N; cin >> N;

	long long giuste = 0;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;
		if( check(str,"")){
			giuste++;
			continue;
		}
		corrette.push_back(str);
	}
	long long pairs=giuste+(giuste-1)*giuste;

	for( int i=0; i<corrette.size(); i++ ){
		for( int j=i+1;j<corrette.size();j++ ){
			if( corrette[i][0] == '(' && corrette[j].back() == ')' )
			if( check(corrette[i],corrette[j])) pairs++;

			if( corrette[j][0] == '(' && corrette[i].back() == ')' )
			if( check(corrette[j],corrette[i])) pairs++;
		}
	}

	cout << pairs;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont < 0 ) return false;
	}
	for( char c : b ){
		if( c == '(' ) cont++;
		else if( c == ')' ) cont --;
		if( cont < 0 ) return false;
	}
	if( cont != 0 ) return false;
	return true;
}

void fast_read( string& str ){
	char c = 0;
	while( c != '(' && c != ')' ) c = _getchar_nolock();
	while( c == '(' || c == ')' ){
		str += c;
		c = _getchar_nolock();
	}
}

int main(){
//	freopen("input.txt","r",stdin);
	int N; cin >> N;
	vector<string> brackets(N,"");

	vector<string> corrette;
	vector<string> inizio_apro;
	vector<string> inizio_chiudo;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;
		if( check(str,"") == true ) corrette.push_back( str );
		else if( str.front()==')' && str.back()=='(' ) continue;
		else if( str.front() == '(' && str.back() == ')' ) continue;
		else if( str.front() == '(' ) inizio_apro.push_back(str);
		else inizio_chiudo.push_back(str);
	}

	long long pairs = corrette.size() + (corrette.size()-1)*corrette.size();
	for( int i=0;i<inizio_apro.size(); i++ ){
		for( int j=i+1; j<inizio_apro.size(); j++ ){
			if( check(inizio_apro[i],inizio_apro[j]) ) pairs++;
			if( check(inizio_apro[j],inizio_apro[i]) ) pairs++;
		}
		for( int j=0; j<inizio_chiudo.size(); j++ ){
			if( check(inizio_apro[i],inizio_chiudo[j]) ) pairs++;
		}
		if( check(inizio_apro[i],inizio_apro[i])) pairs++;
	}
	cout << pairs;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 300500

long long aperte[MAXN];
long long chiuse[MAXN];

int main(){
	//freopen("input.txt","r",stdin);
	int N; cin >> N;

	while( N-- ){
		string str; cin >> str;
		long long open = 0;
		long long close = 0;
		if( str.back() == '(' && str.front() == ')' ) continue;

		for( char c : str ){
			if( c == '(' ){
				open++;
			}else{
				if( open == 0 ){
					close++;
				}else{
					open--;
				}
			}
		}
		if( open==0 && close==0 ){
			aperte[0]++;
			chiuse[0]++;
		}else if( open==0 ){
			chiuse[close]++;
		}else{
			aperte[open]++;
		}
	}

	unsigned long long sol=0;
	for( int i=0; i<300400; i++ ){
		sol += aperte[i]*chiuse[i];
	}
	cout << sol;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont < 0 ) return false;
	}
	for( char c : b ){
		if( c == '(' ) cont++;
		else if( c == ')' ) cont --;
		if( cont < 0 ) return false;
	}
	if( cont != 0 ) return false;
	return true;
}

void fast_read( string& str ){
	char c = 0;
	while( c != '(' && c != ')' ) c = _getchar_nolock();
	while( c == '(' || c == ')' ){
		str += c;
		c = _getchar_nolock();
	}
}

int main(){
//	freopen("input.txt","r",stdin);
	int N; cin >> N;
	vector<string> brackets(N,"");

	vector<string> corrette;
	vector<string> inizio_apro;
	vector<string> inizio_chiudo;
	for( int i=0; i<N; i++ ){
		string str; fast_read(str);
		if( check(str,"") == true ) corrette.push_back( str );
		else if( str.front()==')' && str.back()=='(' ) continue;
		else if( str.front() == '(' ) inizio_apro.push_back(str);
		else inizio_chiudo.push_back(str);
	}

	long long pairs = corrette.size() + (corrette.size()-1)*corrette.size();
	for( auto& it : inizio_apro ){
		for( auto& ij : inizio_chiudo ){
			if( check(it,ij) ) pairs++;
		}
	}
	cout << pairs;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont < 0 ) return false;
	}
	for( char c : b ){
		if( c == '(' ) cont++;
		else if( c == ')' ) cont --;
		if( cont < 0 ) return false;
	}
	if( cont != 0 ) return false;
	return true;
}

void fast_read( string& str ){
	char c = 0;
	while( c != '(' && c != ')' ) c = _getchar_nolock();
	while( c == '(' || c == ')' ){
		str += c;
		c = _getchar_nolock();
	}
}

int main(){
	//freopen("input.txt","r",stdin);
	int N; cin >> N;
	vector<string> brackets(N,"");

	vector<string> corrette;
	vector<string> inizio_apro;
	vector<string> inizio_chiudo;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;
		if( check(str,"") == true ) corrette.push_back( str );
		else if( str.front()==')' && str.back()=='(' ) continue;
		else if( str.front() == '(' ) inizio_apro.push_back(str);
		else inizio_chiudo.push_back(str);
	}

	long long pairs = corrette.size() ? pow(2,corrette.size()-1) : 0;
	for( auto& it : inizio_apro ){
		for( auto& ij : inizio_chiudo ){
			if( check(it,ij) ) pairs++;
		}
	}
	cout << pairs;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a ){
	int cont = 0;
	if(a.size()%2) return false;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}
	return cont==0;
}

bool verifica( const string& a, const string& b ){
	int cont = 0;
	if( (a.size()+b.size())%2 ) return false;

	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}

	for( char c : b ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}
	return cont==0;
}

unordered_map<string,int> conteggi;
int main(){
	//freopen("input.txt","r",stdin);
	//ios_base::sync_with_stdio(false);
	//cin.tie(0);
	int N; cin >> N;

	vector<string> iniziali;
	vector<string> finali;
	conteggi.reserve( 310000 );

	long long sol = 0;
	long long giuste = 0;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;

		if( check(str) ) giuste++;
		else if( str[0] == ')' && str.back() == '(' ) continue;
		else{
			if( str.front() == '(' ) iniziali.push_back( str );
			else finali.push_back( str );
		}
		conteggi[str] ++;
	}

	sol = giuste+(giuste-1)*giuste;
	for( int i=0; i<iniziali.size(); i++ ){
		auto& it = iniziali[i];
		for( auto& j : finali ){
			if( verifica(it,j) ){
				sol += conteggi[j];
				break;
			}
		}
		for( int j=i+1; j<iniziali.size(); j++ ){
			if( verifica(it,iniziali[j]) ){
				sol+=conteggi[iniziali[j]];
				break;
			}
		}
	}
	cout << sol;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont < 0 ) return false;
	}
	for( char c : b ){
		if( c == '(' ) cont++;
		else if( c == ')' ) cont --;
		if( cont < 0 ) return false;
	}
	if( cont != 0 ) return false;
	return true;
}

void fast_read( string& str ){
	char c = 0;
	while( c != '(' && c != ')' ) c = _getchar_nolock();
	while( c == '(' || c == ')' ){
		str += c;
		c = _getchar_nolock();
	}
}

int main(){
	int N; cin >> N;
	vector<string> brackets(N,"");

	for( int i=0; i<N; i++ ){
		fast_read( brackets[i] );
	}

	int pairs = 0;
	for( int i=0; i<N; i++ ){
		for( int j=i+1; j<N; j++ ){
			if( check(brackets[i],brackets[j]) ) pairs++;
			if( check(brackets[j],brackets[i]) ) pairs++;
		}
		if( check(brackets[i],brackets[i]) ) pairs++;
	}
	cout << pairs;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont < 0 ) return false;
	}
	for( char c : b ){
		if( c == '(' ) cont++;
		else if( c == ')' ) cont --;
		if( cont < 0 ) return false;
	}
	if( cont != 0 ) return false;
	return true;
}

vector<string> corrette;

char esiti[450000];
bool controlla( int i ){
	if( esiti[i] ) return esiti[i] == 1;

	if( check(corrette[i],"") ){
		esiti[i] = 1;
	}else{
		esiti[i] = -1;
	}
	return esiti[i] == 1;
}

int main(){
	//freopen("input.txt","r",stdin);
	int N; cin >> N;

	for( int i=0; i<N; i++ ){
		string str; cin >> str;
		corrette.push_back(str);
	}

	long long pairs=0;
	for( int i=0; i<N; i++ ){
		if( corrette[i].front() == '(' ){
			if( controlla(i) == true ){
				pairs++;
				for( int j=i+1;j<N;j++){
					if( controlla(j) == 1 ){
						pairs++;
					}
				}
			}else{
				for( int j=i+1;j<N;j++ ){
					if( check(corrette[i],corrette[j]) == 1 ){
						pairs++;
					}
					if( check(corrette[j],corrette[i]) == 1 ){
						pairs++;
					}
				}
			}
		}else{
			for( int j=i+1;j<N;j++ ){
				if( check(corrette[i],corrette[j]) == 1 ){
					pairs++;
				}
				if( check(corrette[j],corrette[i]) == 1 ){
					pairs++;
				}
			}
		}
	}

	cout << pairs;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a ){
	int cont = 0;
	if(a.size()%2) return false;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
	}
	return cont==0;
}

bool verifica( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
	}

	for( char c : b ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
	}
	return cont==0;
}

int conta( const string& a ){
	int cont = 0;
	for( char c : a ){
		if( c == '(') cont++;
		else cont--;
	}
	return cont;
}

unordered_map<string,int> conteggi;

int main(){
//	freopen("input.txt","r",stdin);
	//ios_base::sync_with_stdio(false);
	//cin.tie(0);
	int N; cin >> N;

	vector<string> iniziali;
	vector<string> finali;

	long long sol = 0;
	long long giuste = 0;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;

		if( check(str) ) giuste++;
		else if( str[0] == ')' && str.back() == '(' ) continue;
		else{
			int cont = conta(str);
			if( cont > 0 ) iniziali.push_back( str );
			else finali.push_back( str );
			conteggi[str] ++;
		}
	}

	sol = giuste+(giuste-1)*giuste;
	for( auto& it : iniziali ){
		for( auto& j : finali ){
			if( verifica(it,j) ){
				sol += conteggi[j];
				break;
			}
		}
	}
	cout << sol;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 300500

long long aperte[MAXN];
long long chiuse[MAXN];

int main(){
//	freopen("input.txt","r",stdin);
	int N; cin >> N;

	while( N-- ){
		string str; cin >> str;
		long long open = 0;
		long long close = 0;
		if( str.back() == '(' && str.front() == ')' ) continue;

		for( char c : str ){
			if( c == '(' ){
				open++;
			}else{
				if( open == 0 ){
					close++;
				}else{
					open--;
				}
			}
		}
		if( open==0 && close==0 ){
			aperte[0]++;
			chiuse[0]++;
		}else if( open==0 ){
			chiuse[close]++;
		}else if( close==0 ){
			aperte[open]++;
		}
	}

	unsigned long long sol=0;
	for( int i=0; i<300400; i++ ){
		sol += aperte[i]*chiuse[i];
	}
	cout << sol;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a ){
	int cont = 0;
	if(a.size()%2) return false;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
	}
	return cont==0;
}

bool verifica( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}

	for( char c : b ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}
	return cont==0;
}

int conta( const string& a ){
	int cont = 0;
	for( char c : a ){
		if( c == '(') cont++;
		else cont--;
	}
	return cont;
}

unordered_map<string,int> conteggi;

int main(){
//	freopen("input.txt","r",stdin);
	//ios_base::sync_with_stdio(false);
	//cin.tie(0);
	int N; cin >> N;

	vector<string> iniziali;
	vector<string> finali;

	long long sol = 0;
	long long giuste = 0;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;

		if( check(str) ) giuste++;
		else if( str[0] == ')' && str.back() == '(' ) continue;
		else{
			if( str.front() == '(' ) iniziali.push_back( str );
			else finali.push_back( str );
		}
	}

	sol = giuste+(giuste-1)*giuste;
	for( int i=0; i<iniziali.size(); i++ ){
		auto& it = iniziali[i];
		for( auto& j : finali ){
			if( verifica(it,j) ){
				sol += 1;
			}
		}
		for( int j=i+1; j<iniziali.size(); j++ ){
			if( verifica(it,iniziali[j]) ) sol+=1;
			if( verifica(iniziali[j],it) ) sol+=1;
		}
	}
	cout << sol;
}


#include <bits/stdc++.h>
using namespace std;

bool check( const string& a ){
	int cont = 0;
	if(a.size()%2) return false;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}
	return cont==0;
}

bool verifica( const string& a, const string& b ){
	int cont = 0;
	for( char c : a ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}

	for( char c : b ){
		if( c == '(' ) cont ++;
		else if( c == ')' ) cont--;
		if( cont<0 ) return false;
	}
	return cont==0;
}

unordered_map<string,int> conteggi;
int main(){
//	freopen("input.txt","r",stdin);
	//ios_base::sync_with_stdio(false);
	//cin.tie(0);
	int N; cin >> N;

	vector<string> iniziali;
	vector<string> finali;

	long long sol = 0;
	long long giuste = 0;
	for( int i=0; i<N; i++ ){
		string str; cin >> str;

		if( check(str) ) giuste++;
		else if( str[0] == ')' && str.back() == '(' ) continue;
		else{
			if( str.front() == '(' ) iniziali.push_back( str );
			else finali.push_back( str );
		}
		conteggi[str] ++;
	}

	sol = giuste+(giuste-1)*giuste;
	for( int i=0; i<iniziali.size(); i++ ){
		auto& it = iniziali[i];
		for( auto& j : finali ){
			if( verifica(it,j) ){
				sol += conteggi[j];
				break;
			}
		}
		for( int j=i+1; j<iniziali.size(); j++ ){
			if( verifica(it,iniziali[j]) ) sol+=1;
			if( verifica(iniziali[j],it) ) sol+=1;
		}
	}
	cout << sol;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    long n;
    cin >> n;
    vector<pair<int, int>> v;
    char ch;
    cin.get(ch);
    for (int i = 0; i < n; ++i) {
        long sumO = 0, sumC = 0;
        while (cin.get(ch) && ch != '\n') {
            if (ch == '(')
                sumO++;
            else if (ch == ')' && sumO > 0)
                sumO--;
            else if (ch == ')' && sumO == 0)
                sumC++;
        }
        v.emplace_back(sumC, sumO);
    }
//    for (int i = 0; i < n; ++i) {
//        cout << v[i].first << " " << v[i].second << endl;
//    }
//    cout << endl;
    long long beforeSize = v.size();
    v.erase(std::remove_if(v.begin(), v.end(), [](pair<int, int> &t1) { return t1.first == 0 && t1.second == 0; }), v.end());
    long long zeroCount= beforeSize - v.size();
    long long answer = (zeroCount * zeroCount);
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (v[i].first == 0 && v[j].second == 0 && v[i].second == v[j].first)
                answer++;
            if (v[j].first == 0 && v[i].second == 0 && v[j].second == v[i].first)
                answer++;
        }
    }
    cout << answer;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    long n;
    cin >> n;
    vector<pair<int, int>> v;
    char ch;
    cin.get(ch);
    for (int i = 0; i < n; ++i) {
        long sumO = 0, sumC = 0;
        while (cin.get(ch) && ch != '\n') {
            if (ch == '(')
                sumO++;
            else if (ch == ')' && sumO > 0)
                sumO--;
            else if (ch == ')' && sumO == 0)
                sumC++;
        }
        if (sumC == 0 || sumO == 0)
            v.emplace_back(sumC, sumO);
    }

    long long beforeSize = v.size();
    v.erase(std::remove_if(v.begin(), v.end(), [](pair<int, int> &t1) { return t1.first == 0 && t1.second == 0; }), v.end());
    long long zeroCount= beforeSize - v.size();
    long long answer = (zeroCount * zeroCount);
//    for (int i = 0; i < v.size(); ++i) {
//        cout << v[i].first << " " << v[i].second << endl;
//    }
//    cout << endl;
    for (int i = 0; i < v.size(); ++i) {
        for (int j = i + 1; j < v.size(); ++j) {
            if (v[i].first == 0 && v[j].second == 0 && v[i].second == v[j].first)
                answer++;
            if (v[j].first == 0 && v[i].second == 0 && v[j].second == v[i].first)
                answer++;
        }
    }
    cout << answer;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    long n;
    cin >> n;
    vector<pair<int, int>> v;
    char ch;
    cin.get(ch);
    long long zeroCount = 0;
    for (int i = 0; i < n; ++i) {
        long sumO = 0, sumC = 0;
        while (cin.get(ch) && ch != '\n') {
            if (ch == '(')
                sumO++;
            else if (ch == ')' && sumO > 0)
                sumO--;
            else if (ch == ')' && sumO == 0)
                sumC++;
        }
        if (sumC == 0 && sumO == 0)
            zeroCount++;
        else if (sumC == 0 || sumO == 0)
            v.emplace_back(sumC, sumO);
    }

    long long answer = (zeroCount * zeroCount);
//    for (int i = 0; i < v.size(); ++i) {
//        cout << v[i].first << " " << v[i].second << endl;
//    }
//    cout << endl;
    for (int i = 0; i < v.size(); ++i) {
        for (int j = i + 1; j < v.size(); ++j) {
            if (v[i].first == 0 && v[j].second == 0 && v[i].second == v[j].first)
                answer++;
            if (v[j].first == 0 && v[i].second == 0 && v[j].second == v[i].first)
                answer++;
        }
    }
    cout << answer;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>

using namespace std;

unsigned long long Open[300100], Close[300100];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    long long n;
    cin >> n;
//    vector<pair<int, int>> v;
    char ch;
    cin.get(ch);
    unsigned long long zeroCount = 0;
    for (int i = 0; i < n; ++i) {
        long sumO = 0, sumC = 0;
        while (cin.get(ch) && ch != '\n') {
            if (ch == '(')
                sumO++;
            else if (ch == ')' && sumO > 0)
                sumO--;
            else if (ch == ')' && sumO == 0)
                sumC++;
        }
        if (sumC == 0 && sumO == 0)
            zeroCount++;
        else if (sumC == 0) {
            Open[sumO]++;
        } else if (sumO == 0) {
            Close[sumC]++;
        }
//        else if (sumC == 0 || sumO == 0)
//        {
//            v.emplace_back(sumC, sumO);
//        }
    }

    unsigned long long answer = (zeroCount * zeroCount);
//    for (int i = 0; i < v.size(); ++i) {
//        cout << v[i].first << " " << v[i].second << endl;
//    }
//    cout << endl;
    for (int i = 0; i < 300100; ++i) {
        answer += (Open[i] * Close[i]);
    }
//    for (int i = 0; i < v.size(); ++i) {
//        for (int j = i + 1; j < v.size(); ++j) {
//            if (v[i].second == v[j].first)
//                answer++;
//            if (v[j].second == v[i].first)
//                answer++;
//        }
//    }
    cout << answer;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>

using namespace std;

int Open[300100], Close[300100];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    long n;
    cin >> n;
    vector<pair<int, int>> v;
    char ch;
    cin.get(ch);
    long long zeroCount = 0;
    for (int i = 0; i < n; ++i) {
        long sumO = 0, sumC = 0;
        while (cin.get(ch) && ch != '\n') {
            if (ch == '(')
                sumO++;
            else if (ch == ')' && sumO > 0)
                sumO--;
            else if (ch == ')' && sumO == 0)
                sumC++;
        }
        if (sumC == 0 && sumO == 0)
            zeroCount++;
        else if (sumC == 0) {
            Open[sumO]++;
        } else if (sumO == 0) {
            Close[sumC]++;
        }
//        else if (sumC == 0 || sumO == 0)
//        {
//            v.emplace_back(sumC, sumO);
//        }
    }

    long long answer = (zeroCount * zeroCount);
//    for (int i = 0; i < v.size(); ++i) {
//        cout << v[i].first << " " << v[i].second << endl;
//    }
//    cout << endl;
    for (int i = 0; i < 300100; ++i) {
        answer += (Open[i] * Close[i]);
    }
//    for (int i = 0; i < v.size(); ++i) {
//        for (int j = i + 1; j < v.size(); ++j) {
//            if (v[i].second == v[j].first)
//                answer++;
//            if (v[j].second == v[i].first)
//                answer++;
//        }
//    }
    cout << answer;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    long n;
    cin >> n;
    vector<pair<int, int>> v;
    char ch;
    cin.get(ch);
    for (int i = 0; i < n; ++i) {
        long sumO = 0, sumC = 0;
        while (cin.get(ch) && ch != '\n') {
            if (ch == '(')
                sumO++;
            else if (ch == ')' && sumO > 0)
                sumO--;
            else if (ch == ')' && sumO == 0)
                sumC++;
        }
        v.emplace_back(sumC, sumO);
    }
    for (int i = 0; i < n; ++i) {
        cout << v[i].first << " " << v[i].second << endl;
    }
    cout << endl;
    long long beforeSize = v.size();
    v.erase(std::remove_if(v.begin(), v.end(), [](pair<int, int> &t1) { return t1.first == 0 && t1.second == 0; }), v.end());
    long long zeroCount= beforeSize - v.size();
    long long answer = (zeroCount * zeroCount);
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (v[i].first == 0 && v[j].second == 0 && v[i].second == v[j].first)
                answer++;
            if (v[j].first == 0 && v[i].second == 0 && v[j].second == v[i].first)
                answer++;
        }
    }
    cout << answer;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    long n;
    cin >> n;
    vector<pair<int, int>> v;
    char ch;
    cin.get(ch);
    for (int i = 0; i < n; ++i) {
        long sumO = 0, sumC = 0;
        while (cin.get(ch) && ch != '\n') {
            if (ch == '(')
                sumO++;
            else if (ch == ')' && sumO > 0)
                sumO--;
            else if (ch == ')' && sumO == 0)
                sumC++;
        }
        v.emplace_back(sumC, sumO);
    }

    long long beforeSize = v.size();
    v.erase(std::remove_if(v.begin(), v.end(), [](pair<int, int> &t1) { return t1.first == 0 && t1.second == 0; }), v.end());
    long long zeroCount= beforeSize - v.size();
    long long answer = (zeroCount * zeroCount);
//    for (int i = 0; i < v.size(); ++i) {
//        cout << v[i].first << " " << v[i].second << endl;
//    }
//    cout << endl;
    for (int i = 0; i < v.size(); ++i) {
        for (int j = i + 1; j < v.size(); ++j) {
            if (v[i].first == 0 && v[j].second == 0 && v[i].second == v[j].first)
                answer++;
            if (v[j].first == 0 && v[i].second == 0 && v[j].second == v[i].first)
                answer++;
        }
    }
    cout << answer;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    long n;
    cin >> n;
    vector<pair<int, int>> v;
    char ch;
    cin.get(ch);
    for (int i = 0; i < n; ++i) {
        long sumO = 0, sumC = 0;
        while (cin.get(ch) && ch != '\n') {
            if (ch == '(')
                sumO++;
            else if (ch == ')' && sumO > 0)
                sumO--;
            else if (ch == ')' && sumO == 0)
                sumC++;
        }
        v.emplace_back(sumC, sumO);
    }
//    for (int i = 0; i < n; ++i) {
//        cout << v[i].first << " " << v[i].second << endl;
//    }
//    cout << endl;
    long long beforeSize = v.size();
    pair<int,int> p = make_pair(0, 0);
    v.erase(std::remove(v.begin(), v.end(), p), v.end());
    long long zeroCount= beforeSize - v.size();
    long long answer = (zeroCount * zeroCount);
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (v[i].first == 0 && v[j].second == 0 && v[i].second == v[j].first)
                answer++;
            if (v[j].first == 0 && v[i].second == 0 && v[j].second == v[i].first)
                answer++;
        }
    }
    cout << answer;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    long n;
    cin >> n;
    vector<pair<int, int>> v;
    char ch;
    cin.get(ch);
    for (int i = 0; i < n; ++i) {
        long sumO = 0, sumC = 0;
        while (cin.get(ch) && ch != '\n') {
            if (ch == '(')
                sumO++;
            else if (ch == ')' && sumO > 0)
                sumO--;
            else if (ch == ')' && sumO == 0)
                sumC++;
        }
        v.emplace_back(sumC, sumO);
    }
//    for (int i = 0; i < n; ++i) {
//        cout << v[i].first << " " << v[i].second << endl;
//    }
//    cout << endl;
    long long zeroCount= count_if(v.begin(), v.end(), [](pair<int, int> &t1) { return t1.first == 0 && t1.second == 0; });
    long long answer = 0;
    answer += (zeroCount * zeroCount);
    for (int i = 0; i < n; ++i) {
        if (v[i].first == 0 && v[i].second == 0)
            continue;
        for (int j = i + 1; j < n; ++j) {
            if (v[j].first == 0 && v[j].second == 0)
                continue;
            if (v[i].first == 0 && v[j].second == 0 && v[i].second == v[j].first)
                answer++;
            if (v[j].first == 0 && v[i].second == 0 && v[j].second == v[i].first)
                answer++;
        }
    }
    cout << answer;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <math.h>

using namespace std;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    long n;
    cin >> n;
    auto A = new long[n];
    char ch;
    cin.get(ch);
    for (int i = 0; i < n; ++i) {
        long sum = 0;
        while (cin.get(ch) && ch != '\n') {
            if (ch == '(')
                sum++;
            else if (ch == ')')
                sum--;
        }
        A[i] = sum;
    }
    long long zeroCount = count(A, A + n, 0);
    long long answer = 0;
    answer += (zeroCount * zeroCount);
    for (int i = 0; i < n; ++i) {
        if (A[i] == 0)
            continue;
        for (int j = i + 1; j < n; ++j) {
            if (A[j] == 0)
                continue;
            if (A[i] + A[j] == 0)
                answer++;
        }
    }
    cout << answer;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define IO ios_base::sync_with_stdio(0);cin.tie(0);

int main()
{
  IO;
	int n; cin >> n;
	map <int, int> m;
	long long res = 0;
    map <int, int> me;

	for (int i = 0; i < n; ++i)
 	{
        int mn = 1e9 + 7;
        int bal = 0;
		string s; cin >> s;
		for (auto elem : s) {
			if (elem == '(') ++bal;
			else --bal;
			mn = min(mn, bal);
		}
        if (bal >= 0 && mn >= 0) m[bal]++;
        if (bal <= 0 && -bal + mn >= 0) me[-bal]++;
	}

	for (auto elem : me)
        if (elem.first != 0)
            res += (long long)elem.second * m[elem.first];
        else res += (long long)elem.second * elem.second;
	cout << res << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define IO ios_base::sync_with_stdio(0);cin.tie(0);

int main()
{
  IO;
	int n; cin >> n;
	map <int, vector <string>> m;
	vector <int> bal(n);
	vector <bool> v(n, true);

	for (int i = 0; i < n; ++i)
 	{
		string s; cin >> s;
		for (auto elem : s) {
			if (elem == '(') ++bal[i];
			else --bal[i];
			if (bal[i] < 0) v[i] = false;
		}
		m[bal[i]].push_back(s);
	}

	long long res = 0;
	for (int i = 0; i < n; ++i) {
		if (bal[i] >= 0 && v[i]) {
			for (auto elem : m[-bal[i]])
			{
				int b = bal[i];
				bool check = true;
				for (auto elem1 : elem) {
					if (elem1 == '(') ++b;
					else --b;
					if (b < 0) {
						check = false;
						break;
					}
				}
				res += check;
			}
		}
	}
	cout << res << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define IO ios_base::sync_with_stdio(0);cin.tie(0);

int main()
{
  IO;
	int n; cin >> n;
	map <int, vector <int>> m;
	vector <int> bal(n);
	vector <bool> v(n, true);
	vector <int> mn(n);

	for (int i = 0; i < n; ++i)
 	{
		string s; cin >> s;
		for (auto elem : s) {
			if (elem == '(') ++bal[i];
			else --bal[i];
			if (bal[i] < 0) {
				v[i] = false;
				mn[i] = min(mn[i], bal[i]);
			}
		}
		m[bal[i]].push_back(i);
	}

	long long res = 0;
	for (int i = 0; i < n; ++i) {
		if (bal[i] >= 0 && v[i]) {
			for (auto elem : m[-bal[i]])
				res += (bal[i] - mn[elem] >= 0);
		}
	}
	cout << res << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define IO ios_base::sync_with_stdio(0);cin.tie(0);

int main()
{
  IO;
	int n; cin >> n;
	map <int, vector <string>> m;
	vector <int> bal(n);
	vector <bool> v(n, true);

	for (int i = 0; i < n; ++i)
 	{
		string s; cin >> s;
		for (auto elem : s) {
			if (elem == '(') ++bal[i];
			else --bal[i];
			if (bal[i] < 0) v[i] = false;
		}
		m[bal[i]].push_back(s);
	}

	long long res = 0;
	for (int i = 0; i < n; ++i) {
		if (bal[i] >= 0 && v[i]) {
			for (auto elem : m[-bal[i]])
			{
				int b = bal[i];
				bool check = true;
				for (auto elem1 : elem)
					if (elem1 == '(') ++b;
					else --b;
					if (b < 0) {
						check = false;
						break;
					}
				res += check;
			}
		}
	}
	cout << res << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define IO ios_base::sync_with_stdio(0);cin.tie(0);

int main()
{
  IO;
	int n; cin >> n;
	map <int, vector <int>> m;
	vector <int> bal(n);
	vector <int> mn(n, 1e9 + 7);

	for (int i = 0; i < n; ++i)
 	{
		string s; cin >> s;
		for (auto elem : s) {
			if (elem == '(') ++bal[i];
			else --bal[i];
			mn[i] = min(mn[i], bal[i]);
		}
		m[bal[i]].push_back(i);
	}

	long long res = 0;
	for (int i = 0; i < n; ++i) {
		if (bal[i] >= 0 && mn[i] >= 0) {
			for (auto elem : m[-bal[i]])
				res += (bal[i] + mn[elem] >= 0);
		}
	}
	cout << res << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define IO ios_base::sync_with_stdio(0);cin.tie(0);

int main()
{
  IO;
	int n; cin >> n;
	map <int, vector <int>> m;
	long long res = 0;

	for (int i = 0; i < n; ++i)
 	{
        int mn = 1e9 + 7;
        int bal = 0;
		string s; cin >> s;
		for (auto elem : s) {
			if (elem == '(') ++bal;
			else --bal;
			mn = min(mn, bal);
		}

		if (bal > 0 && mn >= 0) {
		for (auto elem : m[-bal])
            res += (elem + bal >= 0);
        }
        else if (bal < 0) {
		for (auto elem : m[-bal])
            res += (elem >= 0 && -bal + mn >= 0);
        }
        else {
            for (auto elem : m[bal])
                res += 2 * (mn >= 0 && elem >= 0);
            res += (mn >= 0);
        }
		m[bal].push_back(mn);
	}
	cout << res << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define IO ios_base::sync_with_stdio(0);cin.tie(0);

int main()
{
  IO;
	int n; cin >> n;
	map <int, vector <int>> m;
	vector <int> bal(n);
	vector <int> mn(n, 1e9 + 7);

	for (int i = 0; i < n; ++i)
 	{
		string s; cin >> s;
		for (auto elem : s) {
			if (elem == '(') ++bal[i];
			else --bal[i];
			mn[i] = min(mn[i], bal[i]);
		}
		m[bal[i]].push_back(i);
	}

	long long res = 0;
	for (int i = 0; i < n; ++i) {
		if (bal[i] >= 0 && mn[i] >= 0) {
			for (auto elem : m[-bal[i]])
				res += (bal[i] - mn[elem] >= 0);
		}
	}
	cout << res << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define IO ios_base::sync_with_stdio(0);cin.tie(0);

int main()
{
  IO;
	int n; cin >> n;
	map <int, int> m;
	for (int i = 0; i < n; ++i)
 	{
		string s; cin >> s;
		int bal = 0;
		for (auto elem : s)
			if (elem == '(') ++bal;
			else --bal;
		++m[bal];
	}

	long long res = 0;
	for (auto elem : m)
		if (elem.first > 0)
		res += m[-elem.first];
		else if (elem.first == 0)
			res += (long long)elem.second * elem.second;

	cout << res << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define IO ios_base::sync_with_stdio(0);cin.tie(0);

int main()
{
  IO;
	int n; cin >> n;
	map <int, vector <int>> m;
	long long res = 0;

	for (int i = 0; i < n; ++i)
 	{
        int mn = 1e9 + 7;
        int bal = 0;
		string s; cin >> s;
		for (auto elem : s) {
			if (elem == '(') ++bal;
			else --bal;
			mn = min(mn, bal);
		}
        if (bal > 0) {
		for (auto elem : m[-bal])
            res += (elem + bal >= 0);
        }
        else if (bal < 0) {
		for (auto elem : m[-bal])
            res += (-bal + mn >= 0);
        }
        else {
            for (auto elem : m[bal])
                res += 2 * (mn >= 0 && elem >= 0);
            res += (mn >= 0);
        }
		m[bal].push_back(mn);
	}
	cout << res << endl;

  return 0;
}


#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
typedef long long                   ll;
typedef long double                 ld;
typedef pair<int,int>               pii;
typedef pair<ll,ll>                 pll;
typedef pair<double,double>         pdd;
typedef pair<ld,ld>                 pld;
typedef pair<string , string>       pss;
#define ts(x)                       to_string(x)
#define ti(x)                       stoi(x)
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define Mp                          make_pair
#define tab                         '\t'
#define sep                         ' '
#define mine(v)                     *min_element(all(v))
#define maxe(v)                     *max_element(all(v))
#define error(x)                    cerr << #x << " = " << x << endl
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define endl                        "\n"
inline ll input(){ll n; cin >> n ; return n;}
ll pw(ll a , ll b)  { return (!b ? 1 : (b & 1 ? a * pw(a * a , b / 2) : pw(a * a , b / 2))); }
int v1[300009], v2[300009];
int main(){
    fast_io;
    //cout << fixed << setprecision(15);
    ll n, t = 0;
    cin >> n;
    string s;
    for(ll i = 0;i < n;i ++){
        cin >> s;
        bool f1 = 0, f2 = 0;
        ll sum = 0;
        for(ll j = 0; j < (int)s.size(); j++){
            if(s[j] == '('){
                sum++;
            }
            else{
                sum--;
                if(sum < 0){
                    f1 = 1;
                    break;
                }
            }
        }
        sum = 0;
        for(ll j = (int)s.size() - 1; j > -1; j--){
            if(s[j] == ')'){
                sum++;
            }
            else{
                sum--;
                if(sum < 0){
                    f2 = 1;
                    break;
                }
            }
        }

        if(f1 && f2){
            continue;
        }
        if(sum <= 0){
            v2[-sum]++;
        }
        else if (sum >= 0){
            v1[sum]++;
        }
    }
    ll ans = 0;
    for(ll i = 0 ; i < 3e5; i++){
        ans += v1[i]*v2[i];
    }
    //cout <<  ans << " ";
    cout << ans;
    return 0;
}


#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
typedef long long                   ll;
typedef long double                 ld;
typedef pair<int,int>               pii;
typedef pair<ll,ll>                 pll;
typedef pair<double,double>         pdd;
typedef pair<ld,ld>                 pld;
typedef pair<string , string>       pss;
#define ts(x)                       to_string(x)
#define ti(x)                       stoi(x)
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define Mp                          make_pair
#define tab                         '\t'
#define sep                         ' '
#define mine(v)                     *min_element(all(v))
#define maxe(v)                     *max_element(all(v))
#define error(x)                    cerr << #x << " = " << x << endl
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define endl                        "\n"
inline ll input(){ll n; cin >> n ; return n;}
ll pw(ll a , ll b)  { return (!b ? 1 : (b & 1 ? a * pw(a * a , b / 2) : pw(a * a , b / 2))); }
int v1[100009], v2[100009];
int main(){
    fast_io;
    //cout << fixed << setprecision(15);
    int n, t = 0;
    cin >> n;
    string s;
    for(int i = 0;i < n;i ++){
        cin >> s;
        bool f1 = 0, f2 = 0;
        int h = 0, sum = 0;
        for(int j = 0; j < (int)s.size(); j++){
            if(s[j] == '('){
                h++;
                sum++;
            }
            else{
                sum--;
                if(h == 0){
                    f1 = 1;
                }
                else{
                    h--;
                }
            }
        }

        if(h){
            f2 = 1;
        }
        if(f1 && f2){
            continue;
        }
        if(sum == 0){
            t++;
        }
        else if(f1){
            v2[-sum]++;
        }
        else{
            v1[sum]++;
        }
    }
    ll ans = 0;
    for(int i = 1 ; i < 1e5; i++){
        ans += v1[i]*v2[i];
    }
    //cout <<  ans << " ";
    ans += t*t;
    cout << ans;
    return 0;
}


#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
typedef long long                   ll;
typedef long double                 ld;
typedef pair<int,int>               pii;
typedef pair<ll,ll>                 pll;
typedef pair<double,double>         pdd;
typedef pair<ld,ld>                 pld;
typedef pair<string , string>       pss;
#define ts(x)                       to_string(x)
#define ti(x)                       stoi(x)
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define Mp                          make_pair
#define tab                         '\t'
#define sep                         ' '
#define mine(v)                     *min_element(all(v))
#define maxe(v)                     *max_element(all(v))
#define error(x)                    cerr << #x << " = " << x << endl
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define endl                        "\n"
inline ll input(){ll n; cin >> n ; return n;}
ll pw(ll a , ll b)  { return (!b ? 1 : (b & 1 ? a * pw(a * a , b / 2) : pw(a * a , b / 2))); }
ll v1[300009], v2[300009];
int main(){
    fast_io;
    //cout << fixed << setprecision(15);
    ll n, t = 0;
    cin >> n;
    string s;
    for(ll i = 0;i < n;i ++){
        cin >> s;
        bool f1 = 0, f2 = 0;
        ll sum = 0;
        for(ll j = 0; j < (int)s.size(); j++){
            if(s[j] == '('){
                sum++;
            }
            else{
                sum--;
                if(sum < 0){
                    f1 = 1;
                    break;
                }
            }
        }
        sum = 0;
        for(ll j = (int)s.size() - 1; j > -1; j--){
            if(s[j] == ')'){
                sum++;
            }
            else{
                sum--;
                if(sum < 0){
                    f2 = 1;
                }
            }
        }

        if(f1 && f2){
            continue;
        }
        if(sum <= 0){
            v2[-sum]++;
        }
        if (sum >= 0){
            v1[sum]++;
        }
    }
    ll ans = 0;
    for(ll i = 0 ; i < 3e5; i++){
        ans += v1[i]*v2[i];
    }
    //cout <<  ans << " ";
    cout << ans;
    return 0;
}


#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
typedef long long                   ll;
typedef long double                 ld;
typedef pair<int,int>               pii;
typedef pair<ll,ll>                 pll;
typedef pair<double,double>         pdd;
typedef pair<ld,ld>                 pld;
typedef pair<string , string>       pss;
#define ts(x)                       to_string(x)
#define ti(x)                       stoi(x)
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define Mp                          make_pair
#define tab                         '\t'
#define sep                         ' '
#define mine(v)                     *min_element(all(v))
#define maxe(v)                     *max_element(all(v))
#define error(x)                    cerr << #x << " = " << x << endl
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define endl                        "\n"
inline ll input(){ll n; cin >> n ; return n;}
ll pw(ll a , ll b)  { return (!b ? 1 : (b & 1 ? a * pw(a * a , b / 2) : pw(a * a , b / 2))); }
int v1[100009], v2[100009];
int main(){
    fast_io;
    //cout << fixed << setprecision(15);
    ll n, t = 0;
    cin >> n;
    string s;
    for(ll i = 0;i < n;i ++){
        cin >> s;
        bool f1 = 0, f2 = 0;
        ll h = 0, sum = 0;
        for(ll j = 0; j < (int)s.size(); j++){
            if(s[j] == '('){
                h++;
                sum++;
            }
            else{
                sum--;
                if(h == 0){
                    f1 = 1;
                }
                else{
                    h--;
                }
            }
        }

        if(h){
            f2 = 1;
        }
        if(f1 && f2){
            continue;
        }
        if(sum == 0){
            t++;
        }
        else if(f1){
            v2[-sum]++;
        }
        else{
            v1[sum]++;
        }
    }
    ll ans = 0;
    for(ll i = 1 ; i < 1e5; i++){
        ans += v1[i]*v2[i];
    }
    //cout <<  ans << " ";
    ans += t*t;
    cout << ans;
    return 0;
}


#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
typedef long long                   ll;
typedef long double                 ld;
typedef pair<int,int>               pii;
typedef pair<ll,ll>                 pll;
typedef pair<double,double>         pdd;
typedef pair<ld,ld>                 pld;
typedef pair<string , string>       pss;
#define ts(x)                       to_string(x)
#define ti(x)                       stoi(x)
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define Mp                          make_pair
#define tab                         '\t'
#define sep                         ' '
#define mine(v)                     *min_element(all(v))
#define maxe(v)                     *max_element(all(v))
#define error(x)                    cerr << #x << " = " << x << endl
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define endl                        "\n"
inline ll input(){ll n; cin >> n ; return n;}
ll pw(ll a , ll b)  { return (!b ? 1 : (b & 1 ? a * pw(a * a , b / 2) : pw(a * a , b / 2))); }
int v1[100009], v2[100009];
int main(){
    fast_io;
    //cout << fixed << setprecision(15);
    int n;
    cin >> n;
    string s;
    for(int i = 0;i < n;i ++){
        cin >> s;
        bool f1 = 0, f2 = 0;
        int h = 0, sum = 0;
        for(int j = 0; j < (int)s.size(); j++){
            if(s[j] == '('){
                h++;
                sum++;
            }
            else{
                sum--;
                if(h == 0){
                    f1 = 1;
                }
                else{
                    h--;
                }
            }
        }
        if(h){
            f2 = 1;
        }
        if(f1 && f2){
            continue;
        }
        if(f1){
            v2[-sum]++;
        }
        else{
            v1[sum]++;
        }
    }
    int ans = 0;
    for(int i = 1 ; i < 1e5; i++){
        ans += v1[i]*v2[i];
    }
    //cout <<  ans << " ";
    ans += v1[0]*v1[0];
    cout << ans;
    return 0;
}


#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
typedef long long                   ll;
typedef long double                 ld;
typedef pair<int,int>               pii;
typedef pair<ll,ll>                 pll;
typedef pair<double,double>         pdd;
typedef pair<ld,ld>                 pld;
typedef pair<string , string>       pss;
#define ts(x)                       to_string(x)
#define ti(x)                       stoi(x)
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define Mp                          make_pair
#define tab                         '\t'
#define sep                         ' '
#define mine(v)                     *min_element(all(v))
#define maxe(v)                     *max_element(all(v))
#define error(x)                    cerr << #x << " = " << x << endl
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define endl                        "\n"
inline ll input(){ll n; cin >> n ; return n;}
ll pw(ll a , ll b)  { return (!b ? 1 : (b & 1 ? a * pw(a * a , b / 2) : pw(a * a , b / 2))); }
int v1[300009], v2[300009];
int main(){
    fast_io;
    //cout << fixed << setprecision(15);
    ll n, t = 0;
    cin >> n;
    string s;
    for(ll i = 0;i < n;i ++){
        cin >> s;
        bool f1 = 0, f2 = 0;
        ll h = 0, sum = 0;
        for(ll j = 0; j < (int)s.size(); j++){
            if(s[j] == '('){
                h++;
                sum++;
            }
            else{
                sum--;
                if(h == 0){
                    f1 = 1;
                }
                else{
                    h--;
                }
            }
        }

        if(h){
            f2 = 1;
        }
        //cout << s << endl;
        cout << f1 << " " << f2 << endl;
        if(f1 && f2){
            continue;
        }
        if(sum == 0){
            t++;
        }
        else if(f1){
            v2[-sum]++;
        }
        else{
            v1[sum]++;
        }
    }
    ll ans = 0;
    for(ll i = 1 ; i < 3e5; i++){
        ans += v1[i]*v2[i];
    }
    //cout <<  ans << " ";
    ans += t*t;
    cout << ans;
    return 0;
}


//                             In The Name Of Allah
//
//                             	Mohammad Hosseini
#include <bits/stdc++.h>
#define	ss second
#define ff first
#define use_fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ret(n) return cout << n, 0
#define se(n) cout << setprecision(n) << fixed
#define pb push_back
//#define int long long
#define ld long double
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;

const int N = 3e5 + 100, OO = 724249387, T = 1e6 + 100, M = 1e9 + 7, P = 6151, SQ = 280, lg = 30;
typedef pair <int, int> pii;
int ans[T], tr[N][lg], st[N], en[N], is[N], fi[N], nxt = 1, gnxt = 1;
vector <int> v[N];
string t[N];

int Add(string s) {
	int nw = 0;
	for(auto u : s) {
		if(!tr[nw][u - 'a'])
			tr[nw][u - 'a'] = nxt++;
		nw = tr[nw][u - 'a'];
	}
	return nw;
}

void Aho_Corasick() {
	queue <int> q;
	for(int i = 0; i < lg; i++)
		if(tr[0][i])
			q.push(tr[0][i]);
	while(!q.empty()) {
		int x = q.front();
		q.pop();
		for(int i = 0; i < lg; i++) {
			if(!tr[x][i])
				tr[x][i] = tr[fi[x]][i];
			else {
				q.push(tr[x][i]);
				fi[tr[x][i]] = tr[fi[x]][i];
			}
		}
	}
}

void ETT(int x) {
	st[x] = gnxt++;
	for(auto u : v[x])
		ETT(u);
	en[x] = gnxt;
}

////////////////////////////////////
int l[T], r[T], k[T], p[T], wh[T];
bool cmp(int x, int y) {
	if(k[x] != k[y])
		return k[x] < k[y];
	return l[x] < l[y];
}
///////////////////////////////
int fen[N];
void upd(int x, int y) {
	for(; x < gnxt; x += (x & -x))
		fen[x] += y;
}

int get(int x) {
	int cnt = 0;
	for(; x; x -= (x & -x))
		cnt += fen[x];
	return cnt;
}
///////////////////////////

int32_t main() {
 	use_fast;
	int n, q;
	cin >> n >> q;
	string s;
	for(int i = 1; i <= n; i++) {
		cin >> t[i];
		is[i] = Add(t[i]);
	}
	Aho_Corasick();
	for(int i = 0; i < nxt; i++)
		if(i != fi[i])
			v[fi[i]].pb(i);
	ETT(0);
	for(int i = 0; i < q; i++) {
		int x, y, z;
		cin >> x >> y >> z;
		p[i << 1] = i << 1;
		p[i << 1 | 1] = i << 1 | 1;
		l[i << 1] = st[is[z]], r[i << 1] = en[is[z]], k[i << 1] = x - 1, wh[i << 1] = -1;
		l[i << 1 | 1] = st[is[z]], r[i << 1 | 1] = en[is[z]], k[i << 1 | 1] = y, wh[i << 1 | 1] = +1;
	}
	q *= 2;
	sort(p, p + q, cmp);
	int pre = 0;
	for(int tp = 0; tp < q; tp++) {
		int i = p[tp];
		while(pre < k[i]) {
			pre++;
			int nw = 0;
			for(auto u : t[pre]) {
				nw = tr[nw][u - 'a'];
				upd(st[nw], +1);
			}
		}
		ans[i / 2] += wh[i] * (get(r[i] - 1) - get(l[i] - 1));
	}
	for(int i = 0; i < q/2; i++)
		cout << ans[i] << endl;
	return 0;
}
/*
be carefull :
1- if not solve after 20 min, read again twice
2- after submit read the code again
3- fun with contest
4- uploaded by ubuntu 20.04
5- ...
*/


#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;

const int LOG = 20;
const int MAXN = 500010;

class FenwickTree
{
	public:

		void update(int i, int v)
		{
			for( ; i < MAXN ; i += i & -i)
				BIT[i] += v;
		}

		int query(int i)
		{
			int ans = 0;

			for( ; i > 0 ; i -= i & -i)
				ans += BIT[i];

			return ans;
		}

		int query(int L, int R) { return query(R) - query(L - 1); }

		FenwickTree() { memset( BIT , 0 , sizeof(BIT) ); }

	private:

		int BIT[MAXN];
};

class SuffixArray
{
	public:

		bool areDifferent(int i, int j)
		{
			if( i + gap > n || j + gap > n ) return true;
			if( posSuffix[i] != posSuffix[j] ) return true;
			return ( posSuffix[i + gap] != posSuffix[j + gap] );
		}

		void build(string& t)
		{
			n = t.size();
			gap = 1; s = t;

			for(int i = 0 ; i < n ; i++)
			{
				tmp[i] = 0;
				indSuffix[i] = i;
				posSuffix[i] = s[i];
			}

			while( tmp[n - 1] != n - 1 )
			{
				sort( indSuffix , indSuffix + n , [&](int i, int j)
				{
					if( posSuffix[i] != posSuffix[j] )
						return posSuffix[i] < posSuffix[j];

					i += gap; j += gap;

					if( i >= n || j >= n ) return i > j;
					return posSuffix[i] < posSuffix[j];
				} );

				for(int i = 1 ; i < n ; i++)
					tmp[i] = tmp[i - 1] + areDifferent( indSuffix[i] , indSuffix[i - 1] );

				for(int i = 0 ; i < n ; i++)
					posSuffix[ indSuffix[i] ] = tmp[i];

				gap <<= 1;
			}

			buildLCPArray();
		}

		void buildLCPArray()
		{
			int curLCP = 0;

		    for(int i = 0 ; i < n ; i++)
		    {
		    	if( posSuffix[i] == n - 1 )
		        {
		            curLCP = 0;
		            continue;
		        }

		        int j = indSuffix[ posSuffix[i] + 1 ];

		        while( i + curLCP < n && j + curLCP < n )
		        {
		        	if( s[i + curLCP] != s[j + curLCP] ) break;
		        	curLCP++;
		        }

		        lcp[ posSuffix[i] ] = curLCP;
		        if( curLCP > 0 ) curLCP--;
		    }

		    for(int i = 0 ; i < n ; i++)
		    	tab[0][i] = lcp[i];

		    for(int k = 0 ; k < LOG - 1 ; k++)
		    	for(int i = 0 ; i + (1 << k) < n ; i++)
		    		tab[k + 1][i] = min( tab[k][i] , tab[k][i + (1 << k)] );
		}

		void getIntervalPrefix(int i, int sz, int& L, int& R)
		{
			L = R = posSuffix[i];

			for(int k = LOG - 1 ; k >= 0 ; k--)
			{
				if( R + (1 << k) < n && tab[k][R] >= sz ) R += (1 << k);

				L -= (1 << k);

				if( L < 0 || tab[k][L] < sz )
					L += (1 << k);
			}
		}

		int getPosSuffix(int i) { return posSuffix[i]; }

	private:

		string s;

		int n, gap;

		int tab[LOG][MAXN];
		int indSuffix[MAXN];
		int posSuffix[MAXN];
		int tmp[MAXN], lcp[MAXN];
};

int n, q;

int ans[MAXN];
int Lstring[MAXN], Rstring[MAXN];
int Lquery[MAXN], Rquery[MAXN], Kquery[MAXN];

string v[MAXN];

FenwickTree BIT;
SuffixArray sArray;

vector<int> sweep[MAXN];

int main()
{
	cin >> n >> q;

	for(int i = 1 ; i <= n ; i++)
		cin >> v[i];

	string conc;

	for(int i = 1 ; i <= n ; i++)
	{
		for(int j = 0 ; j < (int)v[i].size() ; j++)
			conc.push_back( v[i][j] );

		conc.push_back( '$' );
	}

	sArray.build( conc );

	int sum = 0;

	for(int i = 1 ; i <= n ; i++)
	{
		sArray.getIntervalPrefix( sum , (int)v[i].size() , Lstring[i] , Rstring[i] );
		sum += (int)v[i].size() + 1;
	}

	for(int i = 1 ; i <= q ; i++)
	{
		scanf("%d %d %d",&Lquery[i],&Rquery[i],&Kquery[i]);

		sweep[ Rquery[i] ].push_back( i );
		sweep[ Lquery[i] - 1 ].push_back( -i );
	}

	int ind = 0;

	for(int i = 1 ; i <= n ; i++, ind++)
	{
		for(int j = 0 ; j < (int)v[i].size() ; j++, ind++)
			BIT.update( sArray.getPosSuffix(ind) , 1 );

		while( !sweep[i].empty() )
		{
			int cur = sweep[i].back();
			sweep[i].pop_back();

			int signal = 1;
			if( cur < 0 )
				cur = -cur, signal = -signal;

			int L = Lstring[ Kquery[cur] ];
			int R = Rstring[ Kquery[cur] ];

			ans[cur] += signal*BIT.query( L , R );
		}
	}

	for(int i = 1 ; i <= q ; i++)
		printf("%d\n",ans[i]);
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;

const int LOG = 20;
const int MAXN = 400010;

class FenwickTree
{
	public:

		void update(int i, int v)
		{
			for( ; i < MAXN ; i += i & -i)
				BIT[i] += v;
		}

		int query(int i)
		{
			int ans = 0;

			for( ; i > 0 ; i -= i & -i)
				ans += BIT[i];

			return ans;
		}

		int query(int L, int R) { return query(R) - query(L - 1); }

		FenwickTree() { memset( BIT , 0 , sizeof(BIT) ); }

	private:

		int BIT[MAXN];
};

class SuffixArray
{
	public:

		bool areDifferent(int i, int j)
		{
			if( i + gap > n || j + gap > n ) return true;
			if( posSuffix[i] != posSuffix[j] ) return true;
			return ( posSuffix[i + gap] != posSuffix[j + gap] );
		}

		void build(string& t)
		{
			n = t.size();
			gap = 1; s = t;

			for(int i = 0 ; i < n ; i++)
			{
				tmp[i] = 0;
				indSuffix[i] = i;
				posSuffix[i] = s[i];
			}

			while( tmp[n - 1] != n - 1 )
			{
				sort( indSuffix , indSuffix + n , [&](int i, int j)
				{
					if( posSuffix[i] != posSuffix[j] )
						return posSuffix[i] < posSuffix[j];

					i += gap; j += gap;

					if( i >= n || j >= n ) return i > j;
					return posSuffix[i] < posSuffix[j];
				} );

				for(int i = 1 ; i < n ; i++)
					tmp[i] = tmp[i - 1] + areDifferent( indSuffix[i] , indSuffix[i - 1] );

				for(int i = 0 ; i < n ; i++)
					posSuffix[ indSuffix[i] ] = tmp[i];

				gap <<= 1;
			}

			buildLCPArray();
		}

		void buildLCPArray()
		{
			int curLCP = 0;

		    for(int i = 0 ; i < n ; i++)
		    {
		    	if( posSuffix[i] == n - 1 )
		        {
		            curLCP = 0;
		            continue;
		        }

		        int j = indSuffix[ posSuffix[i] + 1 ];

		        while( i + curLCP < n && j + curLCP < n )
		        {
		        	if( s[i + curLCP] != s[j + curLCP] ) break;
		        	curLCP++;
		        }

		        lcp[ posSuffix[i] ] = curLCP;
		        if( curLCP > 0 ) curLCP--;
		    }

		    for(int i = 0 ; i < n ; i++)
		    	tab[0][i] = lcp[i];

		    for(int k = 0 ; k < LOG - 1 ; k++)
		    	for(int i = 0 ; i + (1 << k) < n ; i++)
		    		tab[k + 1][i] = min( tab[k][i] , tab[k][i + (1 << k)] );
		}

		void getIntervalPrefix(int i, int sz, int& L, int& R)
		{
			L = R = posSuffix[i];

			for(int k = LOG - 1 ; k >= 0 ; k--)
			{
				if( R + (1 << k) < n && tab[k][R] >= sz ) R += (1 << k);

				L -= (1 << k);

				if( L < 0 || tab[k][L] < sz )
					L += (1 << k);
			}
		}

		int getPosSuffix(int i) { return posSuffix[i]; }

	private:

		string s;

		int n, gap;

		int tab[LOG][MAXN];
		int indSuffix[MAXN];
		int posSuffix[MAXN];
		int tmp[MAXN], lcp[MAXN];
};

int n, q;

int ans[MAXN];
int Lstring[MAXN], Rstring[MAXN];
int Lquery[MAXN], Rquery[MAXN], Kquery[MAXN];

string v[MAXN];

FenwickTree BIT;
SuffixArray sArray;

vector<int> sweep[MAXN];

int main()
{
	cin >> n >> q;

	for(int i = 1 ; i <= n ; i++)
		cin >> v[i];

	string conc;

	for(int i = 1 ; i <= n ; i++)
	{
		for(int j = 0 ; j < (int)v[i].size() ; j++)
			conc.push_back( v[i][j] );

		conc.push_back( '$' );
	}

	sArray.build( conc );

	int sum = 0;

	for(int i = 1 ; i <= n ; i++)
	{
		sArray.getIntervalPrefix( sum , (int)v[i].size() , Lstring[i] , Rstring[i] );
		sum += (int)v[i].size() + 1;
	}

	for(int i = 1 ; i <= q ; i++)
	{
		scanf("%d %d %d",&Lquery[i],&Rquery[i],&Kquery[i]);

		sweep[ Rquery[i] ].push_back( i );
		sweep[ Lquery[i] - 1 ].push_back( -i );
	}

	int ind = 0;

	for(int i = 1 ; i <= n ; i++, ind++)
	{
		for(int j = 0 ; j < (int)v[i].size() ; j++, ind++)
			BIT.update( sArray.getPosSuffix(ind) , 1 );

		while( !sweep[i].empty() )
		{
			int cur = sweep[i].back();
			sweep[i].pop_back();

			int signal = 1;
			if( cur < 0 )
				cur = -cur, signal = -signal;

			int L = Lstring[ Kquery[cur] ];
			int R = Rstring[ Kquery[cur] ];

			ans[cur] += signal*BIT.query( L , R );
		}
	}

	for(int i = 1 ; i <= q ; i++)
		printf("%d\n",ans[i]);
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 200005, Q = 500005;
int n, q, trans[2 * N][26], fail[2 * N], dep[2 * N], tot = 1, lst = 1, sz, root[2 * N], pos[N], l[Q], r[Q], k[Q];
ll ans[Q];
vector<int> qry[2 * N], g[2 * N];
char in[N];

int extend(int p, int c) {
	if (trans[p][c]) {
		int q = trans[p][c];
		if (dep[q] == dep[p] + 1) return q;
		int nq = ++tot;
		dep[nq] = dep[p] + 1;
		fail[nq] = fail[q];
		copy(trans[q], trans[q] + 26, trans[nq]);
		fail[q] = nq;
		for (; p && trans[p][c] == q; p = fail[p])
			trans[p][c] = nq;
		return nq;
	}
	int np = ++tot;
	dep[np] = dep[p] + 1;
	for (; p && !trans[p][c]; p = fail[p])
		trans[p][c] = np;
	if (!p) fail[np] = 1;
	else {
		int q = trans[p][c];
		if (dep[q] == dep[p] + 1)
			fail[np] = q;
		else {
			int nq = ++tot;
			dep[nq] = dep[p] + 1;
			fail[nq] = fail[q];
			copy(trans[q], trans[q] + 26, trans[nq]);
			fail[q] = fail[np] = nq;
			for (; p && trans[p][c] == q; p = fail[p])
				trans[p][c] = nq;
		}
	}
	return np;
}

struct node {
	int ls, rs;
	ll size;
} T[N * 19];

void update(int &x, int l, int r, int pos) {
	if (!x) x = ++sz;
	++T[x].size;
	if (l == r) return;
	int mid = (l + r) / 2;
	if (pos <= mid) update(T[x].ls, l, mid, pos);
	else update(T[x].rs, mid + 1, r, pos);
}
int merge(int x, int y) {
	if (!x || !y) return x ^ y;
	T[x].size += T[y].size;
	T[x].ls = merge(T[x].ls, T[y].ls);
	T[x].rs = merge(T[x].rs, T[y].rs);
	return x;
}
ll query(int x, int l, int r, int ql, int qr) {
	if (!x || l >= ql && r <= qr) return T[x].size;
	int mid = (l + r) / 2, res = 0;
	if (ql <= mid) res += query(T[x].ls, l, mid, ql, qr);
	if (qr > mid) res += query(T[x].rs, mid + 1, r, ql, qr);
	return res;
}

void dfs(int u) {
	for (int v : g[u]) {
		dfs(v);
		root[u] = merge(root[u], root[v]);
	}
	for (int x : qry[u]) ans[x] = query(root[u], 1, n, l[x], r[x]);
}

int main() {
	scanf("%d%d", &n, &q);
	for (int i = 1; i <= n; ++i) {
		scanf("%s", in);
		lst = 1;
		for (int j = 0; in[j]; ++j) {
			lst = extend(lst, in[j] - 'a');
			update(root[lst], 1, n, i);
		}
		pos[i] = lst;
	}
	for (int i = 1; i <= q; ++i) {
		scanf("%d%d%d", l + i, r + i, k + i);
		qry[pos[k[i]]].push_back(i);
	}
	for (int i = 2; i <= tot; ++i)
		g[fail[i]].push_back(i);
	dfs(1);
	for (int i = 1; i <= q; ++i)
		printf("%lld\n", ans[i]);
	return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <bits/stdc++.h>
using namespace std;
#define long long long
const long MOD = 1e9+7, LINF = 1e18 + 1e16;
const int INF = 1e9+1;
const double EPS = 1e-10;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int N = 4e5+10, M = 5e5+10;

struct node {
    int bound, type, l, r, id;
    node(int bound, int type, int l, int r, int id): bound(bound), type(type), l(l), r(r), id(id) {}
    bool operator<(const node &rhs) const {
        return bound < rhs.bound || (bound == rhs.bound && (type == 0) > (rhs.type == 0)); // check comparator
    }
};

struct fenwick {
    int f[N];
    void update(int i) {
        ++i;
        while(i < N) ++f[i], i += i & -i;
    }
    int query(int i) {
        ++i;
        int ret = 0;
        while(i > 0) {
            ret += f[i];
            i -= i & -i;
        }
        return ret;
    }
};

class TaskE {

    int n, q, len, suff[N], res[M];
    string s[N];
    string concat;

    int sa[N], pos[N], tmp[N], lcp[N];
    void buildSA() {
        int gap;
        auto cmp = [&] (int i, int j) {
            if(pos[i] != pos[j]) return pos[i] < pos[j];
            i += gap;
            j += gap;
            return i < len && j < len ? pos[i] < pos[j] : i > j;
        };
        for(int i = 0; i < len; i++) {
            sa[i] = i;
            pos[i] = concat[i];
        }
        for(gap = 1; ; gap <<= 1) {
            sort(sa, sa + len, cmp);
            for(int i = 1; i < len; i++) tmp[i] = tmp[i-1] + cmp(sa[i-1], sa[i]);
            for(int i = 0; i < len; i++) pos[sa[i]] = tmp[i];
            if(tmp[len - 1] == len - 1) break;
        }
        for(int i = 0, k = 0; i < len; i++, k = (k ? k - 1 : 0)) {
            if(pos[i] == len - 1) {
                k = 0;
                continue;
            }
            int j = sa[pos[i] + 1];
            while(max(i + k, j + k) < len && concat[i + k] == concat[j + k]) ++k;
            lcp[pos[i]] = k;
        }
//        for(int i = 0; i < len; i++) cerr << i << ' ' << lcp[i] << ' ' << concat.substr(sa[i]) << endl;
    }

    int t[2*N];
    void build() {
        for(int i = 0; i < len; i++) t[len + i] = lcp[i];
        for(int i = len - 1; i >= 1; i--) t[i] = min(t[i << 1], t[i << 1 | 1]);
    }

    int query(int l, int r) {
        int ret = INF;
        for(l += len, r += len + 1; l < r; l >>= 1, r >>= 1) {
            if(l & 1) ret = min(t[l++], ret);
            if(r & 1) ret = min(t[--r], ret);
        }
        return ret;
    }

    int lb[N], rb[N];
    vector<node> ops;
    fenwick *fen;

public:
    void solve(istream &cin, ostream &cout) {
        cin >> n >> q;
        for(int i = 0; i < n; i++) {
            cin >> s[i];
            concat.push_back('?');
            suff[i] = concat.length();
            for(char c: s[i]) concat.push_back(c);
        }
        len = concat.length();
        buildSA();
        build();
        for(int i = 0; i < n; i++) {
            for(int j = suff[i]; j < suff[i] + s[i].length(); j++) {
                ops.emplace_back(i, 0, pos[j], pos[j], -1);
//                cerr << "insert " << i << ' ' << j << ' ' << pos[j] << endl;
            }
        }
        for(int i = 0; i < n; i++) {
            int l = 0, r = pos[suff[i]] - 1;
            while(l < r) {
                int mid = l+r >> 1;
                if(query(mid, pos[suff[i]] - 1) >= s[i].length()) r = mid;
                else l = mid + 1;
            }
            if(query(l, pos[suff[i]] - 1) >= s[i].length()) lb[i] = l;
            else lb[i] = pos[suff[i]];

            l = pos[suff[i]], r = len - 1;
            while(l < r) {
                int mid = l+r+1 >> 1;
                if(query(pos[suff[i]], mid) >= s[i].length()) l = mid;
                else r = mid - 1;
            }
            if(query(pos[suff[i]], l) >= s[i].length()) rb[i] = l + 1;
            else rb[i] = pos[suff[i]];
//            cerr << "precompute " << i << ' ' << lb[i] << ' ' << rb[i] << endl;
        }

        for(int i = 0, l, r, k; i < q; i++) {
            cin >> l >> r >> k;
            --l, --r, --k;
            ops.emplace_back(r, 1, lb[k], rb[k], i);
            if(l) ops.emplace_back(l - 1, -1, lb[k], rb[k], i);
        }
        sort(ops.begin(), ops.end());
        fen = new fenwick();
        for(node tt: ops) {
//            cerr << "nodes " << tt.bound << ' ' << tt.type << ' ' << tt.l << ' ' << tt.r << ' ' << tt.id << endl;
            if(tt.type == 0) {
                assert(tt.l == tt.r);
                fen->update(tt.l);
            } else {
                res[tt.id] += tt.type * (fen->query(tt.r) - fen->query(tt.l - 1));
            }
        }
        for(int i = 0; i < q; i++) cout << res[i] << '\n';
    }
};

class Solver {
public:
    void solve(std::istream& in, std::ostream& out) {
        TaskE *obj = new TaskE();
        obj->solve(in, out);
        delete obj;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    Solver solver;
    std::istream& in(std::cin);
    std::ostream& out(std::cout);
    solver.solve(in, out);
    return 0;
}