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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class cnf {

	public static class Clause {
		
		private BitSet literals;
		private int clauseOffset;
		private int maskSize;
		
		
		public Clause(int literalCount) {
			this.literals = new BitSet(literalCount);
			this.clauseOffset = Integer.MAX_VALUE;
			this.maskSize = 0;
		}

		public BitSet getLiterals() {
			return literals;
		}
		
		public int getClauseOffset() {
			return clauseOffset;
		}

		public int getMaskSize() {
			return maskSize;
		}
		
		public void setMaskBit(int maskIndex){
			clauseOffset = Math.min(clauseOffset, maskIndex);
			maskSize++;
		}
		
		public void truncateClause(){
			this.literals = literals.get(clauseOffset, clauseOffset + maskSize);
		}

		@Override
		public String toString() {
			return literals + ", " + clauseOffset + ", " + maskSize;
		}
	}
	
	public static class MaskComparator implements Comparator<Clause> {

		public int compare(Clause c0, Clause c1) {
			int result = Integer.compare(c0.getMaskSize(), c1.getMaskSize());
			result = result == 0 ? Integer.compare(c0.getClauseOffset(), c1.getClauseOffset()) : result;
			return result;
		}
		
	}
	/*
	public static class Pair implements Comparable<Pair>, Comparator<Pair> {
		
		private int key;
		private int value;
		
		public Pair(int key, int value) {
			this.key = key;
			this.value = value;
		}

		public int getKey() {
			return key;
		}

		public int getValue() {
			return value;
		}

		@Override
		public int compareTo(Pair pair) {
			return Integer.compare(this.key, pair.key);
		}

		@Override
		public String toString() {
			return "[" + key + ", " + value + "]";
		}

		@Override
		public int compare(Pair p0, Pair p1) {
			return Integer.compare(p0.key, p1.key);
		}
		
	}
	*/
	
	public static void main(String[] args) {
		try {
			BufferedReader buffReader = new BufferedReader(new InputStreamReader(System.in));
			StringTokenizer tokenzier = new StringTokenizer(buffReader.readLine());
			
			int n = Integer.parseInt(tokenzier.nextToken());
			LinkedList<Clause> clausesList = new LinkedList<Clause>();
			
			Clause newClause = null;
			tokenzier = new StringTokenizer(buffReader.readLine());
			while(tokenzier.hasMoreTokens()){
				String token = tokenzier.nextToken();
				char symbols[] = token.toCharArray();
				if(symbols[0] == '('){
					newClause = new Clause(n);
					if(symbols[1] == 'x'){
						if(symbols[symbols.length-1] == ')'){
							 int index = Integer.parseInt(new String(symbols, 2, symbols.length - 3));
							newClause.setMaskBit(index - 1);
							newClause.truncateClause();
							clausesList.add(newClause);
						}else {
							int index = Integer.parseInt(new String(symbols, 2, symbols.length - 2));
							newClause.setMaskBit(index - 1);
						}
					}else if(symbols[1] == '~'){
						if(symbols[symbols.length-1] == ')'){
							int index = Integer.parseInt(new String(symbols, 3, symbols.length - 4));
							newClause.getLiterals().set(index - 1);
							newClause.setMaskBit(index - 1);
							newClause.truncateClause();
							clausesList.add(newClause);
						}else {
							int index = Integer.parseInt(new String(symbols, 3, symbols.length - 3));
							newClause.getLiterals().set(index - 1);
							newClause.setMaskBit(index - 1);
						}
					}
				}else if(symbols[0] == 'x'){
					if(symbols[symbols.length-1] == ')'){
						int index = Integer.parseInt(new String(symbols, 1, symbols.length - 2));
						newClause.setMaskBit(index - 1);
						newClause.truncateClause();
						clausesList.add(newClause);
					}else {
						int index = Integer.parseInt(new String(symbols, 1, symbols.length - 1));
						newClause.setMaskBit(index - 1);
					}
				}else if(symbols[0] == '~'){
					if(symbols[symbols.length-1] == ')'){
						int index = Integer.parseInt(new String(symbols, 2, symbols.length - 3));
						newClause.getLiterals().set(index - 1);
						newClause.setMaskBit(index - 1);
						newClause.truncateClause();
						clausesList.add(newClause);
					}else {
						int index = Integer.parseInt(new String(symbols, 2, symbols.length - 2));
						newClause.getLiterals().set(index - 1);
						newClause.setMaskBit(index - 1);
					}
				}
			}
			
			clausesList.sort(new MaskComparator());
			
			LinkedList<Clause> compactdClauses = new LinkedList<Clause>();
			
			
			while(!clausesList.isEmpty()){
				Clause currentClause = clausesList.pollFirst();
				Iterator<Clause> clausesIterDesc = clausesList.descendingIterator();
				while(clausesIterDesc.hasNext()){
					Clause lastClause = clausesIterDesc.next();
					int currentStart = currentClause.getClauseOffset();
					int currentEnd = currentClause.getClauseOffset() + currentClause.getMaskSize();
					int lastStart = lastClause.getClauseOffset();
					int lastEnd = lastClause.getClauseOffset() + lastClause.getMaskSize();
					/*
					System.out.println(currentClause);
					System.out.println(lastClause);
					*/
					if(currentStart >= lastStart && currentEnd <= lastEnd){
						BitSet comparedBitSet = lastClause.getLiterals().get(currentStart - lastStart, currentEnd - lastStart);
						if(comparedBitSet.equals(currentClause.getLiterals()))
							clausesIterDesc.remove();
					}/*
					else if(currentStart < lastEnd && lastStart < currentStart){
						BitSet comparedBitSetCurrent = currentClause.getLiterals().get(0, lastEnd - currentStart);
						BitSet comparedBitSetLast = lastClause.getLiterals().get(currentStart - lastStart, lastClause.getMaskSize());
						//System.out.println(comparedBitSetCurrent.equals(comparedBitSetLast));
					}else if(currentEnd > lastStart && lastEnd > currentEnd){
						BitSet comparedBitSetCurrent = currentClause.getLiterals().get(lastStart - currentStart, currentClause.getMaskSize());
						BitSet comparedBitSetLast = lastClause.getLiterals().get(0, currentEnd - lastStart);
						//System.out.println(comparedBitSetCurrent.equals(comparedBitSetLast));
					}
					*/
					
				}
				compactdClauses.add(currentClause);
			}
			
			clausesList = compactdClauses;
			
			int[] bitCountTab = new int[32];
			
			for(Clause clause : clausesList){
				if(n - clause.getMaskSize() < bitCountTab.length)
					bitCountTab[n - clause.getMaskSize()]++;
			}
				
			
			while(!clausesList.isEmpty()){
				Clause currentClause = clausesList.pollFirst();
				Iterator<Clause> clausesIterDesc = clausesList.descendingIterator();
				while(clausesIterDesc.hasNext()){
					Clause lastClause = clausesIterDesc.next();
					int currentStart = currentClause.getClauseOffset();
					int currentEnd = currentClause.getClauseOffset() + currentClause.getMaskSize();
					int lastStart = lastClause.getClauseOffset();
					int lastEnd = lastClause.getClauseOffset() + lastClause.getMaskSize();

					if(currentStart >= lastStart && currentEnd <= lastEnd){

					}
					else if(currentStart < lastEnd && lastStart < currentStart){
						BitSet comparedBitSetCurrent = currentClause.getLiterals().get(0, lastEnd - currentStart);
						BitSet comparedBitSetLast = lastClause.getLiterals().get(currentStart - lastStart, lastClause.getMaskSize());
						if(comparedBitSetCurrent.equals(comparedBitSetLast));
							bitCountTab[n - Math.max(currentClause.maskSize, lastClause.maskSize)]--;
					}else if(currentEnd > lastStart && lastEnd > currentEnd){
						BitSet comparedBitSetCurrent = currentClause.getLiterals().get(lastStart - currentStart, currentClause.getMaskSize());
						BitSet comparedBitSetLast = lastClause.getLiterals().get(0, currentEnd - lastStart);
						if(comparedBitSetCurrent.equals(comparedBitSetLast));
							bitCountTab[n - Math.max(currentClause.maskSize, lastClause.maskSize)]--;
					}
					
					
				}
			}
			
			long result = 1 << Math.min(62, n);
			for(int i = 0; i < bitCountTab.length; i++)
				result -= (1l << i)*(bitCountTab[i]);

			//System.out.println(Arrays.toString(bitCountTab));
			System.out.println(result % 1000000007);
			
			/*
			ArrayList<Clause> clausesArray = new ArrayList<Clause>(clausesList);
			
			// clausesArray.sort(new MaskComparator());
			
			ArrayList<Pair> maskMapping = new ArrayList<Pair>();
			ArrayList<Pair> startPosition = new ArrayList<Pair>();
			ArrayList<Pair> endPosition = new ArrayList<Pair>();
			for(int i = 0; i < clausesArray.size(); i++){
				maskMapping.add(new Pair(clausesArray.get(i).getMaskSize(), i));
				startPosition.add(new Pair(clausesArray.get(i).getClauseOffset(), i));
				endPosition.add(new Pair(-(clausesArray.get(i).getClauseOffset()+clausesArray.get(i).getMaskSize()), i));
			}
			maskMapping.sort(new Pair(0, 0));
			startPosition.sort(new Pair(0, 0));
			endPosition.sort(new Pair(0, 0));
			
			System.out.println(maskMapping);
			System.out.println(startPosition);
			System.out.println(endPosition);
			
			ArrayList<Clause> compactedClauses = new ArrayList<Clause>(clausesArray.size());
			boolean[] compacted = new boolean[clausesArray.size()];
			*/
			
			/*
			while(!maskMapping.isEmpty()){
				int i = maskMapping.pollFirst().getValue();
				for(int j = i + 1; j < clausesArray.size(); j++){
					if(clausesArray.get(j).getClauseOffset() == clausesArray.get(i).getClauseOffset()){
						BitSet comparedBitSet = clausesArray.get(j).getLiterals().get(0, clausesArray.get(i).getMaskSize());
						boolean compactTest = comparedBitSet.equals(clausesArray.get(i).getLiterals());
						if(compactTest)
							compacted[j] = true;
					}else{
						break;
					}
				}
			}
			*/
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}