#include <bits/stdc++.h>   using namespace std;   using vi = vector<long long>;   template <typename T>   std::istream& operator>>(std::istream& input, std::pair<T, T>& data) {     input >> data.first >> data.second;     return input;   }   template <typename T>   std::istream& operator>>(std::istream& input, std::vector<T>& data) {     for (T& first : data) input >> first;     return input;   }   template <typename T>   std::ostream& operator<<(std::ostream& output, const pair<T, T>& data) {     output <<  (  << data.first <<  ,  << data.second <<  ) ;     return output;   }   template <typename T>   std::ostream& operator<<(std::ostream& output, const std::vector<T>& data) {     for (const T& first : data) output << first <<    ;     return output;   }   std::ostream& operator<<(std::ostream& output, const __int128& first) {     __int128 n = first;     if (n == 0) {       output <<  0 ;       return output;     }     if (n < 0) {       n = -n;       output <<  - ;     }     string s;     while (n) {       s +=  0  + (n % 10);       n /= 10;     }     reverse(s.begin(), s.end());     cout << s;     return output;   }   long long div_up(long long a, long long b) {     return a / b + ((a ^ b) > 0 && a % b);   }   long long div_down(long long a, long long b) {     return a / b - ((a ^ b) < 0 && a % b);   }   long long math_mod(long long a, long long b) { return a - b * div_down(a, b); }   template <class T>   using V = vector<T>;   template <class T>   bool ckmin(T& a, const T& b) {     return b < a ? a = b, 1 : 0;   }   template <class T>   bool ckmax(T& a, const T& b) {     return a < b ? a = b, 1 : 0;   }   long long gcd(long long a, long long b) {     while (b) {       tie(a, b) = make_pair(b, a % b);     }     return a;   }   long long Bit(long long mask, long long bit) { return (mask >> bit) & 1; }   template <long long MOD, long long RT>   struct mint {     static const long long mod = MOD;     static constexpr mint rt() { return RT; }     long long v;     explicit operator long long() const { return v; }     mint() { v = 0; }     mint(long long _v) {       v = (long long)((-MOD < _v && _v < MOD) ? _v : _v % MOD);       if (v < 0) v += MOD;     }     friend bool operator==(const mint& a, const mint& b) { return a.v == b.v; }     friend bool operator!=(const mint& a, const mint& b) { return !(a == b); }     friend bool operator<(const mint& a, const mint& b) { return a.v < b.v; }     friend string ts(mint a) { return to_string(a.v); }     mint& operator+=(const mint& m) {       if ((v += m.v) >= MOD) v -= MOD;       return *this;     }     mint& operator-=(const mint& m) {       if ((v -= m.v) < 0) v += MOD;       return *this;     }     mint& operator*=(const mint& m) {       v = (long long)((long long)v * m.v % MOD);       return *this;     }     mint& operator/=(const mint& m) { return (*this) *= inv(m); }     friend mint pow(mint a, long long p) {       mint ans = 1;       assert(p >= 0);       for (; p; p /= 2, a *= a)         if (p & 1) ans *= a;       return ans;     }     mint& operator^=(const long long& p) { return (*this) = pow(this, p); }     friend mint inv(const mint& a) {       assert(a.v != 0);       return pow(a, MOD - 2);     }     mint operator-() const { return mint(-v); }     mint& operator++() { return *this += 1; }     mint& operator--() { return *this -= 1; }     friend mint operator+(mint a, const mint& b) { return a += b; }     friend mint operator-(mint a, const mint& b) { return a -= b; }     friend mint operator*(mint a, const mint& b) { return a *= b; }     friend mint operator/(mint a, const mint& b) { return a /= b; }     friend mint operator^(mint a, const long long p) { return pow(a, p); }   };   const long long MOD = 1e9 + 7;   typedef mint<MOD, 5> mi;   std::ostream& operator<<(std::ostream& o, const mi& a) {     cout << a.v;     return o;   }   vector<vector<mi> > scmb;   void genComb(long long SZ) {     scmb.assign(SZ, vector<mi>(SZ));     scmb[0][0] = 1;     for (long long i = (1); i < (SZ); ++i)       for (long long j = (0); j < (i + 1); ++j)         scmb[i][j] = scmb[i - 1][j] + (j ? scmb[i - 1][j - 1] : 0);   }   vector<mi> invs, fac, ifac;   void genFac(long long SZ) {     invs.resize(SZ), fac.resize(SZ), ifac.resize(SZ);     invs[1] = fac[0] = ifac[0] = 1;     for (long long i = (2); i < (SZ); ++i)       invs[i] = mi(-(long long)MOD / i * (long long)invs[MOD % i]);     for (long long i = (1); i < (SZ); ++i) {       fac[i] = fac[i - 1] * i;       ifac[i] = ifac[i - 1] * invs[i];     }   }   mi comb(long long a, long long b) {     if (a < b || b < 0) return 0;     assert(a < fac.size());     return fac[a] * ifac[b] * ifac[a - b];   }   const long long N = 507;   long long link[N];   const long long K = 1 << 16;   void ANDConvolution(mi v[K]) {     for (long long step = K; step > 1; step /= 2) {       for (long long start = 0; start < K; start += step) {         for (long long w = 0; w < step / 2; w++) {           v[start + w] += v[start + w + step / 2];         }       }     }   }   void inverseANDConvolution(mi v[K]) {     for (long long step = K; step > 1; step /= 2) {       for (long long start = 0; start < K; start += step) {         for (long long w = 0; w < step / 2; w++) {           v[start + w] -= v[start + w + step / 2];         }       }     }   }   void ORConvolution(mi v[K]) {     for (long long step = K; step > 1; step /= 2) {       for (long long start = 0; start < K; start += step) {         for (long long w = 0; w < step / 2; w++) {           v[start + step / 2 + w] += v[start + w];         }       }     }   }   void inverseORConvolution(mi v[K]) {     for (long long step = K; step > 1; step /= 2) {       for (long long start = 0; start < K; start += step) {         for (long long w = 0; w < step / 2; w++) {           v[start + step / 2 + w] -= v[start + w];         }       }     }   }   mi dp[N][1 << 16];   string s;   mi A[K], B[K], C[K];   long long ptr = 0;   long long solve(long long l, long long r) {     long long v = ptr;     ptr++;     if (l == r) {       long long who = -1, add = -1;       if (s[l] ==  a ) {         who = 0;         add = 1;       } else if (s[l] ==  A ) {         who = 0;         add = 0;       } else if (s[l] ==  b ) {         who = 1;         add = 1;       } else if (s[l] ==  B ) {         who = 1;         add = 0;       } else if (s[l] ==  c ) {         who = 2;         add = 1;       } else if (s[l] ==  C ) {         who = 2;         add = 0;       } else if (s[l] ==  d ) {         who = 3;         add = 1;       } else if (s[l] ==  D ) {         who = 3;         add = 0;       } else {         assert(s[l] ==  ? );       }       if (who != -1) {         long long foo = 0;         for (long long mask = (0); mask < (16); ++mask) {           if (Bit(mask, who) ^ add) {             foo ^= 1 << mask;           }         }         dp[v][foo] += 1;       } else {         for (long long who = (0); who < (4); ++who) {           for (long long add = (0); add < (2); ++add) {             long long foo = 0;             for (long long mask = (0); mask < (16); ++mask) {               if (Bit(mask, who) ^ add) {                 foo ^= 1 << mask;               }             }             dp[v][foo] += 1;           }         }       }       return v;     }     long long L = solve(l + 1, link[l] - 1);     long long nextb = link[l] + 2;     long long R = solve(nextb + 1, link[nextb] - 1);     if (s[nextb - 1] !=  & ) {       for (long long i = (0); i < (K); ++i) {         A[i] = dp[L][i];         B[i] = dp[R][i];       }       ORConvolution(A);       ORConvolution(B);       for (long long i = 0; i < K; i++) {         C[i] = A[i] * B[i];       }       inverseORConvolution(C);       for (long long i = (0); i < (K); ++i) {         dp[v][i] += C[i];       }     }     if (s[nextb - 1] !=  | ) {       for (long long i = (0); i < (K); ++i) {         A[i] = dp[L][i];         B[i] = dp[R][i];       }       ANDConvolution(A);       ANDConvolution(B);       for (long long i = 0; i < K; i++) {         C[i] = A[i] * B[i];       }       inverseANDConvolution(C);       for (long long i = (0); i < (K); ++i) {         dp[v][i] += C[i];       }     }     return v;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> s;     long long len = s.size();     vi sk;     for (long long i = (0); i < (len); ++i) {       if (s[i] ==  ( ) {         sk.push_back(i);       } else if (s[i] ==  ) ) {         assert(sk.size());         link[sk.back()] = i;         link[i] = sk.back();         sk.pop_back();       }     }     long long root = solve(0, len - 1);     long long q;     cin >> q;     long long want = 0;     long long know = 0;     for (long long _ = (0); _ < (q); ++_) {       long long bit = 0;       for (long long i = (0); i < (4); ++i) {         long long first;         cin >> first;         if (first) {           bit ^= 1 << i;         }       }       know ^= 1 << bit;       long long res;       cin >> res;       if (res) {         want ^= 1 << bit;       }     }     mi ans = 0;     for (long long mask = (0); mask < (1 << 16); ++mask) {       bool ok = 1;       for (long long bit = (0); bit < (16); ++bit) {         if (Bit(know, bit)) {           ok &= Bit(want, bit) == Bit(mask, bit);         }       }       if (ok) {         ans += dp[root][mask];       }     }     cout << ans <<   n ;   }