Problem:
 Score : 600 points    Problem Statement  Snuke has a blackboard and a set S consisting of N integers.
The i -th element in S is S_i .  He wrote an integer X on the blackboard, then performed the following operation N times:   Choose one element from S and remove it.  Let x be the number written on the blackboard now, and y be the integer removed from S . Replace the number on the blackboard with x \bmod {y} .   There are N! possible orders in which the elements are removed from S .
For each of them, find the number that would be written on the blackboard after the N operations, and compute the sum of all those N! numbers modulo 10^{9}+7 .      Constraints  All values in input are integers.  1 \leq N \leq 200  1 \leq S_i, X \leq 10^{5}  S_i are pairwise distinct.         Input Input is given from Standard Input in the following format:  N  X  S_1  S_2  \ldots  S_{N}       Output Print the answer.        Sample Input 1 2 19
3 7      Sample Output 1 3   There are two possible orders in which we remove the numbers from S .  If we remove 3 and 7 in this order, the number on the blackboard changes as follows: 19 \rightarrow 1 \rightarrow 1 .  If we remove 7 and 3 in this order, the number on the blackboard changes as follows: 19 \rightarrow 5 \rightarrow 2 .  The output should be the sum of these: 3 .        Sample Input 2 5 82
22 11 6 5 13      Sample Output 2 288       Sample Input 3 10 100000
50000 50001 50002 50003 50004 50005 50006 50007 50008 50009      Sample Output 3 279669259   Be sure to compute the sum modulo 10^{9}+7 .

Solution (Python):
import sys
readline = sys.stdin.readline
N, X = map(int, readline().split())
S = list(map(int, readline().split()))
S.sort(reverse = True)
maxS = 10**5 + 1
mod = 10**9+7
dp = [0]*maxS
dp[X] += 1
for i in range(N):
    s = S[i]
    dp2 = [d*(N-1-i)%mod for d in dp]
    for j in range(1, maxS):
        dp2[j%s] = (dp2[j%s] + dp[j])%mod
    dp = dp2[:]
ans = 0
for i in range(maxS):
    ans = (ans + i*dp[i])%mod
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)

MOD = 10**9 + 7

N, X = map(int, input().split())
Ss = list(map(int, input().split()))

Ss.sort(reverse=True)

def dfs(i, xNow):
    if memo[i][xNow] != -1:
        return memo[i][xNow]
    if i == N-1:
        ans = xNow % Ss[i]
    else:
        ans = dfs(i+1, xNow % Ss[i])
        ans += dfs(i+1, xNow) * (N-i-1)
    ans %= MOD
    memo[i][xNow] = ans
    return ans

memo = [[-1]*(X+1) for _ in range(N+1)]
dfs(0, X)

print(memo[0][X])

Solution (Python):
MOD = 10**9 + 7

N, X = map(int, input().split())
Ss = list(map(int, input().split()))

Ss.sort(reverse=True)
minS = Ss[-1]

D = {X: 1}
for i, S in enumerate(Ss[:-1]):
    D2 = {}
    for x, num in D.items():
        D2[x] = (D2.get(x, 0) + num*(N-1-i)) % MOD
        D2[x%S] = (D2.get(x%S, 0) + num) % MOD
    D = D2

ans = 0
for x, num in D.items():
    ans += (x%minS) * num % MOD
    ans %= MOD

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

import numpy as np
MOD = 10 ** 9 + 7

N,X = map(int,input().split())
S = [int(x) for x in input().split()]
S.sort()



dp = np.arange(X+1, dtype=np.int64)

for i,s in enumerate(S):
    prev = dp
    dp = i * prev 
    dp += prev[np.arange(X+1) % s] 
    dp %= MOD

answer = dp[-1]
print(answer)

Solution (Python):
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))

Solution (Python):
N,X = map(int,input().split())
S = list(map(int,input().split()))
S.sort()
mod = 10**9+7

dp = [[x]+[0]*N for x in range(X+1)]
for n in range(1,N+1):
    for x in range(X+1):
        dp[x][n] = (dp[x%S[n-1]][n-1] + (n-1)*dp[x][n-1])%mod

print(dp[X][N])

Solution (Python):
N,X = map(int,input().split())
S = list(map(int,input().split()))
S.sort()
mod = 10**9+7

dp = [[x]+[0]*N for x in range(X+1)]
for x in range(X+1):
    for n in range(1,N+1):
        dp[x][n] = (dp[x%S[n-1]][n-1] + (n-1)*dp[x][n-1])%mod

print(dp[X][N])

Solution (Python):
N, X = map(int, input().split())
S = list(map(int, input().split()))

MOD = 1000000007
numbers = S[:]
numbers.sort(reverse=True)


dp = {X: 1}

for i, n in enumerate(numbers):
    tmp_dp = {}
    for k, v in dp.items():
        if k < n:
            tmp_dp[k] = (tmp_dp.get(k,0)+v*(N-i))
        else:
            tmp_dp[k]=(tmp_dp.get(k,0)+v*(N-i-1)) 
            tmp_dp[k%n]=(tmp_dp.get(k%n,0)+v) 
    dp = tmp_dp.copy()
        
ans = 0
for k, v in dp.items():
    ans += k *  v

print(ans % MOD)

Solution (Python):
import math

def cul(i,x):
    if tmp[i][x]==-1:
        tmp[i][x]=( cul(i+1,x)*(N-1-i) + cul(i+1,x%a[i]) )%MOD
    return tmp[i][x]

MOD=10**9+7
N,x=map(int,input().split())

a=list(map(int,input().split()))
a.sort(reverse=True)
tmp= [[-1 for j in range(x+1)] for i in range(N)] + [[j for j in range(x+1)]]

print(cul(0,x))

Solution (Python):
N, X = map(int, input().split())
S = list(map(int, input().split()))
MOD = 10 ** 9 + 7
dp = [[0] * (X + 1) for i in range(N + 1)]
for i in range(X + 1):
    dp[0][i] = i

S = sorted(S)
for i in range(N):
    for j in range(X + 1):
        dp[i + 1][j] += dp[i][j % S[i]] + dp[i][j] * i
        dp[i + 1][j] %= MOD

print(dp[N][X])

Solution (Python):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from collections import defaultdict

MOD = 10**9+7

n,x = li()
s = list(li())
s.sort(reverse=True)

dd = defaultdict(int)

dd[x] = 1

for i in range(n):
    nexdd = defaultdict(int)
    
    for k, v in dd.items():
        nexdd[k] += dd[k] * (n-i-1)
        nexdd[k] %= MOD
        
        nexdd[k%s[i]] += dd[k]
        nexdd[k%s[i]] %= MOD
        
    dd = nexdd
    
ans = 0
for k,v in dd.items():
    ans += k*v
    ans %= MOD
    
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
n,x = map(int,input().split())
s = list(map(int,input().split()))
mod = 10**9 + 7
s.sort()
s = s[::-1]
c = max(max(s),x)
dp = [[0]*(c+1) for i in range(n+1)]
dp[0][x] = 1
for i in range(n):
    for j in range(c+1):
        dp[i+1][j%s[i]] += dp[i][j]
        dp[i+1][j%s[i]] %= mod
        dp[i+1][j] += dp[i][j]*(n-i-1)
        dp[i+1][j] %= mod
ans = 0
for i in range(c+1):
        ans += dp[n][i]*i
        ans %= mod
print(ans)

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):
N , X = list(map(int, input().split()))
S = list(map(int, input().split()))

MOD = 10**9+7
memo = [{} for i in range(N)]
S.sort(reverse=True)

def ddp(cur_N, cur_x):
    if cur_N == N:
        return cur_x
    if cur_x in memo[cur_N]:
        return memo[cur_N][cur_x]
    ret = ddp(cur_N+1, cur_x % S[cur_N]) + ddp(cur_N+1, cur_x)*(N-cur_N-1)
    ret = ret % MOD
    memo[cur_N][cur_x] = ret
    return ret
print(ddp(0, X))

Solution (Python):
from collections import defaultdict as dd

N, X = [int(i) for i in input().split()]
sList = [int(i) for i in input().split()]
sList.sort(reverse=True)
mod = 10 ** 9 + 7

dp = dd(int)
dp[X] = 1
for i, s in enumerate(sList):
    new_dp = dd(int)
    for x in dp:
        new_dp[x] += dp[x] * (N - i - 1)
        new_dp[x] %= mod
        new_dp[x % s] += dp[x]
        new_dp[x % s] %= mod
    dp = new_dp

ans = 0
for x in dp:
    ans += x * dp[x]
    ans %= mod

print(ans)

Solution (Python):


from collections import defaultdict


n, X = map(int, input().split())
s = list(map(int, input().split()))
s = sorted(s, reverse=True)

mod = 10 ** 9 + 7

dp = defaultdict(int)
dp[X] = 1

for i in range(n):
    new_dp = defaultdict(int)

    for x in dp:
        mod_x = x % s[i]
        new_dp[x] += dp[x] * (n - i - 1)
        new_dp[mod_x] += dp[x]

    dp = new_dp

total = 0
for x in dp:
    total += dp[x] * x

total %= mod

print(total)

Solution (Python):

import sys

MOD = 1000000007  

def solve(N: int, X: int, S: "List[int]"):
    fact = [0] * (N + 1)
    fact[1] = 1
    for i in range(2, N + 1):
        fact[i] = fact[i - 1] * i

    memo = [[-1] * (X + 1) for _ in range(N + 1)]
    S.sort(reverse=True)
    def rec(idx, x, d=0):
        
        
        if memo[idx][x] >= 0:
            return memo[idx][x]
        if idx == N - 1:
            memo[idx][x] = x % S[idx]
            return memo[idx][x]
        ret = 0

        ret += rec(idx + 1, x, d + 1) * (N - idx - 1)
        ret += rec(idx + 1, x % S[idx], d + 1)

        
        
        
        
        memo[idx][x] = ret
        return ret

    ret = rec(0, X)
    print(ret % MOD)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    X = int(next(tokens))  
    S = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, X, S)

if __name__ == '__main__':
    main()

Solution (Python):
n, x = [int(i) for i in input().split()]
S = [int(i) for i in input().split()]
DP = [[0] * (x + 1) for i in range(n + 1)]
DP[0][x] = 1
p = 10 ** 9 + 7
S.sort(reverse=True)

for i in range(n):
    for j in range(x + 1):
        DP[i][j] %= p
        if i != n - 1:
            DP[i + 1][j] += DP[i][j] * (n - i - 1)
        DP[i + 1][j % S[i]] += DP[i][j]

print(sum(i * d % p for i, d in enumerate(DP[n])) % p)

Solution (Python):
N, X = map(int, input().split())
s_list = list(map(int, input().split()))

s_list.sort(reverse=True)

mod = 10 ** 9 + 7

dp = [[0] * (X + 1) for _ in range(N + 1)]
dp[0][X] = 1

for i in range(N):
    for x in range(X + 1):
        if dp[i][x] == 0:
            continue
        s = s_list[i]
        dp[i + 1][x % s] += dp[i][x]
        dp[i + 1][x % s] %= mod
        dp[i + 1][x] += dp[i][x] * (N - i - 1)
        dp[i + 1][x] %= mod

ans = 0
for x in range(1, X + 1):
    ans += dp[N][x] * x
    ans %= mod
print(ans)

Solution (Python):
from collections import defaultdict


def prepare(n, MOD):
    f = 1
    factorials = [1]
    for i in range(1, n + 1):
        f = f * i % MOD
        factorials.append(f)
    return factorials


n, x = map(int, input().split())
sss = list(map(int, input().split()))
sss.sort(reverse=True)
ms = sss.pop()
MOD = 10 ** 9 + 7
factorials = prepare(n, MOD)
dp = {x: 1}
ans = 0
for i, s in enumerate(sss):
    ndp = defaultdict(lambda: 0)
    k = n - i
    for y, c in dp.items():
        if y < s:
            ndp[y] = (ndp[y] + c * k) % MOD
        else:
            z = y % s
            ndp[y] = (ndp[y] + c * (k - 1)) % MOD
            if z < ms:
                ans = (ans + z * c * factorials[k - 1]) % MOD
            else:
                ndp[z] = (ndp[z] + c) % MOD
    dp = ndp

for y, c in dp.items():
    ans = (ans + (y % ms) * c) % MOD

print(ans)

Solution (Python):
N, X = map(int, input().split())
S = sorted(list(map(int, input().split())))
mod = 10 ** 9 + 7
dp = [[0 for _ in range(X+1)] for _ in range(N+1)]
dp[1] = [i % S[0] for i in range(X+1)]
S = [None] + S

for i in range(2, N+1):
    for j in range(X+1):
        dp[i][j] = (dp[i-1][j]*(i-1) + dp[i-1][j%S[i]]) % mod


print(dp[-1][-1])

Solution (Python):
import math
import sys

sys.setrecursionlimit(100000000)

N, X = map(int, input().split())
S = list(map(int, input().split()))
S.sort(reverse=True)


def memoize(f):  
    table = [{} for _ in range(N+1)]

    def func(i, key):
        if key not in table[i]:
            table[i][key] = f(i, key)
        return table[i][key]
    return func


@memoize
def dfs(i, x):
    if i == N:
        return x
    else:
        return dfs(i+1, x % S[i]) + dfs(i+1, x) * (N-i-1)


print(dfs(0, X) % (10**9+7))

Solution (Python):
n, x = map(int, input().split())
a = list(map(int, input().split()))

a.sort()

MOD = 10 ** 9 + 7

S = [0] * (x+1)
T = [0] * (x+1)

S[x] = 1

for i in range(n-1, -1, -1):
    T = [0] * (x + 1)
    for j in range(x+1):
        S[j] %= MOD
        T[j] += S[j] * i % MOD
        T[j % a[i]] += S[j]
    S, T = T, S

print(sum(i * S[i] % MOD for i in range(a[0]) ) % MOD)

Solution (Python):
N,X = map(int, input().split())
S = list(map(int,input().split()))

S.sort(reverse=True)
mod = 10**9+7
dp = [ [0] * (10**5+1) for _ in range(N+1)]
dp[0][X] = 1
for i in range(N):
    for j in range(0,10**5+1):
        dp[i+1][j] += dp[i][j] * (N-1-i) % mod
        dp[i+1][j% S[i]] += dp[i][j] 

        dp[i+1][j] %= mod
        dp[i+1][j%S[i]] %= mod
ans = 0
for j in range(0,10**5+1):
    ans = (ans + dp[N][j] * j % mod ) % mod

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
n,x = map(int,input().split())
s = list(map(int,input().split()))
s.sort(reverse = True)
mod = 10**9+7


dp = [[0]*(x+1) for i in range(n+1)]
dp[0][x] = 1
for i,p in enumerate(s):
    for j in range(x+1):
        if dp[i][j] == 0:
            continue
        dp[i+1][j%p] += dp[i][j]
        dp[i+1][j] += dp[i][j]*(n-i-1)
        dp[i+1][j%p] %= mod
        dp[i+1][j] %= mod
res = 0
for i in range(1,x+1):
    res += dp[-1][i]*i
    res %= mod
print(res)

Solution (Python):
mod = 10**9+7

n,m = map(int,input().split())

s = list(map(int,input().split()))

s.sort()

s = [None] + s

dp = [[0 for i in range(m+1)] for j in range(n+1)]
dp[1] = [i % s[1] for i in range(m+1)]

for i in range(2,n+1):
    for j in range(m+1):
        dp[i][j] = (dp[i-1][j] * (i-1) + dp[i-1][j%s[i]]) % mod
print(dp[n][m])

Solution (Python):
N, X = map(int,input().split())
S = list(map(int,input().split()))
S.sort(reverse=True)
S = [0] + S 
MOD = 10**9+7
dp = [[0]*(10**5+1) for i in range(N+1)]
dp[0][X] = 1
for i in range(N):
    for x in range(10**5+1):
        dp[i+1][x%S[i+1]] += dp[i][x]
        dp[i+1][x] += dp[i][x]*(N-(i+1))
        dp[i+1][x%S[i+1]] %= MOD
        dp[i+1][x] %= MOD
ans = 0
for x in range(10**5+1):
    ans += (x*dp[N][x])%MOD
print(ans%MOD)

Solution (Python):
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()

Solution (Python):



from collections import defaultdict as dd
mod=10**9+7
N,X=map(int,input().split())
S=sorted(list(map(int,input().split())),reverse=True)

d = dd(int)
d[X] = 1
for i in range(N):
    new = dd(int)
    for X in d:
        new[X] += d[X]*(N-i-1)
        new[X] %= mod
        
        new[X%S[i]] += d[X]
        new[X%S[i]] %= mod
    d = new
ans = 0
for x in d:
    ans += x*d[x]
    ans %= mod
print(ans)

Solution (Python):
from collections import defaultdict
inpl = lambda: list(map(int,input().split()))
M = 10**9 + 7

N, X = inpl()
S = sorted(inpl())
P = defaultdict(lambda: -1)

def ModuloOperations(i, x):
    if i == 0:
        return x
    else:
        if P[(i,x)] < 0:
            P[(i,x)] = (ModuloOperations(i-1,x)*(i-1) + ModuloOperations(i-1, x % S[i-1])) % M
        return P[(i,x)]

print(ModuloOperations(N,X))

Solution (Python):
N,X,*S=map(int,open(0).read().split())
e=enumerate
d=range(X+1)
for i,s in e(sorted(S)):d=[(t*i+d[j%s])%(10**9+7)for j,t in e(d)]
print(d[-1])

Solution (Python):
N,X,*S=map(int,open(0).read().split())
e=enumerate
d=range(X+1)
for i,s in e(sorted(S)):d=[(t*i+d[j%s])%(10**9+7)for j,t in e(d)]
print(d[-1])

Solution (Python):
MOD = 10**9+7
MAX = 10**5+1

n,x = map(int,input().split())
s =  list(map(int,input().split()))

fac = [1 for i in range(MAX)]
inv = [1 for i in range(MAX)]
for i in range(2,MAX):
    fac[i] = fac[i-1]*i%MOD
    inv[i] = MOD - inv[MOD%i]*(MOD // i)%MOD

s.sort()
dp = [0 for i in range(x+1)]
for i in range(x+1):dp[i] = (i%s[0])*fac[n-1]%MOD

for i in range(1,n):
    dpp = dp[:]
    for j in range(x+1):
        dpp[j] = (dp[j]+dp[j%s[i]]*inv[i])%MOD
    dp = dpp


print(dp[x])

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()
from math import factorial
from collections import Counter

def main():
    N, X = LI()
    S = LI()
    S.sort(reverse=True)
    dp = Counter()
    dp[X] = factorial(N)
    for i, s in enumerate(S):
        pre = dp
        dp = Counter()
        for n, cnt in pre.items():
            if s <= n:
                dp[n] += cnt * (N - 1 - i) // (N - i)
                dp[n % s] += cnt // (N - i)
            else:
                dp[n] += cnt
    ans = 0
    for n, cnt in dp.items():
        ans = (ans + (n * cnt) % MOD) % MOD

    return ans

print(main())

Solution (Python):
N,X = map(int,input().split())
S = list(map(int,input().split()))
S.sort(reverse=True)

MOD = 10**9+7

dp = [[0]*(X+1) for i in range(N+1)]
dp[0][X] = 1

for i,a in enumerate(S):
    for j in range(1,X+1):
        if dp[i][j] == 0: continue
        dp[i+1][j] += dp[i][j] * (N-1-i)
        dp[i+1][j] %= MOD
        dp[i+1][j%a] += dp[i][j]
        dp[i+1][j%a] %= MOD

ans = sum(i*a for i,a in enumerate(dp[-1]))
print(ans % MOD)

Solution (Python):
from functools import lru_cache
N, X = map(int, input().split())
S = sorted([int(a) for a in input().split()])[::-1]
P = 10**9+7
fact = [1]
for i in range(N+10):
    fact.append(fact[-1]*(i+1)%P)

@lru_cache(None)
def calc(k, x):
    if k >= N:
        return x
    if x < S[-1]:
        return (x*fact[N-k]) % P
    return (calc(k+1, x%S[k]) + calc(k+1, x) * (N-k-1)) % P

print(calc(0, X))

Solution (Python):
mod=10**9+7
N,X=map(int,input().split())
S=[int(i) for i in input().split()]
S.sort(reverse=True)

Factorial=[1]*(N+1)
for i in range(1,N+1):
  Factorial[i]=Factorial[i-1]*(i)%mod
def power(x,y):
  if y==0:
    return 1
  elif y==1:
    return x%mod
  elif y%2==0:
    return power(x,y//2)**2%mod
  else:
    return ((power(x,y//2)**2)*x)%mod
inv=[0]*(N+1)
for i in range(N+1):
  inv[i]=power(i,mod-2)
dp=[[0]*(X+1) for i in range(N+1)]
for x in range(X+1):
  dp[N][x]=x
for i in range(N)[::-1]:
  for x in range(X+1):
    dp[i][x]=dp[i+1][x%S[i]]+(N-i-1)*dp[i+1][x]
    dp[i][x]%=mod
print((dp[0][X])%mod)

Solution (Python):
import sys
input = sys.stdin.readline
mod=10**9+7

N,X=map(int,input().split())
S=list(map(int,input().split()))

S.sort(reverse=True)

DP={X:1}

for i in range(N):
    s=S[i]
    
    NDP=dict()

    for d in DP:
        if d<s:
            NDP[d]=(NDP.get(d,0)+DP[d]*(N-i))%mod

        else:
            NDP[d]=(NDP.get(d,0)+DP[d]*(N-i-1))%mod
            NDP[d%s]=(NDP.get(d%s,0)+DP[d])%mod

    DP=NDP

SUM=0
for d in DP:
    SUM=(SUM+d*DP[d])%mod

print(SUM)

Solution (Python):
from collections import defaultdict
N,X=map(int,input().split())
S=[int(i) for i in input().split()]
S=sorted(S,reverse=True)
mod=10**9+7
P=[1]*(N+1)
for i in range(1,N+1):
    P[i]=pow(i,mod-2,mod)

dd=defaultdict(int)
dd[X]=1
for i in range(N):
    ndd=defaultdict(int)
    for x,p in dd.items():
        ndd[x]+=((1-P[N-i])*p)
        ndd[x]%=mod
        ndd[x%S[i]]+=(P[N-i]*p)
        ndd[x%S[i]]%=mod
    dd=ndd
    
t=1
table=[1]*(N+3)
for i in range(1,N+3):
    t*=i
    t%=mod
    table[i]=t
ans=0
for x,p in dd.items():
    ans+=(x*p*table[N])%mod
    ans%=mod
print(ans)

Solution (Python):
from collections import defaultdict as dd
mod = 10**9+7
n,x = map(int, input().split())
s = sorted(list(map(int, input().split())),reverse=True)
d = dd(int)
d[x] = 1
for i in range(n):
    new = dd(int)
    for x in d:
        new[x] += d[x]*(n-i-1)
        new[x] %= mod
        new[x%s[i]] += d[x]
        new[x%s[i]] %= mod
    d = new
ans = 0
for x in d:
    ans += x*d[x]
    ans %= mod
print(ans)

Solution (Python):
N, X = map(int, input().split())
*V, = map(int, input().split())

MOD = 10**9 + 7

V.sort()

S = [0]*(X+1)
S[X] = 1
T = [0]*(X+1)
zeros = [0]*(X+1)
for i in range(N-1, -1, -1):
    T[:] = zeros
    for k in range(X+1):
        S[k] %= MOD
        T[k] += S[k] * i % MOD
        T[k % V[i]] += S[k]
    S, T = T, S
print(sum(k*S[k] % MOD for k in range(V[0])) % MOD)

Solution (Python):
from collections import defaultdict
N,X = map(int,input().split())
S = list(map(int,input().split()))
S.sort()
mod = 10**9+7

fact_memo = [0]*201
fact_memo[0] = 1
def fact(n):
    if fact_memo[n] == 0:
        fact_memo[n] = n*fact(n-1)%mod
    return fact_memo[n]

fact_inv_memo = [0]*201
fact_inv_memo[0] = 1
def fact_inv(n):
    if fact_inv_memo[n] == 0:
        fact_inv_memo[n] = pow(fact(n),mod-2,mod)
    return fact_inv_memo[n]

dp = defaultdict(lambda :[-1]*(X+1))
for n in range(N):
    for x in range(X+1):
        if n == 0:
            dp[n][x]=x%S[0]
        else:
            dp[n][x]=(dp[n-1][x%S[n]]+n*dp[n-1][x])%mod

print(dp[N-1][X])

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,x = LI()
    a = sorted(LI(),reverse=True)
    d = collections.defaultdict(int)
    d[x] = 1
    for i in range(n):
        c = a[i]
        e = collections.defaultdict(int)
        for k,v in d.items():
            e[k] += v * (n-i-1)
            e[k] %= mod
            e[k%c] += v
            e[k%c] %= mod

        d = e

    r = 0
    for k,v in d.items():
        r += k * v
        r %= mod

    return r


print(main())

Solution (Python):
import copy
import math

N,X = map(int,raw_input().split())
S = map(int,raw_input().split())
MOD = 10**9 + 7

S.sort()

dp = [[0 for i in range(X+1)] for j in range(N+1)]

for i in range(X+1):
  dp[1][i] = i % S[0]

for n in range(2,N+1):
  for x in range(X+1):
    
    dp[n][x] =  dp[n-1][x] * (n-1) 
    dp[n][x] += dp[n-1][x%S[n-1]]
    dp[n][x] %= MOD

print dp[N][X]

Solution (Python):
def int_raw():
    return int(raw_input())

def ss_raw():
    return raw_input().split()

def ints_raw():
    return map(int, ss_raw())

INF = 1 << 29
DIV = 10**9 +7
N,X  = ints_raw()
dp = [{} for x in xrange(X+1)]

Ss = ints_raw()

Ss.sort()


def rec(n,idx):
    if idx == 0:
        return n%Ss[0]
    elif idx in dp[n]:
        return dp[n][idx]
    else:
        ans = (rec(n,idx-1)*idx + rec(n%Ss[idx],idx-1))%DIV
        dp[n][idx]=ans
        return ans

def ans():
    return rec(X,N-1)

print ans()

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**9)
mod=10**9+7
n,x=map(int,input().split())
S=list(map(int,input().split()))
S.sort()
DP=[[0]*(x+1) for _ in range(n+1)]
for i in range(x+1):
    DP[1][i]=i%S[0]
for i in range(1,n):
    for j in range(x+1):
        DP[i+1][j]=(DP[i][j]*i+DP[i][j%S[i]])%mod
print(DP[n][x])

Solution (Python):
MOD = 10**9 + 7

N,X = map(int,input().split())
S = list(map(int,input().split()))
S.sort()
S = [None] + S
dp = [[0 for i in range(X+1)] for j in range(N+1)]
dp[1] = [i % S[1] for i in range(X+1)]
for n in range(2, N+1):
	for x in range(X+1):
		dp[n][x] = (dp[n-1][x] * (n-1) + dp[n-1][x%S[n]]) % MOD
print(dp[N][X])

Solution (Python):
N, X = map(int, input().split())
*V, = map(int, input().split())

MOD = 10**9 + 7

fact = [1]*(X+1)
rfact = [1]*(X+1)
for i in range(X):
    fact[i+1] = r = fact[i] * (i+1) % MOD
    rfact[i+1] = pow(r, MOD-2, MOD)

V.sort()

S = [0]*(X+1)
S[X] = 1
for i in range(N-1, -1, -1):
    T = [0]*(X+1)
    for k in range(X+1):
        S[k] %= MOD
        T[k] += S[k] * i % MOD
        T[k % V[i]] += S[k]
    S = T
print(sum(k*S[k] % MOD for k in range(V[0])) % MOD)

Solution (Python):


import sys
input = sys.stdin.readline

MOD = 10**9+7

n,x = [int(i) for i in input().split()]
s = [int(i) for i in input().split()]
s.sort(reverse=True)

ans = [0]*(x+1)
ans[x] = 1


for i, ss in enumerate(s):
    bairitu = n-i-1

    for j,xx in enumerate(ans):

        ans[j] = (xx*bairitu)%MOD
        ans[j%ss] = (ans[j%ss]+xx)%MOD



res=0
for j,xx in enumerate(ans):
    res += j*xx 
    res = res%MOD

print(res)