# Codeforces Round 106 (Div. 2) D. Coloring Brackets

## Description

Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a solution. Today you will face it.

You are given string s. It represents a correct bracket sequence. A correct bracket sequence is the sequence of opening ("(") and closing (")") brackets, such that it is possible to obtain a correct mathematical expression from it, inserting numbers and operators between the brackets. For example, such sequences as "(())()" and "()" are correct bracket sequences and such sequences as ")()" and "(()" are not.

In a correct bracket sequence each bracket corresponds to the matching bracket (an opening bracket corresponds to the matching closing bracket and vice versa). For example, in a bracket sequence shown of the figure below, the third bracket corresponds to the matching sixth one and the fifth bracket corresponds to the fourth one.

You are allowed to color some brackets in the bracket sequence so as all three conditions are fulfilled:

Each bracket is either not colored any color, or is colored red, or is colored blue.
For any pair of matching brackets exactly one of them is colored. In other words, for any bracket the following is true: either it or the matching bracket that corresponds to it is colored.
No two neighboring colored brackets have the same color.
Find the number of different ways to color the bracket sequence. The ways should meet the above-given conditions. Two ways of coloring are considered different if they differ in the color of at least one bracket. As the result can be quite large, print it modulo 1000000007.

### 输入描述:

The first line contains the single string s (2 ≤ |s| ≤ 700) which represents a correct bracket sequence.

### 输出描述:

Print the only number — the number of ways to color the bracket sequence that meet the above given conditions modulo 1000000007

Examples
input
(())
output
12
input
(()())
output
40
input
()
output
4

## Probelm solving

$dp[l][r][i][j]$所代表的就是从$l$$r$这个区间内的括号的染色方案数。

dp[l][r][i][j]=(dp[l][r][i][j]+dp[l+1][r-1][k][p])%mod


dp[l][r][i][j]=(dp[l][r][i][j]+dp[l][m[l]][i][k]*dp[m[l]+1][r][p][j]%mod)%mod


## Code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
const ll maxn = 1e3+10;
const ll mod = 1e9+7;
const ll INF = 0x3f3f3f3f;
ll d[4][2]={1,0,0,1,-1,0,0,-1};
ll a[maxn],m[maxn],vis[maxn][maxn],dp[maxn][maxn][3][3];
void dfs(ll l,ll r){
if(vis[l][r])   return ;
vis[l][r]=1; //记忆化搜索
if(l+1==r){
dp[l][r][0][1]=dp[l][r][1][0]=1;
dp[l][r][0][2]=dp[l][r][2][0]=1;
}else if(m[l]==r){
dfs(l+1,r-1);
for(ll i=0;i<3;i++){
for(ll j=0;j<3;j++){
for(ll k=0;k<3;k++){
for(ll p=0;p<3;p++){
if((i==0||j==0)&&(i!=0||j!=0)&&(i!=k||(i==0||k==0))&&(j!=p||(j==0||p==0)))//i和j是匹配位置上的两个染色需要进行判断，i和k，j和p是两组相邻的颜色所以也需要进行判断
dp[l][r][i][j]=(dp[l][r][i][j]+dp[l+1][r-1][k][p])%mod;
}
}
}
}
}else{
dfs(l,m[l]);dfs(m[l]+1,r);
for(ll i=0;i<3;i++){
for(ll j=0;j<3;j++){
for(ll k=0;k<3;k++){
for(ll p=0;p<3;p++){
if((i==0||k==0)&&(i!=0||k!=0)&&(k!=p||(k==0&&p==0)))//因为i和k是两个匹配位置上染的色，所以需要进行判断，k和p是相邻的两个位置所以需要判断
dp[l][r][i][j]=(dp[l][r][i][j]+dp[l][m[l]][i][k]*dp[m[l]+1][r][p][j]%mod)%mod;
}
}
}
}
}
}
int main() {
ios::sync_with_stdio(0);
#ifdef Uncle_drew
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#else
#endif
string s,t=" ";
cin>>s;t+=s;
ll len=t.size()-1,pos=1;
for(int i=1;i<=len;i++){
if(t[i]=='(')   a[pos++]=i;
else m[a[--pos]]=i;
}
dfs(1,len);
ll ans=0;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
ans=(ans+dp[1][len][i][j])%mod;
}
}//所有答案累积起来
cout<<ans<<endl;
return 0;
}