#include "bits/stdc++.h"
#include<unordered_map>
#pragma warning(disable:4996)
using namespace std;

map<char, int>mp;
struct mark {
	bool isname;
	vector<bitset<4>>mays;
	string obname;
};
struct BigMark {
	string name;
	vector<mark>marks;
};



vector<BigMark>bms;
map<string, int>namemp;
vector<bitset<4>>getans(const int id) {
	vector<bitset<4>>anss;
	for (int i = 0; i < bms[id].marks.size(); ++i) {
		mark &amark(bms[id].marks[i]);
		if (amark.isname) {
			amark.mays=getans(namemp[amark.obname]);
			amark.isname = false;
		}
		anss.insert(anss.end(), amark.mays.begin(),amark.mays.end());
		if (anss.size() >= 201)throw(0);
	}
	return anss;
}

const int mod = 1000000007;
struct Mod {
public:
	int num;
	Mod() : num(0) { ; }
	Mod(long long int n) : num((n % mod + mod) % mod) { ; }
	Mod(int n) : num((n % mod + mod) % mod) { ; }
	operator int() { return num; }
};

Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator+(const Mod a, const long long int  b) { return b + a; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator*(const long long int a, const Mod b) { return Mod(a)*b; }
Mod operator*(const Mod a, const long long int b) { return Mod(b)*a; }
Mod operator*(const Mod a, const int b) { return Mod(b)*a; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator*=(Mod& a, const long long int &b) { return a = a * b; }
Mod operator^(const Mod a, const int n) {
	if (n == 0) return Mod(1);
	Mod res = (a * a) ^ (n / 2);
	if (n % 2) res = res * a;
	return res;
}
Mod mod_pow(const Mod a, const int n) {
	if (n == 0) return Mod(1);
	Mod res = mod_pow((a * a), (n / 2));
	if (n % 2) res = res * a;
	return res;
}
Mod inv(const Mod a) { return a ^ (mod - 2); }
Mod operator/(const Mod a, const Mod b) {
	assert(b.num != 0);
	return a * inv(b);
}
Mod operator/(const long long int a, const Mod b) {
	assert(b.num != 0);
	return Mod(a) * inv(b);
}
Mod operator/=(Mod &a, const Mod b) {
	assert(b.num != 0);
	return a = a * inv(b);
}

#define MAX_MOD_N 1024000

Mod fact[MAX_MOD_N], factinv[MAX_MOD_N];
void init() {
	fact[0] = Mod(1); factinv[0] = 1;
	for (int i = 0; i < MAX_MOD_N - 1; ++i) {
		fact[i + 1] = fact[i] * Mod(i + 1);
		factinv[i + 1] = factinv[i] / Mod(i + 1);
	}
}
Mod comb(const int a, const int b) {
	return fact[a] * factinv[b] * factinv[a - b];
}
Mod dp[201][52][52][52];

int main() {
	for (int i = 0; i < 51; ++i) {
		for (int j = 0; j < 51; ++j) {
			for (int k = 0; k < 51; ++k) {
				for (int l = 0; l < 201; ++l) {
					dp[i][j][k][l] = 0;
				}
			}
		}
	}
	mp['A'] = 0;
	mp['T'] = 1;
	mp['G'] = 2;
	mp['C'] = 3;
	vector<int>needs(4);
	cin >> needs[0] >> needs[1] >> needs[2] >> needs[3];
	int N; cin >> N;
	string dummy;
	getline(cin, dummy);
	for (int i = 0; i < N; ++i) {
		string st;
		getline(cin, st);
		st += ' ';
		int semi = st.find(':');
		string name = st.substr(0, semi);
		BigMark bm{ name, vector<mark>() };
		int from = semi + 2;
		while (1) {
			int space = st.find(' ',from);
			if (space == string::npos)break;
			string aname = st.substr(from, space - from);
			if (aname[0] == '[') {
				aname = string(aname.begin() + 1, aname.end() - 1);
				bitset<4>bs;
				for (int i = 0; i < aname.size(); ++i) {
					bs[mp[aname[i]]] = true;
				}
				mark amark{ false, {bs}, "" };
				bm.marks.push_back(amark);
			}
			else {
				mark amark{ true,{},aname };
				bm.marks.push_back(amark);
			}
			from = space + 1;
			if (from >= st.size())break;
		}
		bms.push_back(bm);
	}
	for (int i = 0; i < N; ++i) {
		namemp[bms[i].name] = i;
	}
	vector<bitset<4>>bits;
	try{
		bits = getans(0);
	}
	catch(...){
		cout << 0 << endl;
		return 0;
	};
	int sum = accumulate(needs.begin(), needs.end(), 0);
	if (bits.size() == sum) {

		dp[0][0][0][0]=1;
		for (int i = 0; i < sum; ++i) {
			for (int num0 = 0; num0 < 51; ++num0) {
				for (int num1 = 0; num1 < 51; ++num1) {
					for (int num2 = 0; num2 < 51; ++num2) {
						for (int use = 0; use < 4; ++use) {
							if (bits[i][use]) {
								if (use == 0) {
									dp[i + 1][num0 + 1][num1][num2] += dp[i][num0][num1][num2];
								}
								else if (use == 1) {
									dp[i + 1][num0][num1 + 1][num2] += dp[i][num0][num1][num2];

								}
								else if (use == 2) {
									dp[i + 1][num0][num1][num2 + 1] += dp[i][num0][num1][num2];

								}
								else {
									dp[i+1][num0][num1][num2] += dp[i][num0][num1][num2];
								}
							}
						}
					}
				}
			}
		}
		Mod ans = dp[bits.size()][needs[0]][needs[1]][needs[2]];
		cout << ans << endl;
	}
	else {
		cout << 0 << endl;
	}
	return 0;
}

Measured memory_bytes: 121656