#include <bits/stdc++.h>
using namespace std;
char a[505];
int A[4][505], nx[505], sk[505], tot, m, len;
struct hh {
  int a[1 << 16];
  hh() { memset(a, 0, sizeof(a)); }
} qr[505];
hh dfs(int st, int en) {
  if (st == en) {
    hh ans;
    if (a[st] == '0')
      ans.a[0] = 1;
    else if (a[st] == '1')
      ans.a[len - 1] = 1;
    else if (a[st] == '?') {
      for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 2; ++j) {
          int tp = 0;
          for (int k = 0; k < m; ++k) tp |= (A[i][k] ^ j) << k;
          ans.a[tp] += 1;
        }
      }
    } else {
      int w = a[st] >= 'a' && a[st] <= 'z', f, tp = 0;
      if (w)
        f = a[st] - 'a';
      else
        f = a[st] - 'A';
      for (int j = 0; j < m; ++j) tp |= (A[f][j] ^ w) << j;
      ans.a[tp] = 1;
    }
    return ans;
  }
  int r = st - 1, tot = st - 1;
  for (int i = st; i <= en; ++i) {
    if (a[i] == '?')
      sk[++r] = 1;
    else if (a[i] == '|')
      sk[++r] = 2;
    else if (a[i] == '&')
      sk[++r] = 3;
    else if (a[i] == '(') {
      qr[++tot] = dfs(i + 1, nx[i] - 1);
      i = nx[i];
    }
  }
  hh ans = qr[st];
  for (int i = st + 1; i <= tot; ++i) {
    hh w1, sum1, sum2, w2;
    if (sk[i - 1] == 2 || sk[i - 1] == 1) {
      for (int j = 0; j < len; ++j)
        sum1.a[j] = ans.a[j], sum2.a[j] = qr[i].a[j];
      for (int j = 0; j < m; ++j)
        for (int k = 0; k < len; ++k)
          if ((1 << j) & k) {
            sum1.a[k] = (sum1.a[k] + sum1.a[k ^ (1 << j)]) % 1000000007;
            sum2.a[k] = (sum2.a[k] + sum2.a[k ^ (1 << j)]) % 1000000007;
          }
      for (int j = 0; j < len; ++j)
        w1.a[j] = 1ll * sum1.a[j] * sum2.a[j] % 1000000007;
      for (int j = 0; j < m; ++j)
        for (int k = 0; k < len; ++k)
          if ((1 << j) & k)
            w1.a[k] = (w1.a[k] - w1.a[k ^ (1 << j)]) % 1000000007;
    }
    if (sk[i - 1] == 3 || sk[i - 1] == 1) {
      for (int j = 0; j < len; ++j)
        sum1.a[j] = ans.a[j], sum2.a[j] = qr[i].a[j];
      for (int j = 0; j < m; ++j)
        for (int k = 0; k < len; ++k)
          if (((1 << j) & k) == 0) {
            sum1.a[k] = (sum1.a[k] + sum1.a[k ^ (1 << j)]) % 1000000007;
            sum2.a[k] = (sum2.a[k] + sum2.a[k ^ (1 << j)]) % 1000000007;
          }
      for (int j = 0; j < len; ++j)
        w2.a[j] = 1ll * sum1.a[j] * sum2.a[j] % 1000000007;
      for (int j = 0; j < m; ++j)
        for (int k = 0; k < len; ++k)
          if (((1 << j) & k) == 0)
            w2.a[k] = (w2.a[k] - w2.a[k ^ (1 << j)]) % 1000000007;
    }
    for (int j = 0; j < len; ++j) ans.a[j] = (w1.a[j] + w2.a[j]) % 1000000007;
  }
  return ans;
}
int main() {
  scanf("%s", a + 1);
  int n = strlen(a + 1), w = 0;
  scanf("%d", &m);
  for (int i = 0, x; i < m; ++i) {
    for (int j = 0; j < 4; ++j) scanf("%d", &A[j][i]);
    scanf("%d", &x);
    w = w | (x << i);
  }
  for (int i = 1; i <= n; ++i) {
    if (a[i] == ')')
      nx[sk[tot--]] = i;
    else if (a[i] == '(')
      sk[++tot] = i;
  }
  len = 1 << m;
  hh ans = dfs(1, n);
  printf("%d", (ans.a[w] + 1000000007) % 1000000007);
}
