Buggy python submission (verdict: Time Limit Exceeded):
from itertools import accumulate
mod = 10 ** 9 + 7

N, C = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

memo = []
for p in range(C + 1):
    tmp = [pow(i, p, mod) for i in range(401)]
    memo.append(tuple(accumulate(tmp)))


dp = [[0] * (C + 1) for _ in range(N + 1)]
dp[0][0] = 1
for i, (a, b) in enumerate(zip(A, B)):
    for j in range(C + 1):  # 累計でj個配ったことにする
        for k in range(j + 1):  # i番目の子にk個あげる
            dp[i + 1][j] += dp[i][k] * (memo[j-k][b] - memo[j-k][a-1])
            dp[i + 1][j] %= mod

print(dp[N][C])

Fixed submission:
from itertools import accumulate
mod = 10 ** 9 + 7

N, C = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

memo = []
for p in range(C + 1):
    tmp = [pow(i, p, mod) for i in range(401)]
    memo.append(tuple(accumulate(tmp)))


dp = [[0] * (C + 1) for _ in range(N + 1)]
dp[0][0] = 1
for i, (a, b) in enumerate(zip(A, B)):
    for j in range(C + 1):  # 累計でj個配ったことにする
        for k in range(j + 1):  # i番目の子にk個あげる
            dp[i + 1][j] += dp[i][k] * (memo[j-k][b] - memo[j-k][a-1])
            dp[i + 1][j] %= mod

print(dp[N][C])

Buggy python submission (verdict: Time Limit Exceeded):
mod = 10 ** 9 + 7
U = 400
U = 10

N, C = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))


memo = []
for a, b in zip(A, B):
    tmp = []
    for p in range(C + 1):
        val = 0
        for i in range(a, b + 1):
            val += pow(i, p, mod)
            val %= mod
        tmp.append(val)
    memo.append(tmp)


dp = [[0] * (C + 1) for _ in range(N + 1)]
dp[0][0] = 1
for i in range(N):  # i番目の子に着目
    for j in range(C + 1):  # 累計でj個配ったことにする
        for k in range(j + 1):  # i番目の子にk個あげる
            dp[i + 1][j] += dp[i][k] * memo[i][j - k]
            dp[i + 1][j] %= mod

print(dp[N][C])

Fixed submission:
from itertools import accumulate
mod = 10 ** 9 + 7

N, C = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

memo = []
for p in range(C + 1):
    tmp = [pow(i, p, mod) for i in range(401)]
    memo.append(tuple(accumulate(tmp)))


dp = [[0] * (C + 1) for _ in range(N + 1)]
dp[0][0] = 1
for i, (a, b) in enumerate(zip(A, B)):
    for j in range(C + 1):  # 累計でj個配ったことにする
        for k in range(j + 1):  # i番目の子にk個あげる
            dp[i + 1][j] += dp[i][k] * (memo[j-k][b] - memo[j-k][a-1])
            dp[i + 1][j] %= mod

print(dp[N][C])

Buggy python submission (verdict: Time Limit Exceeded):
mod = 10 ** 9 + 7
U = 400
U = 10

N, C = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))


memo = []
for a, b in zip(A, B):
    tmp = []
    for p in range(C + 1):
        val = 0
        for i in range(a, b + 1):
            val += pow(i, p, mod)
            val %= mod
        tmp.append(val)
    memo.append(tmp)


dp = [[0] * (C + 1) for _ in range(N + 1)]
dp[0][0] = 1
for i in range(N):  # i番目の子に着目
    for j in range(C + 1):  # 累計でj個配ったことにする
        for k in range(j + 1):  # i番目の子にk個あげる
            dp[i + 1][j] += dp[i][k] * memo[i][j - k]
            dp[i + 1][j] %= mod

print(dp[N][C])

Fixed submission:
from itertools import accumulate
mod = 10 ** 9 + 7

N, C = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

memo = []
for p in range(C + 1):
    tmp = [pow(i, p, mod) for i in range(401)]
    memo.append(tuple(accumulate(tmp)))


dp = [[0] * (C + 1) for _ in range(N + 1)]
dp[0][0] = 1
for i, (a, b) in enumerate(zip(A, B)):
    for j in range(C + 1):  # 累計でj個配ったことにする
        for k in range(j + 1):  # i番目の子にk個あげる
            dp[i + 1][j] += dp[i][k] * (memo[j-k][b] - memo[j-k][a-1])
            dp[i + 1][j] %= mod

print(dp[N][C])