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
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>

typedef std::pair<int, int> Info;
typedef long long i64;

const int N = 2000000 + 10, MOD = 1000000007;
const Info B(7, 11);

inline Info operator+ (const Info &lhs, const Info &rhs) {
  return Info((lhs.first + rhs.first) % MOD, (lhs.second + rhs.second) % MOD);
}

inline Info operator- (const Info &lhs, const Info &rhs) {
  return Info((lhs.first - rhs.first + MOD) % MOD, (lhs.second - rhs.second + MOD) % MOD);
}

inline Info operator* (const Info &lhs, const Info &rhs) {
  return Info((i64)lhs.first * rhs.first % MOD, (i64)lhs.second * rhs.second % MOD);
}

int n, m;

int nextInt() {
  char ch;
  while (!isdigit(ch = getchar())) {}
  int res = ch - '0';
  while (isdigit(ch = getchar())) res = 10 * res + ch - '0';
  return res;
}

bool check(char c) { return c == EOF || c == '(' || isdigit(c) || c == '~' || c == 'x'; }

int nextToken() {
  char c;
  while (!check(c = getchar())) {}
  if (c == EOF) return N;
  if (c == '(') return 0;
  int res = nextInt();
  return c == '~' ? -res : res;
}

std::vector<int> pool[N];
std::vector<Info> mem[N];

Info val[N];

int l[N], r[N];

inline int get(int x, int y) { return l[x] <= y && y <= r[x] ? pool[x][y - l[x]] : -1; }

int pos;

bool ispre(const Info &a, const Info &b) {
  if (a.first == -1) return true;
  if (a.first != b.first) return false;
  int x = a.second, y = b.second;
  if (!x) return true;
  if (!y) return pos == l[x];
  if (l[y] > l[x]) return false;
  Info info = mem[y][pos - l[y]];
  info = info - val[pos - l[x]] * mem[y][l[x] - l[y]];
  return mem[x][pos - l[x]] == info;
}

bool iseq(const Info &a, const Info &b) {
  if (a.first != b.first) return false;
  int x = a.second, y = b.second;
  if (!x) return !y || pos == l[y];
  if (!y) return pos == l[x];
  if (l[x] != l[y]) return false;
  return mem[x][pos - l[x]] == mem[y][pos - l[y]];
}

void solve(int f[], int sa[], int &tot) {
  static Info g[N];
  static int temp[N];
  static bool flag[N];
  int cnt = 0;
  g[0] = std::make_pair(-1, 0);
  temp[0] = 0;
  flag[0] = true;
  for (int i = 0; i <= tot; ++i) {
    g[++cnt] = Info(0, sa[i]);
    temp[cnt] = f[i];
    flag[cnt] = (get(sa[i], pos) == 0);
  }
  for (int i = 0; i <= tot; ++i) {
    g[++cnt] = Info(1, sa[i]);
    temp[cnt] = f[i];
    flag[cnt] = (get(sa[i], pos) == 1);
  }
  static int h[N], stk[N];
  std::fill(h, h + cnt + 1, 0);
  int top = 0;
  for (int i = 0; i <= cnt;) {
    int j = i;
    while (j <= cnt && iseq(g[i], g[j])) ++j;
    for (int k = i; k < j; ++k) {
      if (!flag[k]) continue;
      while (top && !ispre(g[stk[top]], g[k])) --top;
      stk[++top] = k;
    }
    while (top && !ispre(g[stk[top]], g[i])) --top;
    for (int k = i; k < j; ++k) (h[stk[top]] += temp[k]) %= MOD;
    i = j;
  }
  static bool mark[N];
  mark[0] = false;
  for (int i = 1; i <= cnt; ++i) mark[i] = (flag[i] && r[g[i].second] == pos);
  for (int i = 1, t = 0; i <= cnt;) {
    int j = i;
    while (j <= cnt && iseq(g[i], g[j])) ++j;
    for (int k = i; k < j; ++k) {
      if (mark[k]) {
        if (!t || !ispre(g[t], g[k])) t = k;
        break;
      }
    }
    if (t && ispre(g[t], g[i])) for (int k = i; k < j; ++k) mark[k] = true;
    i = j;
  }
  tot = -1;
  for (int i = 0; i <= cnt; ++i) if (flag[i] && !mark[i]) f[++tot] = h[i], sa[tot] = g[i].second;
}

int main() {
  val[0] = Info(1, 1);
  for (int i = 1; i < N; ++i) val[i] = B * val[i - 1];
  scanf("%d", &n);
  for (int t; (t = nextToken()) != N;) if (t) pool[m].push_back(t); else ++m;
  for (int i = 1; i <= m; ++i) {
    std::vector<int> temp = pool[i];
    const int tot = temp.size();
    l[i] = n + 1, r[i] = 0;
    for (int j = 0; j < tot; ++j) {
      int t = temp[j];
      l[i] = std::min(l[i], abs(t));
      r[i] = std::max(r[i], abs(t));
    }
    for (int j = 0; j < tot; ++j) {
      int t = temp[j];
      pool[i][abs(t) - l[i]] = (t > 0);
    }
    mem[i].resize(tot + 1);
    for (int j = 0; j < tot; ++j) mem[i][j + 1] = mem[i][j] * B + Info(pool[i][j], pool[i][j]);
  }
  static std::vector<int> ins[N];
  for (int i = 1; i <= m; ++i) ins[l[i]].push_back(i);
  static int f[N], sa[N];
  int tot = 0;
  f[0] = 1, sa[0] = 0;
  for (int i = 1; i <= n; ++i) {
    int t = tot;
    for (int j = 0; j < ins[i].size(); ++j) f[++tot] = 0, sa[tot] = ins[i][j];
    std::rotate(f + 1, f + t + 1, f + tot + 1);
    std::rotate(sa + 1, sa + t + 1, sa + tot + 1);
    pos = i;
    solve(f, sa, tot);
  }
  printf("%d\n", f[0]);
  return 0;
}