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
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
#define debug 0

int n, m;
long long mod = 1000000007;
long long pmod = 1983917513, p2 = 1583917537, pp[2] = {583917553, 123456757};

struct clause {
  int s, e;
  pair<int, int> xxx;
  vector<bool> v; // v[0] = 1 means not x_s in C -- it ia a forbidden pattern
  vector<long long> hpref, hsuf;  // hashes of prefixes / suffixes
  long long no_bad;
  bool operator<(const clause &c) const {
    return s > c.s || (s == c.s && e < c.e);
  }
  bool deleted;
};
vector<clause> C;
vector<bool> t;
vector<int> by_e, by_s;
vector<long long> pow2, pow_p;

void calc_hashes() {
  for (int i = 0; i < m; ++i) {
    if (debug) {
      printf("for %d: \n", i);
      for (unsigned j = 0; j < C[i].v.size(); ++j)
        printf("%d", (int)C[i].v[j]);
      printf("\nhashes:\n");

    }
    long long h = 0, hs = 0;
    for (unsigned j = 0; j < C[i].v.size(); ++j) {
      h = (p2 * h + pp[C[i].v[j]]) % pmod;
      C[i].hpref.push_back(h);
      hs = (hs + (pp[C[i].v[C[i].v.size()-j-1]]) * pow_p[j]) % pmod;
      C[i].hsuf.push_back(hs);
      if (debug)
        printf("%lld %lld\n", h, hs);
    }
  }
}

bool agree(int a, int b) {  // wether prefix of C[a] is a suffix of C[b] (b ends earlier)
  if (C[a].s <= C[b].s) {
    if (C[a].s == C[b].s)
      return C[a].hpref[C[b].e-C[b].s] == C[b].hpref.back();
    else
      return (C[a].hpref[C[b].s-C[a].s-1] * pow_p[C[b].e-C[b].s+1] + C[b].hpref.back()) % pmod == C[a].hpref[C[b].e-C[a].s];
  }
  else
    return C[a].hpref[C[b].e - C[a].s] == C[b].hsuf[C[b].e - C[a].s];
}

vector<int> started;
long long calc_bad() {
  vector<vector<int> > ends_at(n+1);
  vector<long long> ans;
  ans.push_back(0);
  int s_it = 0;
  for (int pos = 0; pos < n; ++pos) {
    if (debug)
      printf("pos = %d, started = %lu, ans = %lld\n", pos, started.size(), ans.back());
    long long new_ans = (2 * ans.back()) % mod;
    while (s_it < m && C[by_s[s_it]].s <= pos) {
      started.push_back(by_s[s_it]);
      ends_at[C[by_s[s_it]].e].push_back(by_s[s_it]);
      C[by_s[s_it]].no_bad = ans.back();
      C[by_s[s_it]].deleted = false;
      ++s_it;
    }
    if (!ends_at[pos].empty()) {
      unsigned i = 0;
      while (i < ends_at[pos].size()) {  // update ending clauses
        int j = ends_at[pos][i];
        if (C[j].deleted) {
          ends_at[pos][i] = ends_at[pos].back();
          ends_at[pos].pop_back();
          continue;
        }
        new_ans = (mod + new_ans + pow2[C[j].s] - C[j].no_bad) % mod;
//        C[j].no_bad = (mod - C[j].no_bad + pow2[C[j].s] + ans[C[j].s]) % mod;
        if (debug)
          printf("**j = %d, no_bad = %lld\n", j, C[j].no_bad);
        ++i;
      }
      new_ans %= mod;
      for (i = 0; i < started.size(); ++i) {  // update still existing  // TODO
        if (C[started[i]].e == pos) {
          started[i] = started.back();
          started.pop_back();
          --i;
          continue;
        }
        for (unsigned j = 0; j < ends_at[pos].size(); ++j) {
          if (agree(started[i], ends_at[pos][j])) {
            if (C[started[i]].s <= C[ends_at[pos][j]].s) {  // we can erase started[i]
              C[started[i]].deleted = true;
              started[i] = started.back();
              started.pop_back();
              --i;
              break;
            }
            if (debug)
              printf("agree %d %d\n", started[i], ends_at[pos][j]);
            C[started[i]].no_bad = (mod + C[started[i]].no_bad + pow2[C[ends_at[pos][j]].s] - C[ends_at[pos][j]].no_bad) % mod;
            if (debug)
              printf("j = %d, no_bad = %lld\n", started[j], C[started[j]].no_bad);
          }
        }
      }
    }
    ans.push_back(new_ans);
  }
  if (s_it < m)
    printf("ERROR!, s_it = %d\n", s_it);

  return ans.back() % mod;
}


bool comp_s(int a, int b) {
  return C[a].s < C[b].s;
}

bool comp_e_s(int a, int b) {
  return C[a].xxx < C[b].xxx;
}

void read() {
  t = vector<bool>(n);
  while (true) {
    char c;
    int x;
    int mini = n, maxi = 0;
    scanf("%*c");
    while (true) {
      scanf("%c", &c);
      if (c == '~')
        scanf("%*c");
     scanf("%d", &x);
     --x;
     if (x < mini)
       mini = x;
     if (x > maxi)
       maxi = x;
     t[x] = (c == '~');
     scanf("%c", &c);
     if (c == ')')
       break;
     else
       scanf("%*c%*c");
    }
    C.push_back(clause());
    clause &q = C.back();
    q.s = mini;
    q.e = maxi;
    q.v = vector<bool>(maxi - mini + 1);
    for (int i = mini; i <= maxi; ++i) {
      if (debug >= 1)
        printf("%d (%d), ", i, (int) t[i]);
      q.v[i-mini] = t[i];
    }

    if (debug)
      printf("\n");

    if (scanf("%c", &c) < 0 || c != ' ')
      break;
    scanf("%*c%*c");
  }
}

vector<int> to_remove;
void clean(vector<int> &v, unsigned pos) {  // all of them have the same suffixes of length pos
  if (v.size() <= 1)
    return;
  unsigned i = 0, s;
  bool remove = false;
  int remain;
  vector<int> ones;
  while (i < v.size()) {
    s = C[v[i]].v.size();
    if (s == pos) {  // v[i] is a sufix of all others
      remove = true;
      remain = v[i];
      break;
    }
    if (C[v[i]].v[s - pos - 1] == 1) {
      ones.push_back(v[i]);
      v[i] = v[v.size() - 1];
      v.pop_back();
    }
    else {
      ++i;
    }
  }
  if (remove) {
    for (unsigned j = 0; j < ones.size(); ++j) {  // ones are longer
      to_remove.push_back(ones[j]);
    }
    for (unsigned j = 0; j < v.size(); ++j) {
      if (v[j] != remain)
        to_remove.push_back(v[j]);
    }
    return;
  }
  clean(v, pos+1);
  clean(ones, pos+1);
}

int main() {
  scanf("%d ", &n);
  read();
  m = C.size();
  long long a = 1, b = 1;
  for (int i = 0; i <= n; ++i) {
    pow2.push_back(a);
    pow_p.push_back(b);
    b = (p2*b) % pmod;
    a = (2*a) % mod;
  }
//  remove_stupid();
  calc_hashes();
  if (debug >= 3) {
    for (int i = 0; i < m; ++i)
      for (int j = i; j < m; ++j) {
//        printf("%d %d - %d %d\n", C[i].s, C[i].e, C[j].s, C[j].e);
        if (C[i].e <= C[j].e && C[j].s <= C[i].e)
          printf("agree %d %d: %d\n", j, i, agree(j, i));
        if (C[j].e <= C[i].e && C[i].s <= C[j].e)
          printf("agree %d %d: %d\n", i, j, agree(i, j));
      }
  }
  if (debug) {
    printf("\nStayed:\n");
    for (int i = 0; i < m; ++i) {
      printf("%d %d: ", C[i].s, C[i].e);
      for (int j = 0; j < C[i].e - C[i].s + 1; ++j)
        printf("%d", (int) C[i].v[j]);
      printf("\n");
    }
    printf("\n");
  }

  //remove stupid clauses (i.e. one is a suffix of the other)
  vector<vector<int> > ends(n);
  for (int i = 0; i < m; ++i) {
    ends[C[i].e].push_back(i);
  }
  for (int i = 0; i < n; ++i) {
    clean(ends[i], 0);
  }
  if (debug) {
    printf("removing:\n");
    for (unsigned i = 0; i < to_remove.size(); ++i) 
      printf("%d ", to_remove[i]);
    printf("\n");
  }

  sort(to_remove.begin(), to_remove.end());
  to_remove.push_back(1000000000);
  int next_rem = 0;
  //sort bo e, then by s
  int m2 = m;
  for (int i = 0; i < m2; ++i) {
    if (i == to_remove[next_rem]) {
      --m;
      ++next_rem;
      continue;
    }

    by_s.push_back(i);
    by_e.push_back(i);
    C[i].xxx.first = C[i].e;
    C[i].xxx.second = C[i].s;
  }
  sort(by_e.begin(), by_e.end(), comp_e_s);
  sort(by_s.begin(), by_s.end(), comp_s);
  if (debug) {
    printf("processing in order:\n");
    for (int i = 0; i < m; ++i)
      printf("%d (%d %d),  ", by_e[i], C[by_e[i]].s, C[by_e[i]].e);
    printf("\n");
  }

  long long bad = calc_bad();
  long long ans = ((pow2[n] - bad)%mod + mod)%mod;
  printf("%lld\n", ans);

  return 0;
}