// Arup Guha
// Solution to 2017 USACO March Contest Problem: Cow Basic
// 11/12/2017

import java.util.*;
import java.io.*;

public class cowbasic {

	final public static long MOD = 1000000007L;

	public static ArrayList<String> lines;
	public static HashMap<String,Integer> varMap;
	public static int numVars;
	public static int[] type;
	public static int[] match;

	public static void main(String[] args) throws Exception {

		Scanner stdin = new Scanner(new File("cowbasic.in"));

		// Read in each line.
		lines = new ArrayList<String>();
		while (stdin.hasNext()) lines.add(stdin.nextLine());

		// Make a map for each variable name.
		varMap = new HashMap<String,Integer>();
		numVars = fillVarMap();

		// Store the type of each statement.
		type = new int[lines.size()-1];
		for (int i=0; i<type.length; i++)
			type[i] = getType(lines.get(i));

		// Find who matches each loop.
		match = new int[type.length];
		for (int i=0; i<type.length; i++) {
			if (type[i] == 1) {
				int sum = 1, j=i+1;
				while (sum>0) {
					sum += type[j];
					j++;
				}
				match[i] = j-1;
			}
		}

		// Start with our initial matrix - just set const to 1.
		long[][] mat = new long[1][numVars+1];
		mat[0][numVars] = 1;

		int curLine = 0;

		// Go through each line.
		while (curLine<lines.size()-1) {

			// Process this assignment statement.
			if (type[curLine] == 0) {
				long[][] tmp = getMat(curLine);
				mat = mult(mat, tmp);
			}

			// Moo loop - just find start and end.
			else {
				int curCnt = 1;
				int saveCurLine = curLine;
				while (curCnt != 0) {
					curCnt += type[curLine+1];
					curLine++;
				}
				long[][] tmp = getMat(saveCurLine, curLine);
				mat = mult(mat, tmp);
			}

			curLine++;
		}

		StringTokenizer tok = new StringTokenizer(lines.get(lines.size()-1));
		tok.nextToken();
		int ret = varMap.get(tok.nextToken());

		// Ta da!
		PrintWriter out = new PrintWriter(new FileWriter("cowbasic.out"));
		out.println(mat[mat.length-1][ret]);
		out.close();
		stdin.close();
	}

	// Fills in the variable map and returns the number of variables.
	public static int fillVarMap() {
		int ID = 0;
		for (int i=0; i<lines.size()-1; i++) {
			StringTokenizer tok = new StringTokenizer(lines.get(i));
			String var = tok.nextToken();
			if (!tok.hasMoreTokens()) continue;
			String eq = tok.nextToken();
			if (eq.equals("=")) {
				if (!varMap.containsKey(var))
					varMap.put(var, ID++);
			}
		}
		return ID;
	}

	public static int getType(String s) {
		if (s.contains("{")) return 1;
		if (s.contains("}")) return -1;
		return 0;
	}

	public static long[][] getMat(int i) {

		StringTokenizer tok = new StringTokenizer(lines.get(i));

		// Set up the return matrix.
		long[][] res = new long[numVars+1][numVars+1];

		// Get the column of this matrix.
		String str = tok.nextToken();
		//System.out.println("var is "+str);
		int col = varMap.get(str);
		tok.nextToken();

		// Go through each token.
		while (tok.hasMoreTokens()) {

			// We can just skip these.
			String next = tok.nextToken();
			if (next.equals("(")) continue;
			if (next.equals(")")) continue;
			if (next.equals("+")) continue;

			// Variable contribution.
			if (varMap.containsKey(next))
				res[varMap.get(next)][col]++;

			// Add to my constant.
			else
				res[res.length-1][col] += Long.parseLong(next);
		}

		// Keeps all other values the same.
		for (int z=0; z<=numVars; z++)
			if (z != col)
				res[z][z] = 1;

		return res;
	}

	public static long[][] getMat(int i, int j) {

		if (type[i] == 1) {
			StringTokenizer tok = new StringTokenizer(lines.get(i));
			long exp = Long.parseLong(tok.nextToken());

			// Do this loop.
			long[][] tmp = getMat(i+1,match[i]-1);
			tmp = expo(tmp, exp);
			if (match[i] == j) return tmp;

			// Get the rest.
			long[][] next = getMat(match[i]+1,j);
			return mult(tmp, next);
		}

		// First statement isn't a loop, separate it out and recurse.
		else {
			long[][] tmp = getMat(i);
			if (i == j) return tmp;
			long[][] tmp2 = getMat(i+1, j);
			return mult(tmp, tmp2);
		}
	}

	public static long[][] mult(long[][] a, long[][] b) {
		long[][] res = new long[a.length][b[0].length];
		for (int i=0; i<a.length; i++)
			for (int j=0; j<b[0].length; j++)
				for (int k=0; k<b.length; k++)
					res[i][j] = (res[i][j] + a[i][k]*b[k][j])%MOD;
		return res;
	}

	public static long[][] expo(long[][] a, long exp) {
		if (exp == 1) return  a;
		if (exp%2 == 0) {
			long[][] tmp = expo(a, exp/2);
			return mult(tmp, tmp);
		}
		long[][] tmp = expo(a, exp-1);
		return mult(tmp, a);
	}
}