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
// Solution to 5CNF
// Author: Michal Czuczman <michal.czuczman@gmail.com>
// created on Sat Nov 26 22:48:24 CET 2016

#include <algorithm>
#include <assert.h>
#include <climits>
#include <iostream>
#include <vector>

static const int CNF_MOD = 1000000007;

using namespace std;

typedef unsigned long long ull;

struct ModuloPower2 {
	ull mod;
	vector<ull> cache;
	explicit ModuloPower2(ull mod_) : mod(mod_) {}
	void init_cache(int max) {
		cache.resize(max + 1);
	}
	ull pow(int exp) {
		if(exp >= (int)cache.size()) {
			cache.resize(exp+1);
		}
		if(cache[exp] == 0) {
			if(exp == 0) {
				cache[exp] = 1;
			} else {
				ull res = pow(exp >> 1);
				res = (res * res) % mod;
				if(exp & 1) {
					res = (res << 1) % mod;
				}
				cache[exp] = res;
			}
		}
		return cache[exp];
	}
};

ModuloPower2 pow2mod(CNF_MOD);

struct Clause {
	int first_index;
	vector<int> values; // 1=positive, 0=negative

	bool var_equals(int index, bool value) {
		if(index < first_index
				|| index >= first_index + (int)values.size()) {
			return false;
		} else {
			return values[index - first_index] == value;
		}
	}
	char get_char(int index) const {
		if(index < first_index
				|| index >= first_index + (int)values.size()) {
			return 'X';
		} else if(values[index - first_index]) {
			return '1';
		} else {
			return '0';
		}
	}
};

bool operator<(const Clause& c1, const Clause& c2) {
	return c1.first_index < c2.first_index
		|| (c1.first_index == c2.first_index
			&& c1.values.size() < c2.values.size());
}

struct Expression {
	int num_vars;
	vector<Clause*> clauses;

	Expression() {}
	Expression(const Expression *src, bool last_value)
		:num_vars(src->num_vars - 1)
	{
		assert(num_vars > 0);
		for(Clause* c : src->clauses) {
			if(!c->var_equals(num_vars, last_value)) {
				clauses.push_back(c);
			}
		}
	}

	int num_satisfied() {
#ifdef MY_DEBUG
		dump();
		cerr << ": ";
#endif
		if(clauses.empty()) {
			int n = pow2mod.pow(num_vars);
#ifdef MY_DEBUG
			cerr << "2**" << num_vars << " = " << n << "\n";
#endif
			return n;
		} else if(clauses.back()->first_index >= num_vars) {
#ifdef MY_DEBUG
			cerr << "all-X 0\n";
#endif
			return 0;
		} else if(num_vars == 1) {
			char ch = 0;
			for(Clause* c : clauses) {
				char p = c->get_char(0);
				if(p == 'X') {
#ifdef MY_DEBUG
					cerr << "(?) 0\n";
#endif
					return 0;
				} else if(ch == 0) {
					ch = p;
				} else if(ch != p) {
#ifdef MY_DEBUG
					cerr << "(x^~x) 0\n";
#endif
					return 0;
				}
			}
#ifdef MY_DEBUG
			cerr << "1\n";
#endif
			return 1;
		} else {
#ifdef MY_DEBUG
			cerr << " split:\n";
#endif
			int n0;
			{
				Expression e0(this, false);
				n0 = e0.num_satisfied();
			}
			int n1;
			{
				Expression e1(this, true);
				vector<Clause*>().swap(clauses);
				n1 = e1.num_satisfied();
			}
			int n = (n0 + n1) % CNF_MOD;
#ifdef MY_DEBUG
			cerr << "--> " << n << "\n";
#endif
			return n;
		}
	}

#ifdef MY_DEBUG
	void dump() const {
		cerr << "N" << num_vars << "(";
		int count = 0;
		for(Clause* c : clauses) {
			if(++count > 1) {
				cerr << ", ";
			}
			for(int var = 0; var < num_vars; ++var) {
				cerr << c->get_char(var);
			}
		}
		cerr << ")";
	}
#endif
};

void read_clause(istream& is, Clause& c, vector<int>& buf) {
	int min = INT_MAX;
	int max = 0;
	buf.clear();
	char ch;
	cin >> ch;
	assert(ch == '(');
	while(1) {
		cin >> ch;
		if(ch == 'v') {
			continue;
		} else if(ch == ')') {
			break;
		}
		int index;
		if(ch == '~') {
			cin >> ch;
			assert(ch == 'x');
			cin >> index;
		} else {
			cin >> index;
			buf.push_back(index); // store in buf only positive
		}

		if(index < min) { min = index; }
		if(index > max) { max = index; }
	}

	c.first_index = min - 1;
	c.values.resize(max - min + 1);
	for(int i = buf.size() - 1; i >= 0; --i) {
		c.values[buf[i] - min] = 1;
	}
}

void read_expression(istream& is, Expression &e, vector<Clause>& clauses) {
	cin >> e.num_vars;
	pow2mod.init_cache(e.num_vars + 1);
	vector<int> buf;
	buf.reserve(e.num_vars);
	Clause c;
	read_clause(is, c, buf);
	clauses.push_back(c);
	while(1) {
		char ch;
		cin >> ch;
		if(!cin) {
			break;
		}
		assert(ch == '^');
		Clause d;
		read_clause(is, d, buf);
		clauses.push_back(d);
	}
	sort(&clauses[0], &clauses[clauses.size()]);
	e.clauses.reserve(clauses.size());
	for(Clause& cl : clauses) {
		e.clauses.push_back(&cl);
	}
}

int main()
{
	ios::sync_with_stdio(false);

	vector<Clause> clauses;
	Expression e;
	read_expression(cin, e, clauses);
	cout << e.num_satisfied() << '\n';
}