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
#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAINS(A,X) (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
template<typename T, typename U> ostream& operator << (ostream& os, const pair<T, U> &_p) { return os << "(" << _p.X << "," << _p.Y << ")"; }
template<typename T> ostream& operator << (ostream &os, const vector<T>& _V) { bool f = true; os << "["; for(auto v: _V) { os << (f ? "" : ",") << v; f = false; } return os << "]"; }
template<typename T> ostream& operator << (ostream &os, const set<T>& _S) { bool f = true; os << "("; for(auto s: _S) { os << (f ? "" : ",") << s; f = false; } return os << ")"; }
template<typename T, typename U> ostream& operator << (ostream &os, const map<T, U>& _M) { return os << set<pair<T, U>>(ALL(_M)); }
typedef signed long long slong;
typedef long double ldouble;
const slong INF = 1000000100;
const ldouble EPS = 1e-9;

int N, M;
string S;
const int MAXN = 1000042;
const slong p = 1000000021, mod = 1000000033, Q = 1000000007;
slong pows[2*MAXN];
slong dp[MAXN];

typedef pair<int,int> pii;
pii seg[MAXN]; // [st,en] klazul
int nope[MAXN]; // nadklauzula, zignorowac
vector<int> V[MAXN]; // 0 - x, 1 - ~x
vector<slong> H[MAXN]; // hasze
set<pair<int,int> > us; // (start,hasz)

#define cerr if(0)cout

void read_data() {
    scanf("%d ", &N);
    getline(cin, S);
    cerr << S << endl;
    vector<vector<int> > C;
    vector<pii> cur;
    vector<pair<pii,int> > w; // ((koniec,dlugosc),nr)
    FORW(i,0,SIZE(S))
    {
        if(S[i] == 'x')
        {
            int neg = (S[i-1] == '~');
            i++;
            int no = 0;
            while('0' <= S[i] && S[i] <= '9')
            {
                no = 10 * no + S[i] - '0';
                i++;
            }
            cur.PB(MP(no,neg));
        }
        if(S[i] == ')')
        {
            sort(ALL(cur));
            w.PB(MP(MP(cur.back().X,cur.back().X-cur[0].X),SIZE(C)));
            C.PB(vector<int>());
            for(pii x : cur)
            {
                C.back().PB(x.Y);
            }
            cur.clear();
        }
    }
    sort(ALL(w));

    M = SIZE(C);
    int cnt = 0;
    for(auto x : w)
    {
        cerr << x.X << " : " << C[x.Y] << endl;
        seg[cnt] = MP(x.X.X-x.X.Y,x.X.X);
        V[cnt] = C[x.Y];
        cnt++;
    }
}

vector<pair<int,int> > starts[MAXN]; // (dlugosc,hasz) sie tam zaczynaja
vector<int> kon[MAXN]; // ktore (nie nope) sie tam koncza
slong bad[MAXN]; // ile prefiksow ze i jest pierwsza zla klauzula
map<pair<int,int> ,int> meh[MAXN]; // sufiks (dlugosc,hasz) ma juz troche bad

void solve() {
    FORW(i,0,M)
    {
        int n = SIZE(V[i]);
        H[i].resize(n,0);
        FORW(j,0,n)
        {
            if(j)
            {
                H[i][j] = H[i][j-1];
            }
            if(V[i][j])
            {
                H[i][j] = (H[i][j] + pows[seg[i].X+j])%mod;
            }
        }
        cerr << seg[i] << " : " << V[i] << endl;
        pair<int,slong> cur(seg[i].X,H[i].back());
        if(us.count(cur))
        {
            // duplikat klauzuli
            cerr << "duplikat" << endl;
            nope[i] = 1;
            continue;
        }
        us.insert(cur);

        // Sprawdz czy sa podklauzule
        FORW(j,0,n)
        {
            int jj = seg[i].X + j;
            if(nope[i])
            {
                break;
            }
            for(auto y : starts[jj])
            {
                if(y.X == n)
                {
                    // ta sama dlugosc, duplikaty juz sprawdzone
                    // to powinno dac O(n^(3/2)) bo porownujemy sie
                    // tylko z krotszymi
                    break;
                }
                int k = jj + y.X - 1;
                slong h = H[i][k - seg[i].X]; // nasz hasz na [jj,k]
                if(j)
                {
                    h = (h + mod - H[i][j-1])%mod;
                }
                if(h == y.Y)
                {
                    cerr << i << " ma podklazule w zakresie " << MP(jj,k) << endl;
                    nope[i] = 1;
                    break;
                }
            }
        }

        starts[seg[i].X].PB(MP(n,H[i].back()));

        if(!nope[i])
        {
            kon[seg[i].Y].PB(i);
        }
    }

    dp[0] = 1;
    FOR(i,1,N)
    {
        dp[i] = (dp[i-1] * 2) % Q;
        for(int x : kon[i])
        {
            bad[x] = dp[seg[x].X-1];
            int n = seg[x].Y - seg[x].X + 1;
            FORW(j,0,n)
            {
                // ehh, to chyba tez wykryje duplikaty...
                pii ehh(j,H[x][j]);
                bad[x] -= meh[seg[x].X][ehh];
            }
            bad[x] = ((bad[x]%Q)+Q)%Q;
            dp[i] -= bad[x];

            FORW(j,0,n)
            {
                pii cur(n-j-1, H[x].back());
                if(j)
                {
                    cur.Y = (cur.Y + mod - H[x][j-1])%mod;
                }
                meh[seg[x].X+j][cur] += bad[x];
                meh[seg[x].X+j][cur] %= Q;
            }
        }
        dp[i] = ((dp[i]%Q)+Q)%Q;
    }
    cout << dp[N] << "\n";
}

int main() {
    pows[0] = 1;
    FORW(i,1,2*MAXN)
    {
        pows[i] = (pows[i-1] * p) % mod;
    }
    read_data();
    solve();
}