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
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <cmath>
#include <cstring>
#include <string>
#include <iostream>
#include <complex>
#include <sstream>
using namespace std;
 
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<int,int> PII;
 
#define REP(i,n) for(int i=0;i<(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i)
#define ALL(v) (v).begin(), (v).end()
 
#define pb push_back
#define mp make_pair
#define st first
#define nd second

const int MAXN = (1<<21) + 20;
int _count[1<<21];
void countSort(int* in, int* out, const int* key, int N, int M){
  fill_n(_count, M, 0);
  REP(i,N)++_count[key[in[i]]];
  FOR(i,1,M)_count[i] += _count[i-1];
  FORD(i,N,0)out[--_count[key[in[i]]]] = in[i];
}

int temp[MAXN], s0[MAXN], s12[MAXN],_rank[MAXN], recOut[MAXN];
const int* _s;
inline bool cmp(int u, int v){
  while(true){
    if(_s[u] != _s[v]) return _s[u]<_s[v];
    if((u%3) && (v%3)) return _rank[u] < _rank[v];
    ++u;++v;
  }
}

/*IN: N - długość
  IN: s - string   
  IN: K - zakres alfabetu
  OUT: out - tablica sufiksowa
 !!ZAŁOŻENIA: N>=2, 0<s[i]<K, s[N]=s[N+1]=s[N+2]=0!! */
void suffixArray(const int* s, int N, int* out, int K){
  int n0 = (N+2)/3, n1 = (N+1)/3, n12 = 0; 
  REP(i,N)if(i%3)temp[n12++] = i;

  countSort(temp, s12, s+2, n12, K);
  countSort(s12, temp, s+1, n12, K);
  countSort(temp, s12, s,   n12, K);    

  int recIn[n12+5], cnt = 2;  
  REP(i,n12){
    if(i>0 && !equal(s+s12[i-1], s+s12[i-1]+3, s+s12[i]))++cnt;
    recIn[s12[i]%3==1?s12[i]/3:s12[i]/3+n1+1] = cnt;  
  }  
  
  if(cnt != n12+1){
    REP(i,3) recIn[n12+1+i] = 0;  
    recIn[n1] = 1;    
    suffixArray(recIn, n12+1, recOut, cnt+1);
    FOR(i,1,n12+1)s12[i-1] = recOut[i]<n1? 3*recOut[i]+1 : 3*(recOut[i]-n1)-1;
  }  
  
  REP(i,n12)_rank[s12[i]] = i+1;  
  _rank[N] = 0;

  REP(i,n0)s0[i] = 3*i;
  countSort(s0,temp,_rank+1,n0,n12+2);   
  countSort(temp,s0,s,n0,K);  

  _s = s;
  merge(s12, s12+n12, s0, s0+n0, out, cmp);  
}

/*IN: sA - tablica sufiksowa
  IN: invSA - odwrotność tablicy sufiksowej
  IN: N - długość
  IN: text - string; T[N]!=T[i] dla i<N!!
  OUT: lcp */ 
void computeLCP(const int* sA, const int* invSA, int N, const int* text, int* lcp){
  int cur = 0;
  REP(i,N){
    int j = invSA[i];
    if(!j)continue;
    int k = sA[j-1];
    while(text[k+cur] == text[i+cur])cur++;
    lcp[j] = cur;
    cur = max(0,cur-1);
  }
}

// END: SUFFIX ARRAY

const LL MOD = 1000000007;
LL pow2[1000005];

int VAR_NUM, M;
vector<vector<int> > unprocessed_clauses;

char buf[15];
void read_input() {
    scanf("%d", &VAR_NUM);
    while (true) {
        vector<int> clause;
        bool clause_finished = false;
        while (!clause_finished) {
            scanf("%s", buf);
            int K = strlen(buf);
            if (buf[K-1] == ')') {
                buf[K-1] = 0;
                clause_finished = true;
            }
            
            char* number = buf;
            if (number[0] == '(') ++number;
            int sign = 1;
            if (number[0] == '~') {
                sign = -1;
                ++number;
            }
            ++number;
            int id = atoi(number);
            clause.pb(sign * id);
            
            if (!clause_finished) scanf("%s", buf);
        }

        unprocessed_clauses.pb(clause);
        if (scanf("%s", buf) != 1) break;        
    }    
}

pair<vector<char>, int> clauses[1000005];
vector<int> suffix[1000005];
vector<int> clauses_by_end[1000005];

int S[2000005];
int SA[2000005];
int invSA[2000005];

int _lcp[5000000];
int W;
void init_lcp() {
    FORD(i,W,1) _lcp[i] = min(_lcp[2*i], _lcp[2*i+1]);
}
int INF = 1e7;
int _get_min(int A, int B, int v, int L, int R) {
    if (B < L || R < A) return INF;
    if (A <= L && R <= B) return _lcp[v];
    return min(
        _get_min(A, B, 2*v, L, (L+R)/2),
        _get_min(A, B, 2*v+1, (L+R)/2+1, R)
    );
}
int get_lcp(int s, int e) {
    if (e < s) swap(s, e);
    return _get_min(s+1, e, 1, 0, W-1);
}

LL result[1000005];

void debug() {
    REP(i,M) {
        REP(j, clauses[i].nd) printf(" ");
        for (char a: clauses[i].st) printf("%d", a);
        printf("\n");
    }
    REP(i,M) {
        for (auto s: suffix[i]) printf("%d ", s);
        printf("\n");
    }
    
    map<int, string> suf;
    REP(i,M) {
        REP(j, clauses[i].st.size()) {
            string ss = "";
            FOR(k, j, clauses[i].st.size()) ss += clauses[i].st[k] ? '1' : '0';
            ss += '$';
            suf[suffix[i][j]] = ss;
        }
    }
    for (auto& p: suf) {
        printf("%d %s\n", p.st, p.nd.c_str());
    }
    REP(i,2*W) {
        printf("%d[%d] %d\n", i, W, _lcp[i]);
    }
}

int main() {
    pow2[0] = 1;
    FOR(i,1,1000005) pow2[i] = (pow2[i-1]<<1) % MOD;

    read_input();    
    M = unprocessed_clauses.size();
    REP(i,M) {
        const vector<int>& clause = unprocessed_clauses[i];
        int clause_start = VAR_NUM;
        for (int literal: clause) clause_start = min(clause_start, abs(literal));
        vector<char> sorted_clause(clause.size());
        for (int literal: clause) {
            sorted_clause[abs(literal) - clause_start] = (literal < 0) ? 0 : 1;
        }
        clauses[i] = mp(sorted_clause, clause_start - 1);
    }

    int L = 0;
    REP(i,M) {
        for (char a: clauses[i].st) S[L++] = a + 2;
        S[L++] = 1;
    }
    S[L] = S[L+1] = S[L+2] = 0;
    suffixArray(S, L, SA, 4);
    REP(i,L) invSA[SA[i]] = i;
    L = 0;
    REP(i,M) {
        for (char a: clauses[i].st) suffix[i].pb(invSA[L++]);
        L++;
    }
    W = 1;
    while (W < L + 10) W <<= 1;
    computeLCP(SA, invSA, L, S, _lcp + W);
    init_lcp();

   // debug();
 
    REP(i,M) clauses_by_end[clauses[i].nd + clauses[i].st.size() - 1].pb(i);    

    vector<int> active_clauses;
    
    LL previously_owned_suffixes = 0;
    FORD(pos, VAR_NUM, 0) {
        for (int id: clauses_by_end[pos]) {
            active_clauses.pb(id);
            result[id] -= previously_owned_suffixes;
        }
        previously_owned_suffixes = (previously_owned_suffixes<<1) % MOD;

        vector<PII> suffixes;
        for (int id: active_clauses) {
            suffixes.pb(PII(suffix[id][pos - clauses[id].nd], id));
        }
        sort(suffixes.begin(), suffixes.end());

        // Get rid of super-clauses
        vector<PII> filtered_suffixes;
        int complete_clause_suffix = -1, complete_clause_length, complete_clause_id;    
        for (auto& p: suffixes) {
            int id = p.nd;
            int sid = p.st;
            //printf("[%d %d %d (%d)]\n", id, sid, complete_clause_suffix, complete_clause_suffix == -1 ? -1 : get_lcp(complete_clause_suffix, sid));
            if (complete_clause_suffix != -1 && get_lcp(complete_clause_suffix, sid) < complete_clause_length) {
                complete_clause_suffix = -1;
            }
            if (complete_clause_suffix != -1) {
                // printf("Dropping %d (because %d %d %d %d)\n", id, complete_clause_id, sid, complete_clause_suffix, get_lcp(complete_clause_suffix, sid));
                result[id] = 0;
                continue;
            }
            filtered_suffixes.pb(p);
            if (pos == clauses[id].nd) {
                complete_clause_suffix = sid;
                complete_clause_length = clauses[id].st.size();
                complete_clause_id = id;
            }
        }
        suffixes = filtered_suffixes;
                
        active_clauses.clear();

        LL preowned_suffixes = 0;
        vector<pair<PII, int> > complete_clauses;
        FORD(i, suffixes.size(), 0) {
            int id = suffixes[i].nd;
            int sid = suffixes[i].st;
            //printf("Processing [%d %d]\n", id, sid);
            int suffix_length = clauses[id].nd + clauses[id].st.size() - pos;
            /*
            while (!complete_clauses.empty()) {
                PII cc = complete_clauses.front();
                if (get_lcp(sid, cc.st) < min((int)clauses[cc.nd].st.size(), suffix_length)) {
                    complete_clauses.pop();
                    preowned_suffixes -= result[cc.nd];
                    printf("Removing %d (%d %d %d %d)\n", cc.nd, cc.st, get_lcp(sid, cc.st), clauses[cc.nd].st.size(), suffix_length);
                } else break;
            }
            */
            int L = 0, R = complete_clauses.size();
            while (L < R) {
                int S = (L + R) / 2;
                PII cc = complete_clauses[S].st;
                if (get_lcp(sid, cc.st) < min((int)clauses[cc.nd].st.size(), suffix_length)) {
                    L = S + 1;
                } else {
                    R = S;
                }
            }
            if (L < complete_clauses.size()) {
                result[id] -= preowned_suffixes - complete_clauses[L].nd;
            }
            if (pos == clauses[id].nd) {
                result[id] = (result[id] + pow2[VAR_NUM - (clauses[id].nd + clauses[id].st.size())]) % MOD;
                complete_clauses.pb(mp(suffixes[i], preowned_suffixes));
                preowned_suffixes += result[id];
                previously_owned_suffixes += result[id];
                //printf("Adding %d\n", id);
            } else {
                active_clauses.pb(id);
            }
        }
    }
    
    LL res = 0;
    REP(i, M) {
        //printf("%d %d\n", i, (result[i] * pow2[clauses[i].nd]) % MOD);
        res += (result[i] * pow2[clauses[i].nd]) % MOD;
    }
    res = ((pow2[VAR_NUM] - res) % MOD + MOD) % MOD;
    
    printf("%lld\n", res);
}