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
#include <bits/stdc++.h>

using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define int LL
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define RE(i,n) FOR(i,1,n)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define R(i,n) REP(i,n)
#define VI vector<int>
#define PII pair<int,int>
#define LD long double
#define FI first
#define SE second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

template<class C> void mini(C&a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxi(C&a4, C b4) { a4 = max(a4, b4); }

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<endl;}
template<class TH, class...TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<",";_dbg(sdbg+1, a...);
}

template<class T> ostream& operator<<(ostream &os, vector<T> V) {
  os<<"[";for(auto vv:V)os<<vv<<","; os<<"]";
  return os;
}
template<class L, class R> ostream& operator<<(ostream &os, pair<L, R> P) {
  os<<"("<<P.first<<","<<P.second<<")";
  return os;
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

const int MaxN = (1 << 20);

int N;

vector<vector<string>> startClauses;


void input() {
  cin >> N;
  string line;
  getline(cin, line);
  getline(cin, line);

  vector<bool> sign(N);
  startClauses.resize(N);
  
  auto procClause = [&](int from, int to) {
    bool neg = false;
    int ptr = from;
    int minId = MaxN, maxId = -1;

    while (ptr < to) {
      if (line[ptr] == '~') { neg = true; }
      if (isdigit(line[ptr])) {
        int who = 0;
        while (isdigit(line[ptr])) {
          who = who * 10 + line[ptr] - '0';
          ptr++;
        }
        who--;

        sign[who] = neg;
        neg = false;
        mini(minId, who);
        maxi(maxId, who);
      }
      ptr++;
    }

    string clause(maxId - minId + 1, '.');
    for (int i = minId; i <= maxId; i++) { clause[i - minId] = sign[i] ? '0' : '1'; }
    startClauses[minId].push_back(clause);
  };

  int ptr = 0, clause = 0;
  while (ptr < SZ(line)) {
    if (line[ptr] == '^') {
      procClause(clause, ptr);
      clause = ptr;
    }
    ptr++;
  }
  procClause(clause, ptr);

  debug(startClauses);
}


const int MaxVerts = (1 << 21);
int nextVert[MaxVerts][2], suflink[MaxVerts];
bool startVert[MaxVerts], endVert[MaxVerts];
int numVerts;
vector<int> atLengths[MaxN];
string charIn[MaxN];


void makeAutomation() {
  numVerts = N + 1;  // i-ty samotny wierzcholek == przed miejscem i w slowie, nie zmatchowalem nic
  for (int start = 0; start < N; start++) {
    nextVert[start][0] = nextVert[start][1] = start + 1;
  }

  for (int start = N - 1; start >= 0; start--) {
    for (auto &word : startClauses[start]) {
      startVert[numVerts] = true;
      debug(word[0], numVerts, start);
      for (int i = 0; i < SZ(word); i++) {
        atLengths[start + i].PB(numVerts++);
        charIn[start + i].PB(word[i]);
      }
      endVert[numVerts - 1] = true;
    }
  }

  for (int pos = 0; pos < N; pos++) {
    int start0 = 0, start1 = 0;

    for (int i = 0; i < SZ(atLengths[pos]); i++) {
      int vert = atLengths[pos][i];
      char ch  = charIn[pos][i];
      int &start = ch == '0' ? start0 : start1;
      int &badStart = ch == '1' ? start0 : start1;
      debug(vert, ch, start0, start1, pos);

      if (startVert[vert]) {
        suflink[vert] = start;
        start = vert;
      } else {
        int prevVert = vert - 1;
        nextVert[prevVert][ch - '0'] = vert;
        int badDigit = (ch - '0') ^ 1;
        int suf = suflink[prevVert];
        int &badNxt = nextVert[prevVert][badDigit];
        if (suf > N) {
          badNxt = nextVert[suf][badDigit];
          if (badNxt == pos + 1 && badStart) { badNxt = badStart; }
          suflink[vert] = nextVert[suf][ch - '0'];
        } else {
          badNxt = pos + 1;
          if (badStart) { badNxt = badStart; }
          suflink[vert] = start;
        }
      }

      if (suflink[vert]) {
        endVert[vert] |= endVert[suflink[vert]];
      }
    }

    if (start0) {
      nextVert[pos][0] = start0;
      /*if (pos) {
        for (int v : atLengths[pos - 1]) {
          if (nextVert[v][0] == pos + 1) { nextVert[v][0] = start0; }
        }
      }*/
    }
    if (start1) {
      nextVert[pos][1] = start1;
      /*if (pos) {
        for (int v : atLengths[pos - 1]) {
          if (nextVert[v][1] == pos + 1) { nextVert[v][1] = start1; }
        }
      }*/
    }
  }

  debug(numVerts);
  for (int i = 0; i < numVerts; i++) {
    debug(i, nextVert[i][0], nextVert[i][1], suflink[i], endVert[i]);
  }
}


const int Mod = 1e9 + 7;

int numPaths[MaxVerts];
bool onQueue[MaxVerts];
int result;

void runDp() {
  numPaths[0] = 1;
  queue<int> Q;
  Q.push(0);
  onQueue[0] = true;

  while (!Q.empty()) {
    int v = Q.front(); Q.pop();
    bool extended = false;
    debug(v, numPaths[v]);

    for (int i = 0; i < 2; i++) {
      int s = nextVert[v][i];
      if (!s) { continue; }
      extended = true;
      if (endVert[s]) { continue; }

      numPaths[s] = (numPaths[s] + numPaths[v]) % Mod;
      if (!onQueue[s]) {
        onQueue[s] = true;
        Q.push(s);
      }
    }

    if (!extended) {
      result = (result + numPaths[v]) % Mod;
    }
  }
}


int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(12);
  cerr << fixed << setprecision(6);

  input();
  makeAutomation();
  runDp();

  cout << result << "\n";
}