#include <bits/stdc++.h>
using namespace std;
const int p = 1e9 + 7, len = 1 << 16;
char ch[100012];
int n, m, dp[502][100012], js, s[100012], tot, f[100012], val[100012];
inline void fwt_or(int x[], int mde) {
  for (int i = 2; i <= len; i <<= 1)
    for (int j = 0, stp = i >> 1; j < len; j += i)
      for (int k = j; k < j + stp; k++)
        (x[k + stp] += 1ll * mde * x[k] % p) %= p;
}
inline void fwt_and(int x[], int mde) {
  for (int i = 2; i <= len; i <<= 1)
    for (int j = 0, stp = i >> 1; j < len; j += i)
      for (int k = j; k < j + stp; k++)
        (x[k] += 1ll * mde * x[k + stp] % p) %= p;
}
int solve(int l, int r) {
  int ret = ++js, mid;
  if (l == r) {
    if (ch[l] == '?') {
      for (int i = 0; i < 4; i++) {
        int va = 0;
        for (int j = 0; j < 16; j++)
          if (j & (1 << i)) va |= 1 << j;
        dp[ret][va]++;
        va = 0;
        for (int j = 0; j < 16; j++)
          if (!(j & (1 << i))) va |= 1 << j;
        dp[ret][va]++;
      }
    } else if (ch[l] >= 'A' && ch[l] <= 'D') {
      int va = 0;
      for (int j = 0; j < 16; j++)
        if (j & (1 << (ch[l] - 'A'))) va |= 1 << j;
      dp[ret][va]++;
    } else {
      int va = 0;
      for (int j = 0; j < 16; j++)
        if (!(j & (1 << (ch[l] - 'a')))) va |= 1 << j;
      dp[ret][va]++;
    }
    return ret;
  }
  tot = 0;
  for (int i = l; i <= r; i++) {
    if (ch[i] == '(')
      s[++tot] = i;
    else if (ch[i] == ')')
      tot--;
    else {
      if (!tot) {
        mid = i;
        break;
      }
    }
  }
  int ls = solve(l + 1, mid - 2), rs = solve(mid + 2, r - 1);
  if (ch[mid] == '?') {
    fwt_or(dp[ls], 1);
    fwt_or(dp[rs], 1);
    for (int i = 0; i < len; i++) dp[ret][i] = 1ll * dp[ls][i] * dp[rs][i] % p;
    fwt_or(dp[ret], p - 1);
    fwt_or(dp[ls], p - 1);
    fwt_or(dp[rs], p - 1);
    fwt_and(dp[ls], 1);
    fwt_and(dp[rs], 1);
    for (int i = 0; i < len; i++) f[i] = 1ll * dp[ls][i] * dp[rs][i] % p;
    fwt_and(f, p - 1);
    fwt_and(dp[ls], p - 1);
    fwt_and(dp[rs], p - 1);
    for (int i = 0; i < len; i++) (dp[ret][i] += f[i]) %= p;
  } else if (ch[mid] == '|') {
    fwt_or(dp[ls], 1);
    fwt_or(dp[rs], 1);
    for (int i = 0; i < len; i++) dp[ret][i] = 1ll * dp[ls][i] * dp[rs][i] % p;
    fwt_or(dp[ret], p - 1);
    fwt_or(dp[ls], p - 1);
    fwt_or(dp[rs], p - 1);
  } else {
    fwt_and(dp[ls], 1);
    fwt_and(dp[rs], 1);
    for (int i = 0; i < len; i++) dp[ret][i] = 1ll * dp[ls][i] * dp[rs][i] % p;
    fwt_and(dp[ret], p - 1);
    fwt_and(dp[ls], p - 1);
    fwt_and(dp[rs], p - 1);
  }
  return ret;
}
int main() {
  scanf("%s", ch + 1);
  n = strlen(ch + 1);
  int rt = solve(1, n);
  memset(val, -1, sizeof(val));
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    int a, b, c, d, w;
    scanf("%d%d%d%d%d", &a, &b, &c, &d, &w);
    val[a | (b << 1) | (c << 2) | (d << 3)] = w;
  }
  int ans = 0;
  for (int i = 0; i < len; i++) {
    bool flag = true;
    for (int j = 0; j < 16; j++)
      if (val[j] != -1) {
        if (((i >> j) & 1) != val[j]) {
          flag = false;
          break;
        }
      }
    if (flag) ans = (ans + dp[rt][i]) % p;
  }
  printf("%d\n", ans);
}
