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
170
171
172
173
174
175
176
177
178
179
180
181
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

#define D(x)

#define I long long
#define N 1000000007

int n;


I mypow(I x,int n) {
	I retu = 1;
	while(n!=0) {
		if(n&1) {
			retu*=x;
			retu%=N;
		}
		n>>=1;
		x*=x;
		x%=N;
	}
	return retu;
}


vector<vector<int> *> F;

bool cmp(const vector<int> *a, const vector<int> * b) {
	return *(a->begin()) < *(b->begin());
}

void printC(vector<int>::iterator begin, vector<int>::iterator end) {
	while(begin != end) {
		int t = *begin;
		begin++;
		if(t&1) cout << "~";
		cout << t/2 << " ";
	}
	cout << "\n";
}
void printF()
{
	for(auto t: F) {
		printC(t->begin(), t->end());
	}
}

struct Tree {
	Tree(): v0(nullptr), v1(nullptr), x(0){}
	Tree(I x): v0(nullptr), v1(nullptr), x(x){}
	Tree * v0;
	Tree * v1;
	I x;
};

void addT(Tree &x, Tree *y) {
	D(cout <<"addT" << "\n");
	if(y == nullptr) return;
	x.x+=y->x;
	x.x%=N;
	if(x.v0 == nullptr) {
		x.v0 = y->v0;
	} else {
		addT(*x.v0, y->v0);
	}
	if(x.v1 == nullptr) {
		x.v1 = y->v1;
	} else {
		addT(*x.v1, y->v1);
	}
}

int pos = 1;
Tree * tree = new Tree;

void printT(Tree * t) {
	if(t == nullptr) return;
	cout << "x:" << t->x;
	if(t->v0) {
		cout << " v0: [";
		printT(t->v0);
		cout << "] ";
	}
	if(t->v1) {
		cout << " v1:[";
		printT(t->v1);
		cout << "] ";
	}
}


void moveTo(int pos2) {
	D(cout <<"moveTo:" << pos << " -> " << pos2 << "\n");
	while(pos != pos2) {
		if(tree->v0 == nullptr) {
			tree->v0 = tree->v1;
		} else {
			addT(*(tree->v0), tree->v1);
		}
		tree->v1 = nullptr;
		if(tree->v0 != nullptr) {
			tree->v0->x += tree->x;
			tree->v0->x %= N;
			tree = tree->v0;
		}
		pos++;
	}
	D(cout << "moveTo tree: "; printT(tree); cout << "\n");
}

#define inv2 (N/2 + 1)

void add(vector<int>::iterator begin, vector<int>::iterator end, I div, I sum) {
	Tree *t = tree;
	D(cout <<"add div: " << div << " sum: " << sum << "\n");
	D(cout << "add C: "; printC(begin, end); cout << "\n");
	while(begin != end) {
		D(cout <<"add" << "\n");
		sum -= t->x*div;
		D(sum %= N; sum +=N);
		sum %= N;
		D(cout << "sum: " << sum << "\n");
		div *= 2;
		div %= N;
		int v = *begin;
		begin++;
		if(v&1) {
			if(t->v1 == nullptr) t->v1 = new Tree;
			t = t->v1;
		} else {
			if(t->v0 == nullptr) t->v0 = new Tree;
			t = t->v0;
		}
	}
	t->x = sum;
	t->v0 = nullptr;
	t->v1 = nullptr;
	D(cout << "add tree: "; printT(tree); cout << "\n");
}

int main()
{
	scanf("%d", &n);
	char c;
	vector<int> * K;
	int neg = 0,x;
	while(scanf("%c", &c)>0) {
//		D(cout << "c: " << c << "\n");
		if(c == '(') {
			K = new vector<int>();
		} else if( c == ')') {
			F.push_back(K);
		} else if( c == '~') {
			neg = 1;
		} else if( c == 'x') {
			scanf("%d",&x);
			K->push_back(2*x+neg);
			neg = 0;
		}
	}
	for(auto t: F) {
		sort(t->begin(),t->end());
	}
	sort(F.begin(), F.end(), cmp);
	D(printF());

	for(auto t: F) {
		int first = *(t->begin());
		moveTo(first/2);
		add(t->begin(), t->end(), mypow(inv2,t->size()), mypow(2,n-t->size()));
		D(printT(tree); cout << "\n");
	}
	moveTo(n+1);
	D(cout << "tree->x:" << tree->x << "\n");
	cout << (mypow(2,n) - tree->x + N)%N << "\n";
}