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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
#include <stdio.h>
#include <cstdio>
#include <string>
#include <algorithm>
#include <set>
#include <vector>
#include <iterator>
#include <string.h>

using namespace std;

#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
#define FOREACH(t, i,c) for(t::iterator i=(c).begin();i!=(c).end();++i)
#define ZERO(a) memset(a,0,sizeof(a))
template<class T> inline int size(const T &c) { return c.size(); }

#define LL unsigned long long
#define MAXN 1000010
#define MOD 1000000007

#define DBG(X)
//#define K3_DEBUG
#ifndef _MSC_VER
#define gets_s(a,b) gets(a)
#endif

LL powersOf2Mod[1000010];
class Clause;
class CNFFormula;
vector<Clause*> input_clauses;
vector<CNFFormula*> spojne_formuly;
LL liczba_wszystkich_literalow;

LL computePowersOf2() {
	powersOf2Mod[0] = 1;
	for (int i = 1; i < MAXN; i++) {
		powersOf2Mod[i] = (powersOf2Mod[i - 1] * 2) % MOD;
	}
	return 0;
}

bool cmp_absolute(const int &a, const int &b) {
	return abs(a) < abs(b);
}

class Clause {
public:
	int start;
	int len;
	int end;
	LL *bits;
	int bits_len64;

	bool invalid;

	Clause(vector<int> &raw_literals) {
		std::sort(raw_literals.begin(), raw_literals.end(), cmp_absolute);

		start = abs(raw_literals[0]);
		len = abs(raw_literals[raw_literals.size() - 1]) - start + 1;
		end = start + len - 1;
		bits_len64 = (len + 63) / 64;
		bits = new LL[bits_len64];
		for (int i = 0; i < bits_len64; i++) {
			bits[i] = 0;
		}
		for (int i = 0; i < raw_literals.size(); i++) {
			int lit = raw_literals[i];
			if (lit > 0) {
				set_var(i + start);
				if (!get_var(i + start) == 1) {
					DBG(printf("error!"));
				}
			}
			else {
				clear_var(i + start);
			}
		}
		invalid = false;
	}

	Clause(const Clause& c) {
		//printf("copy constructor");
		start = c.start;
		len = c.len;
		end = c.end;
		bits_len64 = c.bits_len64;
		bits = new LL[bits_len64];
		memcpy(bits, c.bits, c.bits_len64);
		invalid = c.invalid;
	}

	// Joined Clause
	Clause(Clause *a, Clause* b) {
		if (b->start > a->end) {
			DBG(printf("Wrong joined Clause()\n"));
			//throw "Wrong joined Clause()";
		}
		start = a->start;
		end = b->end;
		len = end - start + 1;
		bits_len64 = (len + 63) / 64;
		bits = new LL[bits_len64];
		memcpy(bits, a->bits, a->bits_len64);
		for (int i = a->bits_len64; i < bits_len64; i++) {
			bits[i] = 0;
		}
		for (int v = b->start; v <= b->end; v++) {
			if (b->get_var(v)) {
				set_var(v);
			}
		}
		invalid = false;
	}

	void print() const {
		printf("Clause: start=%d end=%d len=%d inv[%d]: ", start, end, len, invalid);
		for (int i = start; i <= end; i++) {
			printf("%d ", get_var(i));
		}
		printf("\n");
	}

	void selftest() {

		for (int i = 0; i < 64; i++) {
			printf("%d ", get_var(i));
			set_var(i);
			printf("%d ", get_var(i));
			clear_var(i);
			printf("%d\n", get_var(i));
		}

	}

	inline int get_var(int idx) const {
		idx -= start;
		return ((bits[idx >> 6] >> (idx & 0x3F)) & 1ULL);
	}

	inline void set_var(int idx) {
		idx -= start;
		bits[idx >> 6] |= (1ULL << (idx & 0x3F));
	}

	inline void clear_var(int idx) {
		idx -= start;
		bits[idx >> 6] &= ~(1ULL << (idx & 0x3F));
	}

	inline void flip_var(int idx) {
		idx -= start;
		bits[idx >> 6] ^= (1ULL << (idx & 0x3F));
	}

	// TODO:
	inline int get_var_range(int idx_s, int idx_e) {
		idx_s -= start;
		idx_e -= end;
	}

	bool operator<(Clause* other) {
		if (this->start == other->start) {
			return this->end < other->end;
		}
		return this->start < other->start;
	}

	bool operator<(Clause& other) {
		if (this->start == other.start) {
			return this->end < other.end;
		}
		return this->start < other.start;
	}

	bool operator==(Clause& other) {
		if (this->start != other.start) {
			return false;
		}
		if (this->len != other.len) {
			return false;
		}
		for (int i = 0; i < bits_len64; i++) {
			if (bits[i] != other.bits[i]) return false;
		}
		return true;
	}

	bool disjoint(const Clause* a, const Clause *b) {
		return (a->end < b->start) || (b->end < a->start);
	}

	bool inside(const Clause *b) {
		if (this->start >= b->start && this->end <= b->end) {
			for (int i = this->start; i <= this->end; i++) {
				if (get_var(i) != b->get_var(i)) {
					return false;
				}
			}
			return true;
		}

		return false;
	}

	bool operator^(const Clause& other) {
		return operator^(&other);
	}

	// Independent operator
	bool operator^(const Clause* other) {

		if (disjoint(this, other)) {
			return true;
			//throw "Do preprocessing to avoid disjoint clauses";
		}
		// so they have common part;
		int s = max(this->start, other->start);
		int e = min(this->end, other->end);
		for (int i=s; i <= e; i++) {
			// TODO: use bitmask
			if (this->get_var(i) == !other->get_var(i)) {
				return true;
			}
		}
		return false;
	}

	int operator[](std::size_t idx) { return get_var(idx); }
	const int operator[](std::size_t idx) const { get_var(idx); }

	// non-copy-and-swap assignment
	Clause& operator=(const Clause& other)
	{
		//DBG(printf("non-copy-and-swap assignment\n"));
		// check for self-assignment
		if (&other == this)
			return *this;
		// reuse storage when possible
		if (bits_len64 < other.bits_len64)
		{
			bits_len64 = other.bits_len64;
			bits = new LL[other.bits_len64];
		}
		start = other.start;
		end = other.end;
		len = other.len;
		invalid = other.invalid;
		memcpy(bits, other.bits, bits_len64);

		return *this;
	}

	static Clause* create(char* s, int parsed_len) {
		char * p = s;
		char* end = p + parsed_len;
		vector<int> literals;
		while (p < end) {
			int sign = 1;
			while (p < end) {
				if (*p == 'x' || *p == '~') {
					break;
				}
				p++;
			}
			if (p == end) {
				break;
			}
			if (*p == '~') {
				sign = -1;
				p++;
			}
			int xi;
			int offset = sscanf(p, "%*c%d", &xi);
			literals.push_back(xi * sign);
			p += offset;
		}
		return new Clause(literals);
	}
};

bool operator<(const Clause& a, const Clause& other) {
	return a.start < other.start;
}

bool cmp_clause_ptr(const Clause* a, const Clause* b) {
	if (a->start == b->start) {
		return a->end < b->end;
	}
	return a->start < b->start;
}

bool cmp_clause_min_start(const Clause& a, const Clause& b) {
	if (a.start == b.start) {
		return a.end < b.end;
	}
	return a.start < b.start;
}

bool cmp_min_len(const Clause& a, const Clause& b) {
	if (a.len == b.len) {
		return a.start < b.start;
	}
	return a.len < b.len;
}
//struct APtrComp
//{
//	bool operator()(const A* lhs, const A* rhs) const { /* implement logic here */ }
//};

vector<Clause> convert_to_independent(Clause &c1, Clause &c2) {
	vector<Clause> res;
	if (c1 ^ c2) {
		res.push_back(c1);
		res.push_back(c2);
	}
	else {
		// sa zalezne
		// sprawdz czy jedna nie zawiera sie w drugiej
		if (c1.inside(&c2)) {
			res.push_back(c1);
			#ifdef K3_DEBUG
			printf("C1 is inside C2\n");
			c1.print();
			c2.print();
#endif
			return res;
		}
		else if (c2.inside(&c1)) {
#ifdef K3_DEBUG
			res.push_back(c2);
			printf("C2 is inside C1\n");
			c2.print();
			c1.print();
#endif
			return res;
		}
		vector<int> L;
		if (!(c1 < c2))
			swap(c1, c2);

		// Zbior L to znaki pomiedzy c1.end+1 a c2.end
		Clause joined_c(&c1, &c2);
		res.push_back(c2);

		DBG(printf("Joining clauses:\n"));
		DBG(c1.print());
		DBG(c2.print());
		DBG(printf("Start...\n"));
		for (int v = c1.end + 1; v <= c2.end; v++) {
			Clause c(joined_c);
			c.end = v;
			c.len = c.end - c.start + 1;
			c.flip_var(v);
			res.push_back(c);
			DBG(c.print());
		}
	}
	DBG(printf("End of make independent\n"));
	return res;
}

/*
Formula musi byc spojna!!!
*/
class CNFFormula {
public:
	int number_of_variables; // number of variables xi
	int start;
	int end;
	vector<Clause> clauses;
	int formula_id;

	CNFFormula(int id) {
		formula_id = id;
	}

	void addClause(Clause clause) {
		if (clauses.empty()) {
			start = clause.start;
			end = clause.end;
		}
		if (clause.start < start) {
			throw "Wrong order while adding clauses to CNFFormula";
		}
		clauses.push_back(clause);
		if (clause.end > end) {
			end = clause.end;
		}
		number_of_variables = end - start + 1;
	}

	void print() {
		printf("Formula %d:\n", formula_id);
		for (int i = 0; i < clauses.size(); i++) {
			clauses[i].print();
		}
	}

	/*
		Number of solutions if clauses are mutually independent
	*/
	LL spojnyNBS() {
		LL res = powersOf2Mod[number_of_variables];
		for (int i = 0; i < clauses.size(); i++) {
			res -= powersOf2Mod[number_of_variables - clauses[i].len];
			res += MOD;
			res %= MOD;
		}
		return res;
	}

	LL niespojnyNBS() {
		if (clauses.size() == 0) {
			return powersOf2Mod[number_of_variables];
		}
		int ci = 0;
		LL res = 1;
		std::sort(clauses.begin(), clauses.end(), cmp_clause_min_start);
		int uzyte_zmienne = 0;
		while (ci < clauses.size()) {
			Clause *c = &clauses[ci];
			int i = ci + 1;
			int last_end = c->end;
			while (i < clauses.size() && clauses[i].start <= last_end) {
				c = &clauses[i];
				i++;
				last_end = max(last_end, c->end);
			}
			DBG(printf("spojny [%d,%d]\n", clauses[ci].start, last_end));
			int v_cnt = last_end - clauses[ci].start + 1;
			LL local_res = powersOf2Mod[v_cnt];
			uzyte_zmienne += v_cnt;
			for (int j = ci; j < i; j++) {
				local_res -= powersOf2Mod[v_cnt - clauses[j].len];
				local_res += MOD;
				local_res %= MOD;
			}
			res *= local_res;
			ci = i;
		}
		res *= powersOf2Mod[number_of_variables - uzyte_zmienne];
		res %= MOD;
		return res;
	}
};

vector<Clause> remove_insiders(vector<Clause>& clauses) {
	int len = clauses.size();
	sort(clauses.begin(), clauses.end(), cmp_min_len);
	bool *invalid = new bool[len];
	for (int i = 0; i < len; i++) {
		invalid[i] = false;
	}
	vector<Clause> res;
	for (int i = 0; i < len; i++) {
		if (invalid[i]|| clauses[i].invalid) {
			continue;
		}
		for (int j = i + 1; j < len; j++) {
			if (invalid[j] || clauses[j].invalid) {
				continue;
			}
			if (clauses[i].inside(&clauses[j])) {
				DBG(printf("Clause %d is inside clause %j. Removing:\n"));
				DBG(clauses[i].print());
				DBG(clauses[j].print());
				invalid[j] = true;
			}
		}
		if (!invalid[i] && !clauses[i].invalid) {
			res.push_back(clauses[i]);
		}
	}
	delete[] invalid;
	return res;
}

vector<Clause> transformToIndependent(vector<Clause> cs) {
	if (cs.size() == 1) {
		return cs;
	}

	if (cs.size() == 2) {
		return convert_to_independent(cs[0], cs[1]);
	}

	bool done = true;
	vector<pair<int, int> > dependent;
	cs = remove_insiders(cs);
	DBG(printf("After first removal of insiders:\n"));
#ifdef K3_DEBUG
	for (int i = 0; i < cs.size(); i++) {
		cs[i].print();
	}
#endif
	for (int i = 0; i < cs.size(); i++) {
		for (int j = i + 1; j < cs.size(); j++) {
			if (!(cs[i] ^ cs[j])) {
#ifdef K3_DEBUG
				printf("First pass: %d and %d are still DEPENDENT!\n", i, j);
				cs[i].print();
				cs[j].print();
#endif
				done = false;
				dependent.push_back(make_pair(i, j));
			}
		}
	}
	do {
		done = true;
#ifdef K3_DEBUG
		DBG(printf("Loop start:\n"));
		for (int i = 0; i < cs.size(); i++) {
			cs[i].print();
		}
#endif
		vector<Clause> add_res;

		for (int i = 0; i < dependent.size(); i++) {
			int a = dependent[i].first;
			int b = dependent[i].second;
			if (cs[a].invalid || cs[b].invalid) {
				continue;
			}
			Clause ca = cs[a];
			Clause cb = cs[b];
#ifdef K3_DEBUG
			printf("Making these two clauses independent:\n");
			ca.print();
			cb.print();
#endif
			vector<Clause> r = convert_to_independent(cs[a], cs[b]);
			cs[a].invalid = true;
			cs[b].invalid = true;
#ifdef K3_DEBUG
			printf("These two marked as invalid:\n");
			cs[a].print();
			cs[b].print();
#endif
			for (int i = 0; i < r.size(); i++) {
				add_res.push_back(r[i]);
			}
#ifdef K3_DEBUG
			printf("Results:\n");
			for (int i = 0; i < r.size(); i++) {
				r[i].print();
			}
			printf("end of Results\n");
#endif
		}

		for (int i = 0; i < add_res.size(); i++) {
			cs.push_back(add_res[i]);
		}
#ifdef K3_DEBUG
		printf("All clauses before removing insiders:\n");
		for (int i = 0; i < cs.size(); i++) {
			cs[i].print();
		}
#endif
		cs = remove_insiders(cs);
		dependent.clear();
		for (int i = 0; i < cs.size(); i++) {
			for (int j = i + 1; j < cs.size(); j++) {
				if (!(cs[i] ^ cs[j])) {
#ifdef K3_DEBUG
					printf("%d and %d are still DEPENDENT!\n", i, j);
					cs[i].print();
					cs[j].print();
					done = false;
#endif
					dependent.push_back(make_pair(i, j));
				}
			}
		}

	} while (dependent.size() > 0);

	return cs;
}

void transformToIndependentClauses(CNFFormula& formula) {
	vector<Clause> res = transformToIndependent(formula.clauses);
	// TODO: spojne again?
	formula.clauses = res;
#ifdef K3_DEBUG
	printf("Final clauses for formula %d\n", formula.formula_id);
	for (int i = 0; i < res.size(); i++) {
		res[i].print();
	}
#endif
}

// Algorythm:
// Najpierw podziel na spojne skladowe
// 1. Posortuj po koncach
// jesli clause[i].end < clause[i+1].begin -> rozpocznij nowa skladowa (nowa formule)
// policz zmiennych ktorych w ogole nie ma w rownaniu
// dla kazdej skladowej policz niezalezne formuly
// wynik = NBS(f1) * NBS(f2) * 2 ** (liczba zmiennych ktore nie wystepuja)


int main() {
	computePowersOf2();

	int n;
	scanf("%d", &n);
	liczba_wszystkich_literalow = n;
	const int MAX_BUF = 20000000;
	char *buf = new char[MAX_BUF];
	gets_s(buf, MAX_BUF);// only newline
	gets_s(buf, MAX_BUF);
	char* p = buf;
	char* end = buf + strlen(buf);
	while (p < end) {
		char* begin = p;
		while (*p != ')') {
			p++;
		}
		// *p == ')'
		*p = 0;
		Clause *c = Clause::create(begin + 1, p - begin);
		DBG(c->print());
		p++;
		while (p < end && *p != '(')
			p++;
		input_clauses.push_back(c);
	}

	std::sort(input_clauses.begin(), input_clauses.end(), cmp_clause_ptr);
	int formula_id = 0;
	CNFFormula *f = new CNFFormula(formula_id++);
	Clause *last_clause = input_clauses[0];
	//last_clause->selftest();
	int last_end = last_clause->end;
	f->addClause(*last_clause);
	for (int i = 1; i < input_clauses.size(); i++) {
		Clause *c = input_clauses[i];
		if (last_clause->inside(c)) {
			continue;
		}
		if (c->start > last_end) {
			spojne_formuly.push_back(f);
			f = new CNFFormula(formula_id++);
		}
		f->addClause(*c);
		last_clause = c;
		last_end = max(last_end, last_clause->end);
	}
	spojne_formuly.push_back(f);
	int uzyte_zmienne = 0;
	for (int i = 0; i < spojne_formuly.size(); i++) {
		DBG(spojne_formuly[i]->print());
		uzyte_zmienne += spojne_formuly[i]->number_of_variables;
	}

	LL res = 1;
	for (int i = 0; i < spojne_formuly.size(); i++) {
		DBG(printf("Transform to independent formula %d\n", i));
		transformToIndependentClauses(*spojne_formuly[i]);
		res *= spojne_formuly[i]->niespojnyNBS();
		res %= MOD;
	}
	res *= powersOf2Mod[liczba_wszystkich_literalow - uzyte_zmienne];
	res %= MOD;
	printf("%llu\n", res);
	return 0;
}

/*
Test spojne formuly:
21
(x1 v ~x2 v x3) ^ (x1 v x2) ^ (x3) ^ (~x4 v x5) ^ (x20 v x21) ^ (x10 v x11 v x12) ^ (x11 v x12)

4
(x1 v ~x2) ^ (x2 v x3 v x4) ^ (x1 v ~x3) ^ (~x3 v x4)

8
(x1 v x2 v x3 v x4 v x5) ^ (x5 v x6 v x7) ^ (x1 v x2 v x3 v x4 v x5 ^ ~x6) ^ (x1 v x2 v x3 v x4 v x5 ^ x6 ^ ~x7) ^ (x5 v ~x6 v x7 v x8)

3 2
3 8 3
2 5 2
3 7 3


*/