#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define int long long
#define st first
#define nd second
#define rd third
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using namespace std;
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
#define next ____next
#define prev ____prev
#define left ____left
#define hash ____hash
typedef long long ll;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<pair<int, int> > VPII;
typedef vector<pair<ll, ll> > VPLL;
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 T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third;
bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
template<class T> void ResizeVec(T&, vector<int>) {}
template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
for (T& v : vec) { ResizeVec(v, sz); }
}
typedef Triple<int, int, int> TIII;
template<class A, class B, class C>
ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
const int P = 1e9 + 7;
struct Node {
VI children;
int val;
VI which;
};
struct Word {
int beg;
VI let;
int OnPos(int en) {
assert(en - beg < SZ(let));
return let[en - beg];
}
bool IsEnd(int pos) {
return pos - beg == SZ(let) - 1;
}
};
const int N = 1e6 + 5;
Word words[N];
VI which_start[N];
int32_t main() {
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
cin.tie(0);
//double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;
int n;
cin>>n;
string s;
vector<VPII> all;
VPII cur;
int cnt = 0;
while (cin>>s) {
bool should_end = false;
if (s.back() == ')') {
should_end = true;
s.pop_back();
}
if (s.back() == '^' || s.back() == 'v') {
continue;
}
if (s[0] == '(') {
s = s.substr(1);
}
int forb = 0;
if (s[0] == '~') {
forb = 1;
s = s.substr(1);
}
int ind = stoi(s.substr(1));
cur.PB({ind, forb});
if (should_end) {
sort(ALL(cur));
cnt++;
words[cnt].beg = cur[0].st;
for (auto p : cur) {
words[cnt].let.PB(p.nd);
}
which_start[cur[0].st].PB(cnt);
//all.PB(cur);
cur.clear();
}
}
//debug(all);
vector<Node> tree{{{}, 1, {}}};
RE (pos, n) {
vector<Node> new_tree;
for (auto x : which_start[pos]) {
tree[0].which.PB(x);
}
new_tree.PB({{}, 0, {}}); // TODO ??
function<void(int, VI)> Dfs = [&](int v, VI links) {
VI ends(2);
VVI which(2);
for (auto word_ind : tree[v].which) {
Word& w = words[word_ind];
REP (add, 2) {
if (w.OnPos(pos) == add) {
if (w.IsEnd(pos)) {
ends[add] = true;
} else {
which[add].PB(word_ind);
}
}
}
}
REP (add, 2) {
if (links[add] == -1 || ends[add]) {
links[add] = -1;
} else {
if (which[add].empty()) {
new_tree[links[add]].val = (new_tree[links[add]].val + tree[v].val) % P;
} else {
new_tree[links[add]].children.PB(SZ(new_tree));
links[add] = SZ(new_tree);
new_tree.PB({{}, tree[v].val, which[add]});
}
}
}
for (auto nei : tree[v].children) {
Dfs(nei, links);
}
};
Dfs(0, {0, 0});
swap(tree, new_tree);
}
int ans = 0;
for (auto x : tree) {
ans = (ans + x.val) % P;
}
cout<<ans<<endl;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgTVAgbWFrZV9wYWlyCiNkZWZpbmUgUEIgcHVzaF9iYWNrCiNkZWZpbmUgaW50IGxvbmcgbG9uZwojZGVmaW5lIHN0IGZpcnN0CiNkZWZpbmUgbmQgc2Vjb25kCiNkZWZpbmUgcmQgdGhpcmQKI2RlZmluZSBGT1IoaSwgYSwgYikgZm9yKGludCBpID0oYSk7IGkgPD0oYik7ICsraSkKI2RlZmluZSBSRShpLCBuKSBGT1IoaSwgMSwgbikKI2RlZmluZSBGT1JEKGksIGEsIGIpIGZvcihpbnQgaSA9IChhKTsgaSA+PSAoYik7IC0taSkKI2RlZmluZSBSRVAoaSwgbikgZm9yKGludCBpID0gMDtpIDwobik7ICsraSkKI2RlZmluZSBWQVIodiwgaSkgX190eXBlb2YoaSkgdj0oaSkKI2RlZmluZSBGT1JFKGksIGMpIGZvcihWQVIoaSwgKGMpLmJlZ2luKCkpOyBpICE9IChjKS5lbmQoKTsgKytpKQojZGVmaW5lIEFMTCh4KSAoeCkuYmVnaW4oKSwgKHgpLmVuZCgpCiNkZWZpbmUgU1ooeCkgKChpbnQpKHgpLnNpemUoKSkKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdGVtcGxhdGU8dHlwZW5hbWUgVEg+IHZvaWQgX2RiZyhjb25zdCBjaGFyKiBzZGJnLCBUSCBoKSB7IGNlcnI8PHNkYmc8PCI9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iAgaW50IGNudCA9IDA7CiAgd2hpbGUgKGNpbj4+cykgewogICAgYm9vbCBzaG91bGRfZW5kID0gZmFsc2U7CiAgICBpZiAocy5iYWNrKCkgPT0gJyknKSB7CiAgICAgIHNob3VsZF9lbmQgPSB0cnVlOwogICAgICBzLnBvcF9iYWNrKCk7CiAgICB9CiAgICBpZiAocy5iYWNrKCkgPT0gJ14nIHx8IHMuYmFjaygpID09ICd2JykgewogICAgICBjb250aW51ZTsKICAgIH0KICAgIGlmIChzWzBdID09ICcoJykgewogICAgICBzID0gcy5zdWJzdHIoMSk7CiAgICB9CiAgICBpbnQgZm9yYiA9IDA7CiAgICBpZiAoc1swXSA9PSAnficpIHsKICAgICAgZm9yYiA9IDE7CiAgICAgIHMgPSBzLnN1YnN0cigxKTsKICAgIH0KICAgIGludCBpbmQgPSBzdG9pKHMuc3Vic3RyKDEpKTsKICAgIGN1ci5QQih7aW5kLCBmb3JifSk7CiAgICBpZiAoc2hvdWxkX2VuZCkgewogICAgICBzb3J0KEFMTChjdXIpKTsKICAgICAgY250Kys7CiAgICAgIHdvcmRzW2NudF0uYmVnID0gY3VyWzBdLnN0OwogICAgICBmb3IgKGF1dG8gcCA6IGN1cikgewogICAgICAgIHdvcmRzW2NudF0ubGV0LlBCKHAubmQpOwogICAgICB9CiAgICAgIHdoaWNoX3N0YXJ0W2N1clswXS5zdF0uUEIoY250KTsKICAgICAgLy9hbGwuUEIoY3VyKTsKICAgICAgY3VyLmNsZWFyKCk7CiAgICB9CiAgfQogIC8vZGVidWcoYWxsKTsKICAKICB2ZWN0b3I8Tm9kZT4gdHJlZXt7e30sIDEsIHt9fX07CiAgUkUgKHBvcywgbikgewogICAgdmVjdG9yPE5vZGU+IG5ld190cmVlOwogICAgZm9yIChhdXRvIHggOiB3aGljaF9zdGFydFtwb3NdKSB7CiAgICAgIHRyZWVbMF0ud2hpY2guUEIoeCk7CiAgICB9CiAgICBuZXdfdHJlZS5QQih7e30sIDAsIHt9fSk7IC8vIFRPRE8gPz8KICAgIGZ1bmN0aW9uPHZvaWQoaW50LCBWSSk+IERmcyA9IFsmXShpbnQgdiwgVkkgbGlua3MpIHsKICAgICAgVkkgZW5kcygyKTsKICAgICAgVlZJIHdoaWNoKDIpOwogICAgICBmb3IgKGF1dG8gd29yZF9pbmQgOiB0cmVlW3ZdLndoaWNoKSB7CiAgICAgICAgV29yZCYgdyA9IHdvcmRzW3dvcmRfaW5kXTsKICAgICAgICBSRVAgKGFkZCwgMikgewogICAgICAgICAgaWYgKHcuT25Qb3MocG9zKSA9PSBhZGQpIHsKICAgICAgICAgICAgaWYgKHcuSXNFbmQocG9zKSkgewogICAgICAgICAgICAgIGVuZHNbYWRkXSA9IHRydWU7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgd2hpY2hbYWRkXS5QQih3b3JkX2luZCk7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH0KICAgICAgUkVQIChhZGQsIDIpIHsKICAgICAgICBpZiAobGlua3NbYWRkXSA9PSAtMSB8fCBlbmRzW2FkZF0pIHsKICAgICAgICAgIGxpbmtzW2FkZF0gPSAtMTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgaWYgKHdoaWNoW2FkZF0uZW1wdHkoKSkgewogICAgICAgICAgICBuZXdfdHJlZVtsaW5rc1thZGRdXS52YWwgPSAobmV3X3RyZWVbbGlua3NbYWRkXV0udmFsICsgdHJlZVt2XS52YWwpICUgUDsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIG5ld190cmVlW2xpbmtzW2FkZF1dLmNoaWxkcmVuLlBCKFNaKG5ld190cmVlKSk7CiAgICAgICAgICAgIGxpbmtzW2FkZF0gPSBTWihuZXdfdHJlZSk7CiAgICAgICAgICAgIG5ld190cmVlLlBCKHt7fSwgdHJlZVt2XS52YWwsIHdoaWNoW2FkZF19KTsKICAgICAgICAgIH0KICAgICAgICB9IAogICAgICB9CiAgICAgIGZvciAoYXV0byBuZWkgOiB0cmVlW3ZdLmNoaWxkcmVuKSB7CiAgICAgICAgRGZzKG5laSwgbGlua3MpOwogICAgICB9CiAgICB9OwogICAgRGZzKDAsIHswLCAwfSk7CiAgICBzd2FwKHRyZWUsIG5ld190cmVlKTsKICB9CiAgaW50IGFucyA9IDA7CiAgZm9yIChhdXRvIHggOiB0cmVlKSB7CiAgICBhbnMgPSAoYW5zICsgeC52YWwpICUgUDsKICB9CiAgY291dDw8YW5zPDxlbmRsOwogIHJldHVybiAwOwp9Cg==