#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010

map<string, int> dict;

int decode(string s) {
  int res = 0;

  for (char t : s) {
    if (t == 'A') res |= 1 << 0;
    if (t == 'G') res |= 1 << 1;
    if (t == 'C') res |= 1 << 2;
    if (t == 'T') res |= 1 << 3;
  }

  return res;
}

vector<int> split(char *S) {
  vector<int> res;
  string cur;

  while(*S) {
    while(*S == ' ') S++;

    while(*S && *S != ' ') cur += *S++;

    debug(cur);
    debug(dict[cur]);

    if (cur.size()) {
      if (cur[0] == '[')
        res.push_back(-decode(cur));
      else
        res.push_back(dict[cur]);
    }

    cur = "";
  }

  return res;
}

ll dp[52][52][52][52];

int main(){
  int Na, Nt, Ng, Nc, M;
  char key[20], S[50][400];
  vector<int> rules[50];
  int sum[50][16] = {};

  cin >> Na >> Nt >> Ng >> Nc >> M;

  for (int i=0; i<M; i++) {
    scanf("%s", key);
    key[strlen(key)-1] = '\0';
    string str = key;
    dict[str] = i;
    fgets(S[i], 400, stdin);
    S[i][strlen(S[i])-1] = '\0';
    debug(S[i]);
  }

  for (int i=0; i<M; i++) {
    rules[i] = split(S[i]);
    debug(rules[i]);
  }

  for (int i=M-1; i>=0; i--) {
    for (int p : rules[i]) {
      if (p < 0) {
        sum[i][-p]++;
      } else {
        for (int j=0; j<16; j++) {
          sum[i][j] += sum[p][j];
        }
      }
    }
  }

  int len = 0;
  for (int i=0; i<16; i++)
    len += sum[0][i];

  debug(len);

  if (len != Na + Nt + Ng + Nc) {
    puts("0");
    return 0;
  }

  int r = 0;

  dp[0][0][0][0] = 1;

  for (int i=0; i<16; i++) {
    debug(sum[0][i]);
    for (int j=0; j<sum[0][i]; j++) {
      for (int a=Na; a>=0; a--) {
        for (int g=Ng; g>=0; g--) {
          for (int c=Nc; c>=0; c--) {
            int t = r - a - g - c;
            if (t < 0) continue;

            //cerr << a << g << c << t << " " << i << endl;
            //cerr << dp[a][g][c][t] << endl;

            if (i & (1 << 0)) dp[a+1][g][c][t] = (dp[a+1][g][c][t] + dp[a][g][c][t]) % mod;
            if (i & (1 << 1)) dp[a][g+1][c][t] = (dp[a][g+1][c][t] + dp[a][g][c][t]) % mod;
            if (i & (1 << 2)) dp[a][g][c+1][t] = (dp[a][g][c+1][t] + dp[a][g][c][t]) % mod;
            if (i & (1 << 3)) dp[a][g][c][t+1] = (dp[a][g][c][t+1] + dp[a][g][c][t]) % mod;
          }
        }
      }
      r++;
    }
  }

  cout << dp[Na][Ng][Nc][Nt] << endl;

  return 0;
}



Measured memory_bytes: 60352