Problem p03082 (python)

Fixed submission:
import math
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")

N, X = list(map(int, sys.stdin.readline().split()))
S = list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7


def mod_invs(max, mod):
    """
    逆元のリスト 0 から max まで
    :param max:
    :param mod:
    :return:
    """
    invs = [1] * (max + 1)
    for x in range(2, max + 1):
        invs[x] = (-(mod // x) * invs[mod % x]) % mod
    return invs


S.sort()

invs = mod_invs(N, MOD)
dp = np.arange(X + 1) % S[0]
for i in range(1, N):
    dp += dp[np.arange(X + 1) % S[i]] * invs[i]
    dp %= MOD
print(int(dp[X] * math.factorial(N) % MOD * invs[N] % MOD))

Buggy submission (verdict: Runtime Error):
import math
import os
import sys
from functools import lru_cache

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")

N, X = list(map(int, sys.stdin.readline().split()))
S = list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7

S.sort()


# @lru_cache(maxsize=None)
# def test2(x, si):
#     # S[si] を最初にとったとき、最終的な数値の期待値
#     if si == 0:
#         return x % S[si]
#
#     ret = 0
#     for j in range(si):
#         ret += test2(x % S[si], si=j)
#     return ret / si
#
#
# dp = np.zeros((N, X + 1))
# dp[0] = np.arange(X + 1) % S[0]
# for i in range(1, N):
#     for x in range(X + 1):
#         dp[i][x] = np.mean(dp[:i, x % S[i]])
# print(dp[:, X])
# print(dp[:, X].sum() * math.factorial(N) / N % MOD)


# dp = np.arange(X + 1) % S[0]
# for i in range(1, N):
#     dp += dp[np.arange(X + 1) % S[i]] / i
# print(round(dp[X] * math.factorial(N) / N % MOD))


@lru_cache(maxsize=None)
def rec(x, i):
    if i == 0:
        return x % S[0]
    ret = 0
    for j in range(i):
        ret += rec(x % S[i], j)
    return ret / i


recsum = sum([rec(X, i) for i in range(N)])
print(round(recsum * math.factorial(N) / N % MOD))

Buggy submission (verdict: Runtime Error):
import math
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")

N, X = list(map(int, sys.stdin.readline().split()))
S = list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7

S.sort()


# @lru_cache(maxsize=None)
# def test2(x, si):
#     # S[si] を最初にとったとき、最終的な数値の期待値
#     if si == 0:
#         return x % S[si]
#
#     ret = 0
#     for j in range(si):
#         ret += test2(x % S[si], si=j)
#     return ret / si


# dp = np.zeros((N, X + 1))
# dp[0] = np.arange(X + 1) % S[0]
# for i in range(1, N):
#     for x in range(X + 1):
#         dp[i][x] = np.mean(dp[:i, x % S[i]])
# print(dp[:, X].sum() * math.factorial(N) / N % MOD)


dp = np.arange(X + 1) % S[0]
for i in range(1, N):
    dp += dp[np.arange(X + 1) % S[i]] / i
print(int(dp[X] * math.factorial(N) / N % MOD))

Buggy submission (verdict: Runtime Error):
import math
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")

N, X = list(map(int, sys.stdin.readline().split()))
S = list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7

S.sort()


# @lru_cache(maxsize=None)
# def test2(x, si):
#     # S[si] を最初にとったとき、最終的な数値の期待値
#     if si == 0:
#         return x % S[si]
# 
#     ret = 0
#     for j in range(si):
#         ret += test2(x % S[si], si=j)
#     return ret / si


# dp = np.zeros((N, X + 1))
# dp[0] = np.arange(X + 1) % S[0]
# for i in range(1, N):
#     for x in range(X + 1):
#         dp[i][x] = np.mean(dp[:i, x % S[i]])
# print(dp[:, X].sum() * math.factorial(N) / N % MOD)


dp = np.arange(X + 1) % S[0]
for i in range(1, N):
    dp += dp[np.arange(X + 1) % S[i]] / i
print(dp[X] * math.factorial(N) / N % MOD)