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
#include <bits/stdc++.h>
#define REP(a,b) for(int a=0; a<(b); ++a)
#define FWD(a,b,c) for(int a=(b); a<(c); ++a)
#define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d)
#define BCK(a,b,c) for(int a=(b); a>(c); --a)
#define ALL(a) (a).begin(), (a).end()
#define SIZE(a) ((int)(a).size())
#define VAR(x) #x ": " << x << " "
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
#define gcd __gcd
#define x first
#define y second
#define st first
#define nd second
#define pb push_back

using namespace std;

template<typename T> ostream& operator<<(ostream &out, const vector<T> &v){ out << "{"; for(const T &a : v) out << a << ", "; out << "}"; return out; }
template<typename S, typename T> ostream& operator<<(ostream &out, const pair<S,T> &p){ out << "(" << p.st << ", " << p.nd << ")"; return out; }

typedef long long LL;
typedef pair<int, int> PII;
typedef long double K;
typedef vector<int> VI;

const int dx[] = {0,0,-1,1}; //1,1,-1,1};
const int dy[] = {-1,1,0,0}; //1,-1,1,-1};

const int MAXN = 1000010;
const int MAXM = 1000010;

const int MOD = 1000000007;

typedef pair<int, int> lit;

void addmod(int &a, int b){
	a += b;
	if(a >= MOD) a -= MOD;
}

int n, m;
vector<lit> cls[MAXM];
vector<int> start[MAXN];

void readF(){
	char c;
	while(scanf(" %c", &c) > 0){
		if(c == ')'){
			++m;
		}else if(c == 'x'){
			int i;
			scanf("%d", &i);
			cls[m].push_back(lit(0, i-1));
		}else if(c == '~'){
			int i;
			scanf("%*c%d", &i);
			cls[m].push_back(lit(1, i-1));
		}
	}
}

bool cmp(lit a, lit b){
	return a.nd < b.nd;
}

struct node {
	int nxt[2];
	int res;
};

vector<node> A;

int new_node(int nxt0 = -1, int nxt1 = -1){
	A.push_back(node());
	A.back().nxt[0] = nxt0;
	A.back().nxt[1] = nxt1;
	A.back().res = 0;
	return SIZE(A)-1;
}

vector<bool> V;
vector<int> O;

void topsort(int u){
	if(u == -1 || V[u]) return;
	V[u] = 1;
	FWD(r,0,2)
		topsort(A[u].nxt[r]);
	O.push_back(u);
}

int main(){
	scanf("%d", &n);
	readF();
	FWD(i,0,m){
		sort(cls[i].begin(), cls[i].end(), cmp);
		start[cls[i][0].nd].push_back(i);
	}
	int root = new_node();
	BCK(i,n-1,-1){
		root = new_node(root, root);
		int suf, cur;
		for(int c : start[i]){
			suf = root;
			root = new_node(-1, -1);
			cur = root;
			for(lit x : cls[c]){
				int tmp = new_node();
				A[cur].nxt[x.st] = tmp;
				if(suf != -1) A[cur].nxt[1-x.st] = A[suf].nxt[1-x.st];
				cur = A[cur].nxt[x.st];
				if(suf != -1) suf = A[suf].nxt[x.st];
			}
		}
	}
	V.resize(SIZE(A));
	topsort(root);
	A[root].res = 1;
	reverse(O.begin(), O.end());
	for(int u : O)
		FWD(r,0,2){
			int v = A[u].nxt[r];
			if(v != -1) addmod(A[v].res, A[u].res);
		}
	printf("%d\n", A[0].res);
	return 0;
}