Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n,x = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
mod = 10**9+7
rng = 300
fctr = [1]
finv = [1]
for i in range(1,rng):
  fctr.append(fctr[-1]*i%mod)
def inv(a):
  return pow(a,mod-2,mod)
dp = [{} for i in range(n+1)]
dp[0][x] = 1
for i in range(n):
  for j in range(i+1,n+1):
    for k in dp[i].keys():
      num = dp[i][k]
      k %= a[j-1]
      if k in dp[j]:
        dp[j][k] = (dp[j][k]+num*fctr[n-i-1]*inv(fctr[n-j]))%mod
      else:
        dp[j][k] = (num*fctr[n-i-1]*inv(fctr[n-j]))%mod
ans = 0
for i,x in dp[n].items():
  ans = (ans+i*x)%mod
print(ans)

Fixed submission:
from collections import defaultdict
n,x = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
mod = 10**9+7
dp = [defaultdict(int) for i in range(n+1)]
dp[0][x] = 1
for i in range(1,n+1):
  for j in dp[i-1].keys():
    k = j%a[i-1]
    dp[i][j] = (dp[i][j]+dp[i-1][j]*(n-i))%mod
    dp[i][k] = (dp[i][k]+dp[i-1][j])%mod
ans = 0
for i,x in dp[n].items():
  ans = (ans+i*x)%mod
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n,x = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
ls = []
for i in range(n):
  t = x
  for j in range(i+1)[::-1]:
    t %= a[j]
  ls.append(t)
mod = 10**9+7
rng = 300
fctr = [1]
finv = [1]
for i in range(1,rng):
  fctr.append(fctr[-1]*i%mod)
def inv(a):
  return pow(a,mod-2,mod)
dp = [{} for i in range(n+1)]
dp[0][x] = 1
for i in range(n):
  for j in range(i+1,n+1):
    for k in dp[i].keys():
      num = dp[i][k]
      k %= a[j-1]
      if k in dp[j]:
        dp[j][k] = (dp[j][k]+num*fctr[n-i-1]*inv(fctr[n-j]))%mod
      else:
        dp[j][k] = (num*fctr[n-i-1]*inv(fctr[n-j]))%mod
ans = 0
for i,x in dp[n].items():
  ans = (ans+i*x)%mod
print(ans)

Fixed submission:
from collections import defaultdict
n,x = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
mod = 10**9+7
dp = [defaultdict(int) for i in range(n+1)]
dp[0][x] = 1
for i in range(1,n+1):
  for j in dp[i-1].keys():
    k = j%a[i-1]
    dp[i][j] = (dp[i][j]+dp[i-1][j]*(n-i))%mod
    dp[i][k] = (dp[i][k]+dp[i-1][j])%mod
ans = 0
for i,x in dp[n].items():
  ans = (ans+i*x)%mod
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n,x = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
ls = []
for i in range(n):
  t = x
  for j in range(i+1)[::-1]:
    t %= a[j]
  ls.append(t)
mod = 10**9+7
rng = 300
fctr = [1]
finv = [1]
for i in range(1,rng):
  fctr.append(fctr[-1]*i%mod)
def inv(a):
  return pow(a,mod-2,mod)
dp = [defaultdict(int) for i in range(n+1)]
dp[0][x] = 1
for i in range(n):
  for j in range(i+1,n+1):
    for k in dp[i].keys():
      num = dp[i][k]
      k %= a[j-1]
      dp[j][k] = (dp[j][k]+num*fctr[n-i-1]*inv(fctr[n-j]))%mod
ans = 0
for i,x in dp[n].items():
  ans = (ans+i*x)%mod
print(ans)

Fixed submission:
from collections import defaultdict
n,x = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
mod = 10**9+7
dp = [defaultdict(int) for i in range(n+1)]
dp[0][x] = 1
for i in range(1,n+1):
  for j in dp[i-1].keys():
    k = j%a[i-1]
    dp[i][j] = (dp[i][j]+dp[i-1][j]*(n-i))%mod
    dp[i][k] = (dp[i][k]+dp[i-1][j])%mod
ans = 0
for i,x in dp[n].items():
  ans = (ans+i*x)%mod
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n,x = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
ls = []
for i in range(n):
  t = x
  for j in range(i+1)[::-1]:
    t %= a[j]
  ls.append(t)
mod = 10**9+7
rng = 300
fctr = [1]
finv = [1]
for i in range(1,rng):
  fctr.append(fctr[-1]*i%mod)
def inv(a):
  return pow(a,mod-2,mod)
dp = [defaultdict(int) for i in range(n+1)]
dp[0][x] = 1
for i in range(n):
  for j in range(i+1,n+1):
    for k in dp[i].keys():
      num = dp[i][k]
      k %= a[j-1]
      dp[j][k] = (dp[j][k]+num*fctr[n-i-1]*inv(fctr[n-j]))%mod
ans = 0
for i,x in dp[n].items():
  ans = (ans+i*x)%mod
print(ans)

Fixed submission:
from collections import defaultdict
n,x = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
mod = 10**9+7
dp = [defaultdict(int) for i in range(n+1)]
dp[0][x] = 1
for i in range(1,n+1):
  for j in dp[i-1].keys():
    k = j%a[i-1]
    dp[i][j] = (dp[i][j]+dp[i-1][j]*(n-i))%mod
    dp[i][k] = (dp[i][k]+dp[i-1][j])%mod
ans = 0
for i,x in dp[n].items():
  ans = (ans+i*x)%mod
print(ans)