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

def main():
    N, X = list(map(int, input().split()))
    S = list(map(int, input().split()))
    S = sorted(S, reverse=True)

    dp = [0]*(X+1)
    dp[X] = 1
    for i in range(N):
        new = [0] * (X+1)
        for x in range(X+1):
            xx = x % S[i]
            new[x] += dp[x] * (N-i-1)
            new[x] %= MOD
            new[xx] += dp[x]
            new[xx] %= MOD
        dp = new

    total = 0
    for x in range(X):
        total += dp[x] * x
        total %= MOD
    print(total)

main()


Fixed submission:
from collections import defaultdict as dd
MOD = 10**9 + 7

def main():
    N, X = list(map(int, input().split()))
    S = list(map(int, input().split()))
    S = sorted(S, reverse=True)

    dp = dd(int)
    dp[X] = 1
    for i in range(N):
        new = dd(int)
        for x in dp:
            xx = x % S[i]
            new[x] += dp[x] * (N-i-1)
            new[x] %= MOD
            new[xx] += dp[x]
            new[xx] %= MOD
        dp = new

    total = 0
    for x in dp:
        total += dp[x] * x
        total %= MOD
    print(total)

main()

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

def main():
    N, X = list(map(int, input().split()))
    S = list(map(int, input().split()))
    S = sorted(S, reverse=True)

    def solve(start, num, x):
        if start == N-1:
            return (num * (x % S[-1])) % MOD
        total = 0
        for i in range(start, N-1):
            total = (total + solve(i+1, num, (x % S[i]))) % MOD
            num = (num * (N-i-1)) % MOD
        total = (total + num * (x % S[N-1])) % MOD
        return total
    total = 0
    num = 1
    for i in range(N-1):
        total += solve(i+1, num, (X % S[i]) % MOD)
        num *= N-i-1
    total = (total + num * (X % S[-1])) % MOD
    print(total)

main()


Fixed submission:
from collections import defaultdict as dd
MOD = 10**9 + 7

def main():
    N, X = list(map(int, input().split()))
    S = list(map(int, input().split()))
    S = sorted(S, reverse=True)

    dp = dd(int)
    dp[X] = 1
    for i in range(N):
        new = dd(int)
        for x in dp:
            xx = x % S[i]
            new[x] += dp[x] * (N-i-1)
            new[x] %= MOD
            new[xx] += dp[x]
            new[xx] %= MOD
        dp = new

    total = 0
    for x in dp:
        total += dp[x] * x
        total %= MOD
    print(total)

main()

Buggy python submission (verdict: Runtime Error):
MOD = 10**9 + 7

def gen_candidate(N):
    for i in range(1 << (N-1)):
        bit = bin(i)[2:]
        bit = '0'*(N-1 - len(bit)) + bit
        yield bit+'1'

def main():
    N, X = list(map(int, input().split()))
    S = list(map(int, input().split()))
    S = sorted(S, reverse=True)
    total = 0
    for bit in gen_candidate(N):
        x = X
        for j, char in enumerate(bit):
            if int(char):
                x = x % S[j]
        num = 1
        num_one = 0
        num_zero = 0
        for b in reversed(bit):
            if int(b):
                num_one += 1
            else:
                num *= num_one + num_zero
                num_zero += 1
        total = (total + x * num) % MOD
    print(total)

main()


Fixed submission:
from collections import defaultdict as dd
MOD = 10**9 + 7

def main():
    N, X = list(map(int, input().split()))
    S = list(map(int, input().split()))
    S = sorted(S, reverse=True)

    dp = dd(int)
    dp[X] = 1
    for i in range(N):
        new = dd(int)
        for x in dp:
            xx = x % S[i]
            new[x] += dp[x] * (N-i-1)
            new[x] %= MOD
            new[xx] += dp[x]
            new[xx] %= MOD
        dp = new

    total = 0
    for x in dp:
        total += dp[x] * x
        total %= MOD
    print(total)

main()