Title: Life Game

Taro went to a toy store to buy a life game made by Aizu Hobby Company. The game involves using a board with marked squares and a roulette. The board has a start and a goal square, connected in a continuous sequence. Initially, a piece is placed on the start square, and its movement is determined by the number spun on the roulette. Some squares provide money or trigger events that change the piece's position. The winner is determined by the amount of money held when the piece reaches the goal.

The interesting aspect of this game is that the size of the roulette outcomes, the number of squares to the goal, and the arrangement of event squares vary for each package. These details are written on the package, allowing players to check them. Taro wants to choose the game that yields the most money on average. Your task is to help Taro select the game.

The roulette is divided into X equal parts, each with values V1, V2, ..., VX. The board has squares numbered from 0 to Y, connected in order. There are Z special squares called event squares, each denoted by Ni. These event squares have 3 types (Ei) with specific actions based on a value (Ai).

The initial money is 0 yen, starting from square 0, and the goal is to reach square Y. There are no events at the start and goal, and multiple events do not overlap on a single square. If the money becomes less than 0, it is considered as 0.

The expected money from a particular life game can be calculated. The input includes roulette and board information, and the program should output the expected money at the goal for each dataset.

# Buggy version:
def solve(X, Y, Z, V, E, A):
    dp = [[0.0] * 6001 for _ in range(Y + max(V) + 1)]
    dp[0][0] = 1.0
    for i in range(Y):
        for j in range(5001):
            if dp[i][j] <= 0.0:
                continue
            for k in V:
                t = i + k
                if t > Y:
                    dp[Y][j] += dp[i][j]/X
                elif E[t] == 1:
                    dp[min(Y, t+A[t])][j] += dp[i][j]/X
                else:
                    dp[t][max(0, j+A[t])] += dp[i][j]/X

    s = 0
    for i in range(5001):
        if dp[Y][i] <= 0.0:
            continue
        s += i * dp[Y][i]
    print(int(s))

if __name__ == "__main__":
    import sys

    while True:
        X, Y, Z = map(int, input().split())
        if X | Y | Z == 0:
            sys.exit()
        V = map(int, input().split())
        E = [0] * 100
        A = [0] * 100
        for _ in range(Z):
            n, e, a = map(int, input().split())
            E[n] = e
            if e == 3:
                A[n] = -a
            else:
                A[n] = a
        solve(X, Y, Z, V, E, A)

# Fixed version:
def solve(X, Y, Z, V, E, A):
    dp = [[0.0] * 6001 for _ in range(Y + 11)]
    dp[0][0] = 1.0
    for i in range(Y):
        for j in range(5001):
            if dp[i][j] <= 0.0:
                continue
            for k in V:
                t = i + k
                if t > Y:
                    dp[Y][j] += dp[i][j]/X
                elif E[t] == 1:
                    dp[min(Y, t+A[t])][j] += dp[i][j]/X
                else:
                    dp[t][max(0, j+A[t])] += dp[i][j]/X

    s = 0
    for i in range(5001):
        s += i * dp[Y][i]
    print(int(s))

if __name__ == "__main__":

    while True:
        X, Y, Z = map(int, input().split())
        if X | Y | Z == 0:
            break
        V = list(map(int, input().split()))
        E = [0] * 100
        A = [0] * 100
        for _ in range(Z):
            n, e, a = map(int, input().split())
            E[n] = e
            if e == 3:
                A[n] = -a
            else:
                A[n] = a
        solve(X, Y, Z, V, E, A)