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
#include <bits/stdc++.h>
using namespace std;
#define e1 first
#define e2 second
#define pb push_back
#define boost ios_base::sync_with_stdio(false)
#define eb emplace_back
#define OUT(x) {cout << x; exit(0); }
#define scanf(...) scanf(__VA_ARGS__)?:0
#define FOR(i, a, b) for(int i=(a); i<=(b); ++i)
typedef long long ll;
typedef unsigned int ull;
typedef pair <int, int> PII;
typedef pair <PII, int> PPI;
typedef pair <ll, int> PLI;
typedef pair <ll, ll> PLL;
typedef pair <PII, PII> PP;
typedef unsigned int ui;
const int inf = 1e9+9;
const int mod = 1e9+7;
const ll MOD = 1e9+696969;
const ll INF = 1e18;
const int N = (int) 1e6 + 6, L = 21, base = 727;
vector<int> clauses[N];
vector<int> h[N];
int m, xl[N], xr[N];
bool useful[N];
bool is_number(char p) { return p >= '0' && p <= '9'; }
// SA
#define rank why_the_hell
int pw[N], sa[N], rank[N], ind[N], adr[N];
pair<int, int> value[N];
int a[N], b[N], c[N * 2];

void radix_sort(int n) {
    memset(c, 0, sizeof c);
    for (int i = 0; i < n; ++i) c[value[a[i]].second]++;
    for (int i = 1; i < N * 2; ++i) c[i] += c[i - 1];
    for (int i = 0; i < n; ++i) b[--c[value[a[i]].second]] = a[i];
    memset(c, 0, sizeof c);
    for (int i = 0; i < n; ++i) c[value[b[i]].first]++;
    for (int i = 1; i < N * 2; ++i) c[i] += c[i - 1];
    for (int i = n - 1; i >= 0; --i) a[--c[value[b[i]].first]] = b[i];
}

int make_suffix_array(int n) {
    for (int i = 1; i < n; ++i)
        ind[i] = ind[i - 1] + (int) clauses[i - 1].size();
    int m = ind[n - 1] + (int) clauses[n - 1].size();
    ind[n] = m;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < (int) clauses[i].size(); ++j) {
            adr[ind[i] + j] = i;
            rank[ind[i] + j] = 1 + clauses[i][j];
        }
    for (int g = 1; g < L; ++g) {
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < (int) clauses[i].size(); ++j) {
                value[ind[i] + j] = make_pair(rank[ind[i] + j], (j + (1 << (g - 1))) < clauses[i].size()? rank[ind[i] + j + (1 << (g - 1))]: 0);
                a[ind[i] + j] = ind[i] + j;
            }
        radix_sort(m);
        int cnt = 1;
        for (int i = 0; i < m; ++i) {
            cnt += (i > 0 && value[a[i]] != value[a[i - 1]]);
            rank[a[i]] = cnt;
            sa[i] = a[i];
        }
    }
    return m;
}

int get_hash(int j, int l, int r) { return (h[j][r] - h[j][l] * 1ll * pw[r - l] % mod + mod) % mod; }

int lcp(int a, int b) {
    int pa = adr[a], pb = adr[b];
    int la = a - ind[pa], sza = ind[pa + 1] - a;
    int lb = b - ind[pb], szb = ind[pb + 1] - b;
    if (get_hash(pa, la, la + min(sza, szb)) == get_hash(pb, lb, lb + min(sza, szb))) {
        return min(sza, szb);
    }
    int l = 0;
    for (int j = L - 1; j >= 0; --j) if (l + (1 << j) <= min(sza, szb))
        if (get_hash(pa, la, la + l + (1 << j)) == get_hash(pb, lb, lb + l + (1 << j))) {
            l += 1 << j;
        }
    return l; 
}

// SA end
map<int, int> mp[N];
int rev[N], o[N], sum[N], p2[N], i2[N];
int cmp(int x, int y) { return xr[x] < xr[y]; }
int by_sz(int x, int y) { return clauses[x].size() < clauses[y].size(); }
int bpow(int a, int b) { return b != 0? bpow(a * 1ll * a % mod, b >> 1) * 1ll * (b & 1? a: 1) % mod: 1; }

int main() {
    pw[0] = 1;
    i2[0] = 1;
    p2[0] = 1;
    i2[1] = bpow(2, mod - 2);
    p2[1] = 2;
    for (int i = 1; i < N; ++i) {
        pw[i] = pw[i - 1] * 1ll * base % mod;
        if (i > 1) {
            p2[i] = p2[i - 1] * 1ll * p2[1] % mod;
            i2[i] = i2[i - 1] * 1ll * i2[1] % mod;
        }
    }
    int n;
	scanf("%d\n", &n);
    string s="";
    char zn;
    while (1) {
		zn = getchar_unlocked();
		if (zn == '\n') break;
		s += zn;
	}

    for (int i = 0; i < (int) s.size(); ++i) {
        if (s[i] == '(') {
            for (int j = i + 1; j < (int) s.size(); ++j) {
                if (s[j] == ')') {
                    for (int k = i + 1; k < j; ++k) if (s[k] == 'x') {
                        int sign = !(s[k - 1] == '~');
                        int ind = 0;
                        for (int p = k + 1; p < j; ++p) if (is_number(s[p])) {
                            ind *= 10;
                            ind += s[p] - '0';
                        } else break;
                        clauses[m].push_back((ind - 1) << 1 | sign);
                    }
                    break;
                }
            }
            m++;
        }
    }
    s.clear();

    for (int i = 0; i < m; ++i) {
        sort(clauses[i].begin(), clauses[i].end());
        o[i] = i;
        xl[i] = clauses[i][0] >> 1;
        xr[i] = clauses[i].back() >> 1;
        useful[i] = 1;
    }
    for (int j = 0; j < m; ++j) {
        h[j].resize(clauses[j].size() + 1);
        for (int i = 0; i < (int) clauses[j].size(); ++i) {
            int val = clauses[j][i];
            h[j][i + 1] = (h[j][i] * 1ll * base + val + 1) % mod;
        }
    }
    // Operation SA
    set<int> st;
    int sz = make_suffix_array(m);
    for (int j = 0; j < sz; ++j) {
        int i = sa[j];
        rev[i] = j;
    }
    sort(o, o + m, by_sz);
    reverse(o, o + m);
    for (int _ = 0; _ < m; ++_) {
        int j = o[_];
        for (int i = ind[j]; i < ind[j + 1]; ++i) {
            st.insert(rev[i]);
        }
        int p = rev[ind[j]];
        auto xx = st.lower_bound(p);
        while ((int) st.size()) {
            auto it = xx;
            int cnt = 0;
            if (it != st.begin()) {
                it--;
                if (lcp(ind[j], sa[*it]) == ind[j + 1] - ind[j]) {
                    useful[adr[sa[*it]]] = 0;
                    st.erase(it);
                    continue;
                }
                it++;
                cnt++;
            } else cnt++;
            if (it != st.end() && ++it != st.end()) { 
                if (lcp(ind[j], sa[*it]) == ind[j + 1] - ind[j]) {
                   useful[adr[sa[*it]]] = 0;
                   st.erase(it);
                   continue;
                }
                cnt++;
            } else cnt++;
            if (cnt == 2) break;
        }
    }
    st.clear();
    // udalo sie xd
    for (int j = 0; j < m; ++j) {
        clauses[j].clear();
        clauses[j].resize(0);
        clauses[j].shrink_to_fit();
    }
    sort(o, o + m, cmp);
    int last = 0;
    long long res = p2[n];
    for (int _ = 0; _ < m; ++_) {
        int j = o[_];
        if (!useful[j]) {
            continue;
        }
        while (last < xr[j]) {
            sum[last + 1] = sum[last];
            ++last;
        }
        int dp = 0;

        dp = (xl[j] >= 1? (mod - sum[xl[j] - 1] * 1ll * i2[xr[j] - xl[j] + 1] % mod + mod) % mod: 0);
        dp = (dp + p2[n - (xr[j] - xl[j] + 1)]) % mod;
        for (int x = xl[j]; x <= xr[j]; ++x) {
            dp = (dp - mp[x][get_hash(j, 0, x + 1 - xl[j])] * 1ll * i2[xr[j] - x] % mod + mod) % mod;
        }
        for (int x = xl[j]; x <= xr[j]; ++x) {
            int &to_add = mp[xr[j]][get_hash(j, x - xl[j], xr[j] - xl[j] + 1)];
            to_add = (to_add + dp) % mod;
        } 
        sum[xr[j]] = (sum[xr[j]] + dp) % mod;
        res = (res - dp + mod) % mod;
    }
    cout << res << endl;
    
}