import functools, sys sys.setrecursionlimit(10**5) MOD = 10**9+7 N, P, S, C = map(int, input().split()) probs = [] for _ in range(P): t, g, s, c, b = input().split() probs.append((int(t), g, int(s), int(c), int(b))) # number of ways to go from (s1,c1) in room A to (s2,c2) in room A in exactly d steps @functools.lru_cache(maxsize=None) def ways(s1, c1, s2, c2, d): if d <= 1: if abs(s1-s2) > d or abs(c1-c2) > d: return 0 ds = 1 if s1 == s2 else (s1 if s1 > s2 else S-s1) dc = 1 if c1 == c2 else (c1 if c1 > c2 else C-c1) return ds*dc dd = 1 if (d&1) else d//2 tot = 0 for sm in range(S+1): for cm in range(C+1): tot += ways(s1, c1, sm, cm, dd)*ways(sm, cm, s2, c2, d-dd) return tot%MOD # number of ways to complete a solution given that (s,c) are in room A at time t # and i is the index of the next challenge problem to do @functools.lru_cache(maxsize=None) def solve(s, c, i, t): if i == P: return ways(s, c, S, C, N-t) tot = 0 for ss in range(S+1): for cc in range(C+1): if probs[i][1] == "A": hs, hc = ss, cc else: hs, hc = S-ss, C-cc if hs >= probs[i][2] and hc >= probs[i][3] and hs+hc >= probs[i][4]: tot += ways(s, c, ss, cc, probs[i][0]-t)*solve(ss, cc, i+1, probs[i][0]) return tot%MOD print(solve(S, C, 0, 1))