Source Code
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define ll long long
#define Mid ((L+R)/2);
#define rev(x) reverse(x.begin(),x.end())
#define sortt(x) sort(x.begin(),x.end())
#define deb(x) cout<<#x<<"="<<x<<endl
#define pans(x) x ? cout<<"YES"<<"\n" :cout<<"NO"<<"\n";
#define AB ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define stldeb(x) {cout<< #x << " = " ;for(auto it : x)cout<< it << " ";cout<< endl;}
#define stlpdeb(x){cout<< #x << " : " <<endl;for(auto p : x) cout<<p.fr<< " " <<p.sc<<endl;}
using namespace std;
ll Min=-1e18;
ll Max=1e18;
int mod=1e9+7;
int n;
string s="((())";
int dp[1010][505][1<<6];
int x=28;
int solve(int i,int open,int mask)
{
    if((i-open)>open || open>(n/2))
    {
        return 0;
    }
    if(i==n)
    {
        if((i-open)!=open || mask!=x)
            return 0;
        return 1;
    }
    int &ret=dp[i][open][mask];
    if(ret!=-1)
        return ret;
    ret=0;

    int newmask1,newmask2;
    if(mask==x)
    {
        newmask1=x;
        newmask2=x;
    }
    else
    {
        if(mask & (1<<4))
            mask^=(1<<4);
        mask*=2;
        newmask1=mask+1;
        newmask2=mask;
    }


    ret=(ret+solve(i+1,open+1,newmask1))%mod;
    ret=(ret+solve(i+1,open,newmask2))%mod;

    return ret;
}
int main()
{

    AB


    cin>>n;

    for(int i=0;i<=n+1;i++)
        for(int j=0;j<=(n/2);j++)
            for(int k=0;k<(1<<6);k++)
                dp[i][j][k]=-1;

    cout<<solve(0,0,0)<<"\n";





}
Copy
Bracket Sequence AbeerB
GNU G++17
118 ms
127.8 MB
Accepted