# Original solution
table= {}
def solve(dict,key):
    global table
    if key in table:
    #    print('key=',key)
        return table[key]
    ret = []
    #print(key)
    for i in dict[key]:
        if i[0] == '[':
            ret.append(i[1:-1])
        else:
            ret += (solve(dict, i))
    if len(ret) > 200:
        for i in list(table.keys()):
            table[i]=''
            return ''
    table[key] = ret
    #print(key)
    return ret

Na, Nt, Ng, Nc = [int(i) for i in input().split()]
m = int(eval(input()))

dict = {}

for i in range(m):
    key, value = [i for i in input().split(': ')]
    if i == 0:
        aa = key
    value = value.split()
    dict[key] = value

lis = solve(dict, aa)
if len(lis)==Na+Nt+Ng+Nc:
    DP = [[[[0 for i in range(Ng+1)]for j in range(Nt+1)]for k in range(Na+1)]for l in range(len(lis)+1)]
    #print(DP[4])
    DP[0][0][0][0] = 1 
    #print(DP)
    for i in range(len(lis)):
        str = lis[i]
        for a in range(i+1):
            if a > Na:
                break
            for t in range(i-a+1):
                if t > Nt:
                    break
                for g in range(i-a-t+1):
                    if g > Ng:
                        break
#                    print(i,a,t,g)
                    if i-a-t-g > Nc:
                        continue
                    ans = DP[i][a][t][g] % 1000000007
 #                   print(ans)
                    for char in str:
#                        print(i,a,t,g)
                        if char == 'A':
                            if a+1 > Na:
                                continue
 #                           print(i,a,t,g)
                            DP[i+1][a+1][t][g] += ans
  #                          print(DP[i+1][a+1][t][g])
                        elif char == 'T':
                            if t+1 > Nt:
                                continue
                           # print(i)
                            DP[i+1][a][t+1][g] += ans
                        elif char == 'G':
                            if g+1 > Ng:
                                continue
                            #print(i)
                            DP[i+1][a][t][g+1] += ans
                        elif char == 'C':
                            if i-a-t-g+1> Nc:
                                continue
                            #print(i)
                            DP[i+1][a][t][g] += ans
#                        print(DP)
                    #DP[i][a][t][g] = ans % 1000000007

    print((DP[len(lis)][Na][Nt][Ng]%1000000007))
else:
    print((0))



# Optimized solution
table= {}
def solve(dict,key):
    global table
    if key in table:
    #    print('key=',key)
        return table[key]
    ret = []
    #print(key)
    for i in dict[key]:
        if i[0] == '[':
            ret.append(i[1:-1])
        else:
            ret += (solve(dict, i))
    if len(ret) > 200:
        for i in list(table.keys()):
            table[i]=''
            return ''
    table[key] = ret
    #print(key)
    return ret

Na, Nt, Ng, Nc = [int(i) for i in input().split()]
m = int(eval(input()))

dict = {}

for i in range(m):
    key, value = [i for i in input().split(': ')]
    if i == 0:
        aa = key
    value = value.split()
    dict[key] = value

lis = solve(dict, aa)
if len(lis)==Na+Nt+Ng+Nc:
    DP = [[[[0 for i in range(Nc+1)] for j in range(Ng+1)]for k in range(Nt+1)]for l in range(Na+1)]
    #DPN = [[[0 for j in range(Ng+1)]for k in range(Nt+1)]for l in range(Na+1)]
    #print(DP[4])
    DP[0][0][0][0] = 1
#    print(DPC)
    #print(DP)
    for i in range(len(lis)):
        hA=hT=hG=hC=False
        #print(i)
        for char in lis[i]:
            if char == 'A':
                hA = True
            elif char == 'G':
                hG  = True
            elif char == 'T':
                hT = True
            else:
                hC = True
#        print(hA,hT,hG,hC)
        for a in range(i+1):
            if a > Na:
                break
            for t in range(i-a+1):
                if t > Nt:
                    break
                for g in range(i-a-t+1):
                    if g > Ng:
                        break
#                    print(i,a,t,g)
                    c = i-a-t-g
                    if c > Nc:
                        continue
                    ans = DP[a][t][g][c] % 1000000007
#                    print(ans)
                    if hA and a+1 <= Na:
                        DP[a+1][t][g][c] += ans
  #                          print(DP[i+1][a+1][t][g])
                    if hT and t+1 <= Nt:
                           # print(i)
                        DP[a][t+1][g][c] += ans
                    if hG and g+1 <=  Ng:
                            #print(i)
                        DP[a][t][g+1][c] += ans
                    if hC and c+1 <= Nc:
                            #print(i)
                        DP[a][t][g][c+1] += ans
#        print(DPC)
                    #DP[i][a][t][g] = ans % 1000000007
        #DPC = DPN
        #DPN = [[[0 for j in range(Ng+1)]for k in range(Nt+1)]for l in range(Na+1)]
    print((DP[Na][Nt][Ng][Nc]%1000000007))
else:
    print((0))