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
#pragma warning(disable:4996)

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

#define MODULO 1000000007LL

struct Clause {
	int startPos;
	vector<bool> values;

	bool operator<(const Clause& other) const {
		if (startPos == other.startPos) {
			if (values.size() == other.values.size()) {
				return values < other.values;
			}
			return values.size() < other.values.size();
		}
		return startPos < other.startPos;
	}

	bool inRange(int pos) const {
		return (pos >= startPos) && (pos < startPos + (int)values.size());
	}

	bool getValue(int pos) const {
		return values[pos - startPos];
	}
};

inline void readFormula(const int n, vector<Clause>& formula)
{
/* test input
3
(x2) ^ (x3 v ~x2) ^ (x2 v x1 v ~x3)

*/
	char c;

	vector<bool> clause(n + 1);
	int firstIndex = 0;
	int lastIndex = 0;
	int pos;
	bool value;
	while (getchar() != '\n');  //skip first EOL
	while ((c = getchar()) != EOF && c != '\n') {
		value = true;
		switch (c) {

		case ')': {
			if (firstIndex != lastIndex) {
				Clause newClause;
				newClause.startPos = firstIndex;
				newClause.values.assign(clause.begin() + firstIndex, clause.begin() + lastIndex);
				formula.push_back(newClause);
				firstIndex = lastIndex = 0;
			}
			break;
		}
		case '~':
			value = false;
			getchar(); //skip x
			//no break
		case 'x': {
			scanf("%d", &pos);
			if (firstIndex == lastIndex) {
				firstIndex = pos;
				lastIndex = pos + 1;
			}
			else if (firstIndex > pos) {
				firstIndex = pos;
			}
			else if (lastIndex <= pos) {
				lastIndex = pos + 1;
			}
			clause[pos] = value;
			break;
		}
		default:
			break;
		}
	}
	sort(formula.begin(), formula.end());
}

bool clauseMatches(const vector<Clause>& formula, const vector<int>& stack, int candidate, int range, long long& result)
{
	int cPos = 0;
	int bitCnt = 0;
	int currentPos = stack.size() > 0 ? min(formula[stack[0]].startPos, formula[candidate].startPos) : formula[candidate].startPos;
	vector<int>::const_iterator it = stack.begin();
	while (it != stack.end()) {
		if (formula[*it].inRange(currentPos)) {
			if (formula[candidate].inRange(currentPos)) {
				if (formula[*it].getValue(currentPos) != formula[candidate].getValue(currentPos)) {
					return false;
				}
			}
			bitCnt++;
			currentPos++;
		}
		else if (formula[*it].startPos > currentPos) {
			if (formula[candidate].inRange(currentPos)) {
				bitCnt++;
			}
			currentPos++;
		}
		else {
			++it;
		}
	}

	if (formula[candidate].startPos > currentPos) {
		bitCnt += formula[candidate].values.size();
	}
	else if (formula[candidate].inRange(currentPos)) {
		bitCnt += formula[candidate].startPos + formula[candidate].values.size() - currentPos;
	}

	// assert bitCnt < range
	result = 1;
	while (bitCnt < range) {
		result = (result * 2) % MODULO;
		bitCnt++;
	}

	return true;
}

inline long long calculateRegion(int start, int end, const vector<Clause>& formula)
{
	long long localResult = 1;
	for (int i = start; i < end; ++i) {
		localResult = (localResult * 2) % MODULO;
	}

	vector<int> stack;
	int pos = start;
	while (!stack.empty() || pos < end) {
		int i;
		for (i = pos; i < end; ++i) {
			long long current = 0;
			if (clauseMatches(formula, stack, pos, end-start, current)) {
				stack.push_back(i);
				pos = i + 1;
				localResult = (localResult + (current * ((stack.size() % 2) ? -1 : 1))) % MODULO;
				break;
			}
		}
		if (i == end) {
			pos = stack.back() + 1;
			stack.pop_back();
		}
	}

	return localResult;
}

int main()
{
	int n;
	scanf("%d", &n);
	vector<Clause> formula;
	readFormula(n, formula);

	long long result = 1;
	int i = 0;
	int lastPos = 1;
	int firstPos;
	while (result != 0 && i < formula.size()) {
		int start = i;
		firstPos = formula[start].startPos;
		while (lastPos < firstPos) {
			result = (result * 2LL) % MODULO;
		}
		lastPos = formula[start].startPos + formula[start].values.size();
		++i;
		while (i < formula.size() && formula[i].startPos < lastPos) {
			lastPos = max(lastPos, formula[i].startPos + (int)formula[i].values.size());
			++i;
		}
		result = (result * calculateRegion(start, i, formula)) % MODULO;
	}

	while (lastPos <= n) {
		result = (result * 2LL) % MODULO;
	}

	printf("%lld\n", result);

	return 0;
}