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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <cstdio>
#include <vector>
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
//#define DEBUG_PARSE
//#define DEBUG
using namespace std;

const int MAX = 1000100;
const int MODULO = 1000000007;

// TODO - porównywanie hashowaniem
// TODO - zamienić long long na int

struct Clause {
	int beg, end;
	long long good_vars;
	vector<bool> its; // iterals
	int included, pid;

	Clause(){}
	Clause(int beg, int end): beg(beg), end(end){}
};

int n, m;
long long W[MAX]; 
Clause __C[MAX];
Clause *C[MAX];
vector<Clause*> Q;

inline long long MOD(long long x) {
	if(x >= MODULO) return x % MODULO;
	return x;
}

long long pow(int a) {
	if(a < 0) exit(1);
	long long ret = 1;
	long long b = 2;
	while(a) {
		if(a & 1) ret = MOD(ret * b);
		b = MOD(b * b);
		a >>= 1;
	}
	return ret;
}

bool cross(Clause *a, Clause *b) {
	if(b->beg < a->beg) swap(a,b);
	if(a->end < b->beg) return false;
	for(int ai = b->beg - a->beg, bi = 0; ai <= a->end - a->beg && bi <= b->end - b->beg; ai++, bi++) {
		if(a->its[ai] != b->its[bi]) return false;
	}

	return true;
}

bool inside(Clause *a, Clause *b) {
	if(b->beg < a->beg) swap(a,b);
	if(b->end <= a->end) return true;
	return false;
}

bool cmp(const Clause *a, const Clause *b) {
	return a->end > b->end;
}

void solve_clause(Clause *cl, int pid) {
	Clause __temp = Clause(cl->beg, cl->beg);
	auto ptr = upper_bound(Q.rbegin(), Q.rend(), &__temp, cmp);

	long long good1 = 1, good2 = 1;
	if(ptr != Q.rend()) good1 = (*ptr)->good_vars;
	if(Q.size()) good2 = Q.back()->good_vars;

	#ifdef DEBUG
	if(ptr != Q.rend()) printf("\tpid %d %d\n", (*ptr)->pid, Q.back()->pid);
	printf("\tgood %lld %lld\n", good1, good2);
	#endif

	int end1 = 0, end2 = 0;
	if(ptr != Q.rend()) end1 = (*ptr)->end;
	if(Q.size()) end2 = Q.back()->end;

	#ifdef DEBUG
	printf("\tend %d %d\n", end1, end2);
	#endif
	long long all = MOD(good2 * pow(cl->end - end2));
	long long wrong = pow(cl->beg - 1 - end1);

	wrong = MOD(wrong * good1);

	#ifdef DEBUG
	printf("\tall/wrong %lld %lld\n", all, wrong);
	#endif

	vector<Clause*> tempV(Q.rbegin(), ptr);
	sort(tempV.begin(), tempV.end(), [](const Clause *a, const Clause *b) -> bool {
		return a->beg > b->beg;
	});
	vector<Clause*> S;
	bool is_inside = false;
	for(auto it = Q.rbegin(); it != ptr; it++) {
		bool bol = true;
		for(auto jt = Q.rbegin(); jt != it; jt++) {
			if(cross(*it, *jt) && (*jt)->included == pid) {
				bol = false;
				break;
			}
		}
		if(bol) {
			if((*it)->end < cl->beg || cross(*it, cl)) {
				S.push_back(*it);
				(*it)->included = pid;
			}
		}
	}
	for(auto x: S) {
		//if(x->beg > cl->beg) continue;
		if(x->beg < cl->beg) {
			#ifdef DEBUG
			printf("odejmuje wrong %d %lld\n", x->pid, W[x->pid]);
			#endif
			wrong = MOD(wrong - W[x->pid]);
		}
		else {
			#ifdef DEBUG
			printf("odejmuje wrong %d %lld\n", x->pid, good1 *pow(min(x->beg, cl->beg) - 1 - end1));
			#endif
			wrong = MOD(wrong - MOD(good1 * pow(min(x->beg, cl->beg) - 1 - end1)));
		}
	}
	W[cl->pid] = wrong;
	cl->good_vars = MOD(all - wrong);
	#ifdef DEBUG
	printf("Answer: %lld %lld\n", cl->good_vars, wrong);
	#endif

	Q.push_back(cl);
}

void solve() {
	for(int i = 0; i < m; i++) {
		#ifdef DEBUG
		printf("Solve Clause %d\n", i);
		#endif
		solve_clause(C[i], i + 1);
	}
}


string input;
void parse_input();

int main() {
	getline(cin, input);
	n = stoi(input);
	getline(cin, input);

	string::iterator end = remove(input.begin(), input.end(), '(');
	end = remove(input.begin(), end, ')');
	end = remove(input.begin(), end, 'x');
	end = remove(input.begin(), end, ' ');
	input = input.substr(0, end - input.begin());

	parse_input();

	for(int i = 0; i < m; i++) {
		for(int j = i + 1; j < m; j++) {
			if(cross(C[i], C[j]) && inside(C[i], C[j])) {
				if(C[i]->beg < C[j]->beg || C[i]->end > C[j]->end) {
					swap(C[i], C[m - 1]);
					m--;
					i--;
					break;
				}
				else {
					swap(C[j], C[m-1]);
					m--;
					j--;
				}
			}
		}
	}

	sort(C, C + m, [](const Clause *a, const Clause *b) -> bool { 
		if(a->end == b->end) return a->beg < b->beg;
		return a->end < b->end;
	});
	for(int i = 0; i < m; i++) C[i]->pid = i;

	#ifdef DEBUG
	for(int i = 0; i < m; i++) {
		cout << C[i]->beg << " " << C[i]->end << " ";
		for(auto x: C[i]->its) cout << x;
		cout << "\n";
	}
	for(int i = 0; i < m; i++) {
		for(int j = i + 1; j < m; j++) {
			cout << "cross " << i << "x" << j << ": " << cross(C[i], C[j]) << " " << cross(C[j], C[i]) << "\n"; 
		}
	}
	#endif

	solve();
	printf("%lld\n", MOD(Q.back()->good_vars * pow(n - Q.back()->end)));
}

void parse_literal(set<pair<int, bool>> &S, string lit) {
	#ifdef DEBUG_PARSE
	cout << "\tLiteral: " << lit << "\n";
	#endif

	if(lit[0] == '~')  {
		lit.erase(0, 1);	
		S.insert(make_pair(stoi(lit), 0));
	}
	else
		S.insert(make_pair(stoi(lit), 1));
}

void parse_clause(int b, int e) {
	set<pair<int,bool>> S;

	string cl = input.substr(b, e - b);
	#ifdef DEBUG_PARSE
	cout << "Clause: " << cl << "\n";
	#endif
	int pos = 0, nxt;
	while((nxt = cl.find('v', pos)) != string::npos) {
		parse_literal(S, cl.substr(pos, nxt - pos));
		pos = nxt + 1;
	}
	parse_literal(S, cl.substr(pos));

	#ifdef DEBUG_PARSE
	for(auto x: S) cout << x.first << "," << x.second << " ";
	cout << "\n";
	#endif

	__C[m].beg = S.begin()->first;
	__C[m].end = S.rbegin()->first;
	for(auto &x: S) __C[m].its.push_back(x.second);
	C[m] = &__C[m];
	m++;
}

void parse_input() {
	int pos = 0, nxt;
	#ifdef DEBUG_PARSE
	cout << input << "\n";
	#endif
	while((nxt = input.find('^', pos)) != string::npos) {
		parse_clause(pos, nxt);
		pos = nxt + 1;
	}
	parse_clause(pos, input.size() - pos);
}