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
#include <stdio.h>
#include <stdlib.h>

#define MODULO 1000000007
#define MAX_N 1000000
#define MAX_FORMULAS 1000000

int pow2[1 + MAX_N];

struct {
  int child[2];
  int count;
  int power;
  char leaf;
} T[1 + 1 + 2 * MAX_FORMULAS];
int T_last_used = 0;
int new_node(void) { return ++T_last_used; }

int leaf(int node) {
  return T[node].leaf;
}

int value(int node) {
  if (!node)
    return 0;

  long long v = T[node].count * 1LL * pow2[T[node].power];
  v += value(T[node].child[0]);
  v += value(T[node].child[1]);
  return v % MODULO;
}

void split(int node) {
  int zero = new_node();
  int one = new_node();
  T[zero].child[0] = 0;
  T[zero].child[1] = 0;
  T[zero].count = 0;
  T[zero].power = 0;
  T[zero].leaf = 1;
  T[one ].child[0] = 0;
  T[one ].child[1] = 0;
  T[one ].count = 0;
  T[one ].power = 0;
  T[one ].leaf = 1;
  T[node].child[0] = zero;
  T[node].child[1] = one;
  T[node].leaf = 0;
}

void add(int node, int count, int power) {
  if (!node)
    return;

  if (!count)
    return;

  int c = T[node].count;

  if (!c) {
    T[node].count = count;
    T[node].power = power;
    return;
  }

  int p = T[node].power;
  if (p > power) {
    long long v = c * 1LL * pow2[p - power] + count;
    T[node].count = v % MODULO;
    T[node].power = power;
  } else if (p < power) {
    long long v = count * 1LL * pow2[power - p] + c;
    T[node].count = v % MODULO;
    T[node].power = p;
  } else {
    T[node].count = (c + count) % MODULO;
  }
}

int merge2(int node1, int node2) {
  if (!node1) return node2;
  if (!node2) return node1;
  if (leaf(node1)) {
    add(node2, T[node1].count, T[node1].power);
    return node2;
  } else if (leaf(node2)) {
    add(node1, T[node2].count, T[node2].power);
    return node1;
  } else {
    add(node1, T[node2].count, T[node2].power);
    T[node2].count = 0;
    T[node2].power = 0;
    T[node1].child[0] = merge2(T[node1].child[0], T[node2].child[0]);
    T[node1].child[1] = merge2(T[node1].child[1], T[node2].child[1]);
    T[node1].leaf = 0;
    return node1;
  }
}

int merge(int node) {
  if (leaf(node)) return node;

  int n1 = T[node].child[0];
  int n2 = T[node].child[1];
  int c = T[node].count;
  int p = T[node].power;
  if (c) {
    add(n1, c, p - 1);
    add(n2, c, p - 1);
  }
  int n = merge2(n1, n2);

  return n;
}

int N;
int X[MAX_N];
char Y[MAX_FORMULAS + 1];
int Yff = 0;

typedef struct {
  int ofs, cnt, ptr;
} ofs_cnt_ptr;

ofs_cnt_ptr P[MAX_FORMULAS];
int Pff = 0;

int cmp(const void * a, const void * b) {
  const ofs_cnt_ptr * u = (const ofs_cnt_ptr *)a;
  const ofs_cnt_ptr * v = (const ofs_cnt_ptr *)b;

  if (u->ofs < v->ofs) return -1;
  if (u->ofs > v->ofs) return +1;
  return 0;
}

void processX(int cnt) {
  int minX = N + 1;
  int maxX = 0;
  int i;
  for (i = 0; i < cnt; ++i) {
    int v = X[i];
    if (v < 0) v = -v;
    if (v < minX) minX = v;
    if (v > maxX) maxX = v;
  }
  for (i = 0; i < cnt; ++i) {
    int v = X[i];
    char c = (v < 0) ? '0' : '1';
    if (v < 0) v = -v;
    Y[Yff + v - minX] = c;
  }
  P[Pff].ofs = minX;
  P[Pff].cnt = cnt;
  P[Pff].ptr = Yff;
  ++Pff;
  Yff += cnt;
}

int main (void) {
  int i;
  i = scanf("%d\n", &N);

  pow2[0] = 1;
  for (i = 1; i <= N; ++i) pow2[i] = (pow2[i - 1] * 2) % MODULO;

  for (;;) {
    int Xff = 0;
    int d = getchar();
    if (d == ' ') continue;  // none of
    if (d == '\r') continue; // these should
    if (d == '\n') continue; // ever happen
    if (d == EOF) break;     // entirely empty
    for (;;) {
      int neg = 0;
      int c = getchar();
      if (c == '~') {
        neg = 1;
        c = getchar();
      }
      int i;
      scanf("%d", &i);
      X[Xff++] = neg ? -i : +i;
      c = getchar(); // ')' or ' '
      if (c == ')') break;
      c = getchar(); // 'v'
      c = getchar(); // ' '
    }
    processX(Xff);
    d = getchar(); // probably ' '
    if (d == '\r') break;
    if (d == '\n') break;
    if (d == EOF) break;
    d = getchar(); // '^'
    d = getchar(); // ' '
  }

  qsort(P, Pff, sizeof(P[0]), cmp);

  int root = new_node();
  T[root].child[0] = 0;
  T[root].child[1] = 0;
  T[root].count = 1;
  T[root].power = N;
  T[root].leaf = 1;
  int top_var = 1;

  for (i = 0; i < Pff; ++i) {
    int j;
    int start = P[i].ptr;
    int count = P[i].cnt;
    int end = start + count;

    for (; top_var < P[i].ofs; ++top_var) root = merge(root);

    int where = root;
    for (j = start; j < end; ++j) {
      int dir = Y[j] - '0';
      if (leaf(where)) split(where);
      if (T[where].count) {
        add(T[where].child[0], T[where].count, T[where].power - 1);
        add(T[where].child[1], T[where].count, T[where].power - 1);
      }
      T[where].count = 0;
      T[where].power = 0;
      where = T[where].child[dir];
      if (!where) break;
    }
    if (!where) continue;
    T[where].child[0] = 0;
    T[where].child[1] = 0;
    T[where].count = 0;
    T[where].power = 0;
    T[where].leaf = 1;
  }

  printf("%d\n", value(root));
  return 0;
}