1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| #include <bits/stdc++.h> using namespace std; #define FO(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout) #define fo(i,j,k) for(int i=(j),end_i=(k);i<=end_i;i++) #define ff(i,j,k) for(int i=(j),end_i=(k);i< end_i;i++) #define fd(i,j,k) for(int i=(j),end_i=(k);i>=end_i;i--) #define DEBUG(x) cerr<<#x<<"="<<x<<endl #define all(x) (x).begin(),(x).end() #define cle(x) memset(x,0,sizeof(x)) #define lowbit(x) ((x)&-(x)) #define ll long long #define ull unsigned ll #define db double #define lb long db #define pb push_back #define mp make_pair #define fi first #define se second inline int read() { int x=0; char ch=getchar(); bool f=0; for(;ch<'0'||ch>'9';ch=getchar()) if(ch=='-') f=1; for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+(ch^48); return f?-x:x; } #define CASET fo(___,1,read()) const ll mod=1e9+7; const int N=1004; inline ll Add(ll x,ll y) { x+=y; return x<mod?x:x-mod; } int n; ll f[N][N],C[N][N],g[N],tmp[N]; int siz[N]; int ne[N<<1],head[N],ver[N<<1],col[N<<1],tot; inline void add(int x,int y,int c) { ver[++tot]=y; col[tot]=c; ne[tot]=head[x]; head[x]=tot; ver[++tot]=x; col[tot]=c; ne[tot]=head[y]; head[y]=tot; } void dfs(int u,int pre) { siz[u]=1; f[u][1]=1; for(int x=head[u],v;x;x=ne[x]) if((v=ver[x])!=pre) { dfs(v,u); fo(i,0,siz[v]) tmp[i]=0; fo(i,1,siz[v]) tmp[i]=Add(tmp[i-1],f[v][i]); fo(i,0,siz[u]+siz[v]) g[i]=0; if(col[x]==v) fo(i,1,siz[u]) fo(j,0,siz[v]) g[i+j]=Add(g[i+j],C[i+j-1][i-1]*C[siz[u]+siz[v]-i-j][siz[u]-i]%mod*f[u][i]%mod*(tmp[siz[v]]-tmp[j]+mod)%mod); else fo(i,1,siz[u]) fo(j,0,siz[v]) g[i+j]=Add(g[i+j],C[i+j-1][i-1]*C[siz[u]+siz[v]-i-j][siz[u]-i]%mod*f[u][i]%mod*tmp[j]%mod); siz[u]+=siz[v]; fo(i,1,siz[u]) f[u][i]=g[i]; } } inline void init() { fo(i,1,n) fo(j,0,n) f[i][j]=0; fo(i,1,n) head[i]=0; fo(i,1,tot) ver[i]=col[i]=ne[i]=0; tot=0; } int main() { C[0][0]=1; fo(i,1,1000) { C[i][0]=1; fo(j,1,i) C[i][j]=Add(C[i-1][j-1],C[i-1][j]); } CASET { n=read(); int x,y; char sig[4]; ll ans=0; fo(i,2,n) { x=read(); scanf("%s",sig); y=read(); x++; y++; add(x,y,sig[0]=='<'?x:y); } dfs(1,0); fo(i,1,n) (ans+=f[1][i])%=mod; printf("%lld\n",ans); init(); } return 0; }
|