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
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include<bits/stdc++.h>
#define FOR(i,s,e) for(int i=(s);i<=(e);i++)
#define FORD(i,s,e) for(int i=(s);i>=(e);i--)
#define FOREACH(i,c) for( __typeof((c).begin()) i=(c).begin();i!=(c).end();++i)
#define ALL(k) (k).begin(),(k).end()
#define e1 first 
#define e2 second
#define mp make_pair
#define pb push_back
#define eb emplace_back 
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
const bool print=false;
const int MAXN=1000111,MAXL=20111000;

char a[MAXL];

vector<PII> lst[MAXN];
vector<int> htab[MAXN];
int aa[MAXN],bb[MAXN];

int t[MAXN];
LL rv[MAXN];
LL pp[MAXN];


const LL MOD=(LL)1e9+7;
const LL MANS=(LL)1e9+7;

int el(int nrl,int v){
	return lst[nrl][v-aa[nrl]].e2;
}

int h(int nrl,int a,int b){
	LL h1=(a==aa[nrl])?0:htab[nrl][a-aa[nrl]-1];
	
	h1*=pp[b-a+1];h1%=MOD;
	LL h2=htab[nrl][b-aa[nrl]];
	LL ans=(h2+MOD-h1)%MOD;
	if(print)printf("HH %d %d %d:: %lld %lld %lld\n",nrl,a,b,h1,h2,ans);
	return ans;
}

int erased[MAXN];
LL wynik[MAXN];

main(){
	int n;scanf("%d",&n);
	//rv[0]=1;rv[1]=MOD/2+1;
	//printf("%d %lld %lld\n",2,rv[1],(2*rv[1])%MOD);
	pp[0]=1,pp[1]=2;
	//FOR(i,2,n) rv[i]=((LL)rv[i-1]*(LL)rv[1])%MOD;
	FOR(i,2,n) pp[i]=((LL)pp[i-1]*2LL)%MOD;;
	int ob=0;
	int zn=1;
	int ww=0;
	while(true){
		char a=getchar_unlocked();
		if(a=='\n') break;
	}
	while(true){
		char a=getchar_unlocked();
		if(a==0||a=='\n') break;
		if(a=='(') ob++;
		if(a=='~') zn=-1;
		if('0'<=a&&a<='9') ww=(ww*10)+a-'0';
		if(a==')' || a=='v') lst[ob].eb(ww,(zn+1)/2),ww=0,zn=1;
	}
	if(n<=15){
		int mn=(1<<n);
		int wyn=0;
		FORD(nn,mn-1,0){
			int n1=nn;
			FOR(i,1,n) t[i]=n1%2,n1/=2;
			bool ok=true;
			FOR(i,1,ob){
				ok=false;
				for(auto it:lst[i]) {
					if((it.e2==0&&t[it.e1]==0)||(it.e2==1&&t[it.e1]==1)) {ok=true;break;}
				}
				if(ok==false) break;
			}
			if(ok) wyn++;
		}
		printf("%d\n",wyn);
		return 0;
	}
	
	
	
	
	
	
	srand(696969);
	random_shuffle(lst+1,lst+ob+1);
	FOR(i,1,ob) {
		if(lst[i].empty()) break;
		sort(ALL(lst[i]));
		aa[i]=lst[i][0].e1;
		bb[i]=lst[i].back().e1;
		LL hh=0;
		for(auto it:lst[i]){
			hh=((hh*2LL)+it.e2)%MOD;
			htab[i].pb(hh);
		}
	}
	
	FOR(i,1,ob){
		if(erased[i]) continue;
		FOR(j,1,ob){
			if(i==j) continue;//szukam i w j
			if(erased[j]) continue;
			if(aa[j]>aa[i]||bb[i]>bb[j]) continue;//czyli aa[j]<=aa[i] <= bb[i]<=bb[j]
			if(h(i,aa[i],bb[i])==h(j,aa[i],bb[i])) erased[j]=1;
		}
	}
	if(print){
		puts("A");
		FOR(i,1,ob){
			printf("%d %d:: ",i,erased[i]);
			int xd=0;
			for(auto it:lst[i]) {
				printf("%d %d %d, ",it.e1,it.e2,htab[i][xd]);
				xd++;
			}
			puts("");
			FOR(x,aa[i],bb[i])
				FOR(y,x,bb[i]) printf("\t%d %d:: %d\n",x,y,h(i,x,y));
			
		}
	}
	vector<PII> V;
	FOR(i,1,ob)
		if(!erased[i]) V.eb(bb[i],i);
	sort(ALL(V));
	int vs=(int)V.size()-1;
	LL wtotal=pp[n];;
	FOR(i0,0,vs){
		int i=V[i0].e2;
		int a=aa[i],b=bb[i];
		wynik[i]=pp[a-1];
		FOR(i1,0,i0-1){
			int j=V[i1].e2;
			if(bb[j]<aa[i])
				wynik[i]=(wynik[i]+MOD-(wynik[j]*pp[a-bb[j]-1])%MOD)%MOD;
				
			else if(h(i,aa[i],bb[j])==h(j,aa[i],bb[j]))
					wynik[i]=(wynik[i]+MOD-wynik[j])%MOD;
		}
		LL w1=(wynik[i]*pp[n-bb[i]])%MOD;
		wtotal=(wtotal+MOD-w1)%MOD;
	}
	printf("%lld\n",wtotal);
	
	
		
		
	
	
	
	
	//FOR(i,1,ob){printf("%d:: ",i);for(auto it:lst[i]) printf("%d ",it);puts("");}
	
	
	
	
}