Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import defaultdict
def input():
	return sys.stdin.readline()[:-1]

MOD = 1e9+7
n, x = map(int, input().split())
s = list(map(int, input().split()))
s.sort(reverse=True)
dp = defaultdict(int)
dp[x] = 1
for i in range(n):
	dpp = defaultdict(int)
	for j in range(x+1):
		dpp[j] += (n-1-i) * dp[j]
		dpp[j%s[i]] += dp[j]
	for j in range(x+1):
		dpp[j] %= MOD
	dp = dpp
#print(dp)
ans = 0
for j in range(x+1):
	ans += dp[j] * j
	ans %= MOD
print(int(ans))

Fixed submission:
import sys
from collections import defaultdict
def input():
	return sys.stdin.readline()[:-1]

MOD = 1e9+7
n, x = map(int, input().split())
s = list(map(int, input().split()))
s.sort(reverse=True)
dp = defaultdict(int)
dp[x] = 1
for i in range(n):
	ss = s[i]
	dpp = defaultdict(int)
	for key, value in dp.items():
		dpp[key] += (n-1-i) * value
		dpp[key%ss] += value
	for key in dpp.keys():
		dpp[key] %= MOD
	dp = dpp
ans = 0
for key, value in dp.items():
	ans += value * key
	ans %= MOD
print(int(ans))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import defaultdict
def input():
	return sys.stdin.readline()[:-1]

MOD = 1e9+7
n, x = map(int, input().split())
s = list(map(int, input().split()))
s.sort(reverse=True)
dp = defaultdict(int)
dp[x] = 1
for i in range(n):
	dpp = defaultdict(int)
	for j in range(x+1):
		dpp[j] += (n-1-i) * dp[j]
		dpp[j%s[i]] += dp[j]
	for j in range(x+1):
		dpp[j] %= MOD
	dp = dpp
#print(dp)
ans = 0
for j in range(x+1):
	ans += dp[j] * j
	ans %= MOD
print(int(ans))

Fixed submission:
import sys
from collections import defaultdict
def input():
	return sys.stdin.readline()[:-1]

MOD = 1e9+7
n, x = map(int, input().split())
s = list(map(int, input().split()))
s.sort(reverse=True)
dp = defaultdict(int)
dp[x] = 1
for i in range(n):
	ss = s[i]
	dpp = defaultdict(int)
	for key, value in dp.items():
		dpp[key] += (n-1-i) * value
		dpp[key%ss] += value
	for key in dpp.keys():
		dpp[key] %= MOD
	dp = dpp
ans = 0
for key, value in dp.items():
	ans += value * key
	ans %= MOD
print(int(ans))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input():
	return sys.stdin.readline()[:-1]

MOD = 1e9+7
n, x = map(int, input().split())
s = list(map(int, input().split()))
s.sort(reverse=True)
dp = [[0 for _ in range(x+1)] for _ in range(n+1)]
dp[0][x] = 1
for i in range(n):
	for j in range(x+1):
		dp[i+1][j] += (n-1-i) * dp[i][j]
		dp[i+1][j%s[i]] += dp[i][j]
	for j in range(x+1):
		dp[i+1][j] %= MOD
#print(dp)
ans = 0
for j in range(x+1):
	ans += dp[n][j] * j
	ans %= MOD
print(int(ans))

Fixed submission:
import sys
from collections import defaultdict
def input():
	return sys.stdin.readline()[:-1]

MOD = 1e9+7
n, x = map(int, input().split())
s = list(map(int, input().split()))
s.sort(reverse=True)
dp = defaultdict(int)
dp[x] = 1
for i in range(n):
	ss = s[i]
	dpp = defaultdict(int)
	for key, value in dp.items():
		dpp[key] += (n-1-i) * value
		dpp[key%ss] += value
	for key in dpp.keys():
		dpp[key] %= MOD
	dp = dpp
ans = 0
for key, value in dp.items():
	ans += value * key
	ans %= MOD
print(int(ans))

Buggy python submission (verdict: Time Limit Exceeded):
MOD = 1e9+7
n, x = map(int, input().split())
s = list(map(int, input().split()))
s.sort(reverse=True)
dp = [[0 for _ in range(x+1)] for _ in range(n+1)]
dp[0][x] = 1
for i in range(n):
	for j in range(x+1):
		dp[i+1][j] += (n-1-i) * dp[i][j]
		dp[i+1][j%s[i]] += dp[i][j]
	for j in range(x+1):
		dp[i+1][j] %= MOD
#print(dp)
ans = 0
for j in range(x+1):
	ans += dp[n][j] * j
	ans %= MOD
print(int(ans))

Fixed submission:
import sys
from collections import defaultdict
def input():
	return sys.stdin.readline()[:-1]

MOD = 1e9+7
n, x = map(int, input().split())
s = list(map(int, input().split()))
s.sort(reverse=True)
dp = defaultdict(int)
dp[x] = 1
for i in range(n):
	ss = s[i]
	dpp = defaultdict(int)
	for key, value in dp.items():
		dpp[key] += (n-1-i) * value
		dpp[key%ss] += value
	for key in dpp.keys():
		dpp[key] %= MOD
	dp = dpp
ans = 0
for key, value in dp.items():
	ans += value * key
	ans %= MOD
print(int(ans))

Buggy python submission (verdict: Time Limit Exceeded):
MOD = 1e9+7
n, x = map(int, input().split())
s = list(map(int, input().split()))
s.sort(reverse=True)
dp = [[0 for _ in range(x+1)] for _ in range(n+1)]
dp[0][x] = 1
for i in range(n):
	for j in range(x+1):
		dp[i+1][j] += (n-1-i) * dp[i][j]
		dp[i+1][j%s[i]] += dp[i][j]
	for j in range(x+1):
		dp[i+1][j] %= MOD
#print(dp)
ans = 0
for j in range(x+1):
	ans += dp[n][j] * j
	ans %= MOD
print(int(ans))

Fixed submission:
import sys
from collections import defaultdict
def input():
	return sys.stdin.readline()[:-1]

MOD = 1e9+7
n, x = map(int, input().split())
s = list(map(int, input().split()))
s.sort(reverse=True)
dp = defaultdict(int)
dp[x] = 1
for i in range(n):
	ss = s[i]
	dpp = defaultdict(int)
	for key, value in dp.items():
		dpp[key] += (n-1-i) * value
		dpp[key%ss] += value
	for key in dpp.keys():
		dpp[key] %= MOD
	dp = dpp
ans = 0
for key, value in dp.items():
	ans += value * key
	ans %= MOD
print(int(ans))