CodeEff/ECCO huggingface dataset:
# Original solution
#!/usr/bin/env python3
import sys
import heapq
from collections import deque


inf = float("inf")

def solve(N: int, a: "List[int]"):
    L = a[:N]
    R = [-x for x in a[2*N:]]
    heapq.heapify(L)
    heapq.heapify(R)
    M = a[N:2*N]
    sumL = [0 for _ in range(N+1)] # sumL[i]: a[0]からa[N-1+i]までの大きい順にN個の和
    sumR = [0 for _ in range(N+1)] # sumR[i]: -(a[N-1+i]からa[3*N-1]までの小さい順にN個の和)

    sumL[0] = sum(L)
    sumR[N] = sum(R)

    for i in range(N):
        l = heapq.heappop(L)
        diff = M[i] - l
        if diff > 0:
            sumL[i+1] = sumL[i] + diff
            heapq.heappush(L, M[i])
        else:
            sumL[i+1] = sumL[i]
            heapq.heappush(L, l)
    
    for i in range(N-1, -1, -1):
        r = heapq.heappop(R)
        diff = -M[i] - r
        if diff > 0:
            sumR[i] = sumR[i+1] + diff
            heapq.heappush(R, -M[i])
        else:
            sumR[i] = sumR[i+1]
            heapq.heappush(R, r)
    
    res = -inf
    for suml, sumr in zip(sumL, sumR):
        res = max(res, suml + sumr)
    print(res)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [ int(next(tokens)) for _ in range(3*N) ]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
import sys
import heapq


inf = float("inf")

def solve(N: int, a: "List[int]"):
    L = a[:N]
    R = [-x for x in a[2*N:]]
    heapq.heapify(L)
    heapq.heapify(R)
    M = a[N:2*N]
    sumL = [0 for _ in range(N+1)] # sumL[i]: a[0]からa[N-1+i]までの大きい順にN個の和
    sumR = [0 for _ in range(N+1)] # sumR[i]: -(a[N-1+i]からa[3*N-1]までの小さい順にN個の和)

    sumL[0] = sum(L)
    sumR[N] = sum(R)

    for i in range(N):
        heapq.heappush(L, M[i])
        l = heapq.heappop(L)
        diff = M[i] - l
        sumL[i+1] = sumL[i] + diff
    
    for i in range(N-1, -1, -1):
        heapq.heappush(R, -M[i])
        r = heapq.heappop(R)
        diff = -M[i] - r
        sumR[i] = sumR[i+1] + diff
    
    res = max(suml + sumr for suml, sumr in zip(sumL, sumR))
    print(res)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [ int(next(tokens)) for _ in range(3*N) ]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()
# Original solution
import heapq
N = int(eval(input()))
A = list(map(int,input().split()))

#前半部分の最適化辞書作成
SUM = sum(A[:N])
firstDic = {N-1:SUM}
que = A[:N]
heapq.heapify(que)
for i in range(N,2*N):
    SUM += A[i]
    heapq.heappush(que,A[i])
    SUM -= heapq.heappop(que)
    firstDic[i] = SUM


#後半部分の最適化辞書作成
SUM = sum(A[2*N:])*-1
latterDic = {2*N-1:SUM*-1}
que = [A[i]*-1 for i in range(2*N,3*N)]
heapq.heapify(que)
for i in range(2*N-1,N-1,-1):
    SUM += A[i]*-1
    heapq.heappush(que,A[i]*-1)
    SUM -= heapq.heappop(que)
    latterDic[i-1] = SUM*-1
ans = -1*float("INF")
for k,v in list(firstDic.items()):
    ans = max(ans,v-latterDic[k])

print(ans)

# Optimized solution
N = int(eval(input()))
A = list(map(int,input().split()))
B = []
for i in range(3*N-1,-1,-1):
    B.append(A[i]*-1)
"""
前半をできるだけおおきく、後半をできるだけ小さくする。
前半はpriority queにぶっこんでいって、最小値を除去していく
"""
import heapq
frontSum = sum(A[:N])
frontQue = A[:N]
heapq.heapify(frontQue)
front = [frontSum]
for i in range(N,2*N):
    frontSum += A[i]
    heapq.heappush(frontQue,A[i])
    frontSum -= heapq.heappop(frontQue)
    front.append(frontSum)

backSum = sum(B[:N])
backQue = B[:N]
heapq.heapify(backQue)
back = [backSum*-1]
for i in range(N,2*N):
    backSum += B[i]
    heapq.heappush(backQue,B[i])
    backSum -= heapq.heappop(backQue)
    back.append(backSum*-1)
back = back[::-1]

candi = []
for i in range(len(front)):
    candi.append(front[i]-back[i])
print((max(candi)))
# Original solution
# ABC140E
class SegTree:
    def __init__(self, init_val, ide_ele, seg_func):
        self.segfunc = seg_func
        N = len(init_val)
        self.num = 2**(N-1).bit_length()
        self.ide_ele = ide_ele
        self.seg=[self.ide_ele]*2*self.num
        for i in range(N):
            self.seg[i+self.num-1]=init_val[i]    
        for i in range(self.num-2,-1,-1) :
            self.seg[i]=self.segfunc(self.seg[2*i+1],self.seg[2*i+2]) 
        
    def update(self, k, x):
        k += self.num-1
        self.seg[k] = x
        while k+1:
            k = (k-1)//2
            self.seg[k] = self.segfunc(self.seg[k*2+1],self.seg[k*2+2])
        
    def query(self, p, q):
        if q<=p:
            return self.ide_ele
        p += self.num-1
        q += self.num-2
        res=self.ide_ele
        while q-p>1:
            if p&1 == 0:
                res = self.segfunc(res,self.seg[p])
            if q&1 == 1:
                res = self.segfunc(res,self.seg[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        if p == q:
            res = self.segfunc(res,self.seg[p])
        else:
            res = self.segfunc(self.segfunc(res,self.seg[p]),self.seg[q])
        return res
N, = list(map(int, input().split()))
A = list(map(int, input().split()))
B = sorted([(a, i) for i, a in enumerate(A)], key=lambda x:x[0])
z = dict()
for i, (b, j) in enumerate(B):
    z[(b, j)] = i

ss = SegTree([3*N]*3*N, 3*N, min)
bs = []
y = 0
for i, x in enumerate(A):
    if i == 2*N:
        break
    ss.update(z[(x, i)], z[(x, i)])
    y += x
    if i < N-1:
        continue
    if i == N-1:
        bs.append(y)
    else:
        mx = ss.query(0, 3*N)
        y = y - B[mx][0]
        ss.update(mx, 3*N)
        bs.append(y)

ss = SegTree([-1]*3*N, -1, max)
nbs = []
#tmp = set()
y = 0
for i, x in enumerate(A[::-1]):
    if i == 2*N:
        break
    ss.update(z[(x, 3*N-i-1)], z[(x, 3*N-i-1)])
#    tmp.add((x, i))
    y += x
    if i < N-1:
        continue
    if i == N-1:
        nbs.append(y)
    else:
        mx = ss.query(0, 3*N)
#        print(max(tmp), B[mx])
#        tmp.remove(max(tmp))
        y = y - B[mx][0]
        ss.update(mx, -1)
        nbs.append(y)

#print(B)
#print(bs)
#print(nbs)
r = -10**18
for i in range(N+1):
    r = max(r, bs[i] - nbs[N-i])
print(r)


# Optimized solution
from heapq import *
N, = list(map(int, input().split()))
A = list(map(int, input().split()))
s = sum(A[:N])
rs = [s] + [0] * N
l = []
for i in range(N):
    heappush(l, A[i])
for i in range(N, 2*N):
    s += A[i]
    heappush(l, A[i])
    x = heappop(l)
    s -= x
    rs[i-N+1] = s
l2 = []
s2 = 0
for i in range(2*N, 3*N):
    heappush(l2, -A[i])
    s2+=A[i]

rs2 = [0] * N + [s2]
for i in range(2*N-1, N-1, -1):
    s2 += A[i]
    heappush(l2, -A[i])
    x = -heappop(l2)
#    print(A[i])
#    print(x)
    s2 -= x
    rs2[i-N] = s2
#print(rs2)
r = -10**18
for i in range(N+1):
    c = rs[i] - rs2[i]
    r = max(c, r)
print(r)
# Original solution
# diverta 2019 Programming Contest 2: A – Ball Distribution
n, k = [int(s) for s in input().split()]
if k == 1:
    print((0))
else:
    print((n - k))

# Optimized solution
# diverta 2019 Programming Contest 2: A – Ball Distribution
n, k = list(map(int, input().split()))
print((n - k if k > 1 else 0))
# Original solution
import sys
from collections import deque  # 双方向キュー
from collections import defaultdict  # 初期化済み辞書
from heapq import heapify, heappush, heappop, heappushpop  # プライオリティキュー
from bisect import bisect_left, bisect_right  # 二分探索
#import numpy as np  # 1.8.2
#import scipy  # 0.13.3

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N, K = i2nn()
    if K == 1:
        print((0))
    else:
        print((N - K))

main()


# Optimized solution
import sys
s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2sss = lambda n: [list(sys.stdin.readline().rstrip()) for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N, K = i2nn()
    if K == 1:
        print((0))
    else:
        print((N-K))

main()
# Original solution
N, K = list(map(int, input().split()))

if K == 1:
    print((0))
else:
    print((N - K))


# Optimized solution
N, K = list(map(int, input().split()))
print((0 if K == 1 else N - K))
# Original solution
n, k = list(map(int, input().split()))
if k == 1:
    print((0))
else:
    print((n - k))


# Optimized solution
n, k = list(map(int, input().split()))
print((n % k))
# Original solution
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()


# Optimized solution
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()
# Original solution
N,X = list(map(int,input().split()))
S = list(map(int,input().split()))
S.sort()
mod = 10**9+7

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

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

# Optimized solution
N,X = list(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]))
# Original solution
N,X = list(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]))

# Optimized solution
N,X = list(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]))
# Original solution
import sys
from collections import defaultdict
def input():
	return sys.stdin.readline()[:-1]

MOD = 1e9+7
n, x = list(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)))

# Optimized solution
import sys
from collections import defaultdict
def input():
	return sys.stdin.readline()[:-1]

MOD = 1e9+7
n, x = list(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 list(dp.items()):
		dpp[key] += (n-1-i) * value
		dpp[key%ss] += value
	for key in list(dpp.keys()):
		dpp[key] %= MOD
	dp = dpp
ans = 0
for key, value in list(dp.items()):
	ans += value * key
	ans %= MOD
print((int(ans)))
# Original solution
N, X = list(map(int, input().split()))
*S, = list(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)

S.sort()
memo = {}
def dfs(i, k):
    key = (i, k)
    if key in memo:
        return memo[key]
    if i == N:
        return k == X
    r = dfs(i+1, k) * i % MOD
    if k <= S[i]:
        for j in range(k, X+1, S[i]):
            r = (r + dfs(i+1, j)) % MOD
    memo[key] = r
    return r
print((sum(x*dfs(0, x)%MOD for x in range(S[0])) % MOD))

# Optimized solution
N, X = list(map(int, input().split()))
*V, = list(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))
# Original solution
N, X = list(map(int, input().split()))
*V, = list(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))

# Optimized solution
N, X = list(map(int, input().split()))
*V, = list(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))
# Original solution
mod=10**9+7
N,X=list(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]]*inv[N-i]
    dp[i][x]%=mod
    dp[i][x]+=(N-i-1)*dp[i+1][x]*inv[N-i]
    dp[i][x]%=mod
print(((dp[0][X]*Factorial[N])%mod))

# Optimized solution
mod=10**9+7
N,X=list(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))
# Original solution
import sys
sys.setrecursionlimit(10**9)

MOD = 10**9 + 7

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

Ss.sort(reverse=True)

def dfs(i, xNow):
    if (i, xNow) in memo:
        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 = {}
dfs(0, X)

print((memo[(0, X)]))


# Optimized solution
MOD = 10**9 + 7

N, X = list(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 list(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 list(D.items()):
    ans += (x%minS) * num % MOD
    ans %= MOD

print(ans)
# Original solution
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


# 挿入dp
def solve():
    n,x = LI()
    s = LI()
    s.sort()
    dp = [[0]*(x+1) for i in range(n+1)] # dp[i][j] := スタート時点の値がjの時に最初のi個の順列すべてを試し、えられる総和
    for j in range(x+1):
        dp[0][j] = j # dpの定義より明らか
        for i in range(n):
            si = s[i]
            ni = i+1
            nd = dp[i][j%si] # 先頭にsiを挿入 (スタートの値がj%siとなる)
            nd += i*dp[i][j] # 先頭以外にsiを挿入 (スタートの値も間の値も変わらない)
            dp[ni][j] += nd
            if dp[ni][j] >= mod:
                dp[ni][j] %= mod
    print((dp[n][x]))
    return

#Solve
if __name__ == "__main__":
    solve()


# Optimized solution
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


# 挿入dp
def solve():
    n,x = LI()
    s = LI()
    s.sort()
    dp = [[0]*(x+1) for i in range(n+1)] # dp[i][j] := スタート時点の値がjの時に最初のi個の順列すべてを試し、えられる総和
    for j in range(x+1):
        dp[0][j] = j # dpの定義より明らか
        for i in range(n):
            si = s[i]
            ni = i+1
            nd = dp[i][j%si] # 先頭にsiを挿入 (スタートの値がj%siとなる)
            nd += i*dp[i][j] # 先頭以外にsiを挿入 (スタートの値も間の値も変わらない)
            dp[ni][j] += nd
            if dp[ni][j] >= mod:
                dp[ni][j] %= mod
    print((dp[n][x]))
    return

# 空間の節約
def solve_1():
    n,x = LI()
    s = LI()
    s.sort()
    #dp = [[0]*(x+1) for i in range(n+1)] # dp[i][j] := スタート時点の値がjの時に最初のi個の順列すべてを試し、えられる総和
    dp = [j for j in range(x+1)]
    for i in range(n):
        si = s[i]
        ndp = [0]*(x+1)
        for j in range(x+1):
            nd = dp[j%si] # 先頭にsiを挿入 (スタートの値がj%siとなる)
            nd += i*dp[j] # 先頭以外にsiを挿入 (スタートの値も間の値も変わらない)
            ndp[j] += nd
            if ndp[j] >= mod:
                ndp[j] %= mod
        dp = [j for j in ndp]
    print((dp[x]))
    return

#Solve
if __name__ == "__main__":
    solve_1()
# Original solution
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,0)] = 1

    fm = {}
    def f(i):
        if i < 2:
            return 1
        if i in fm:
            return fm[i]
        r = i * f(i-1) % mod
        fm[i] = r
        return r

    for ai in range(n):
        c = a[ai]
        e = collections.defaultdict(int)
        for kk,v in d.items():
            k,i = kk
            if ai < n-1:
                e[(k,i+1)] += v
                e[(k,i+1)] %= mod
            kc = k % c
            e[(kc,i)] += v
            e[(kc,i)] %= mod
            for j in range(i-1,-1,-1):
                e[(kc,j)] += v * f(i) // f(j)
                e[(kc,j)] %= mod
        d = e

    r = 0
    for kk,v in d.items():
        k,i = kk
        if i > 0:
            continue
        r += k * v
        r %= mod

    return r


print(main())




# Optimized solution
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())
# Original solution
import sys
input = sys.stdin.readline
mod = 10**9+7

def main():
    N, X = list(map(int, input().split()))
    A = list(map(int, input().split()))
    A.sort()
    L = max(A[-1], X)

    dp = [[0]*N for _ in range(L+1)]
    a = A[0]
    for x in range(L+1):
        dp[x][0] = x%a

    dp1 = [[0]*N for _ in range(L+1)] #累積和見たいな
    for n in range(1, N):
        a = A[n]
        for x in range(L+1):
            dp1[x][n] = ((n-1) * dp1[x][n-1] + dp[x][n-1]) % mod
        for x in range(L+1):
            dp[x][n] = dp1[x%a][n]

    ans = 0
    t = 1
    for n in reversed(list(range(N))):
        ans = (ans + dp[X][n]*t) % mod
        t = t * n % mod

    print(ans)


if __name__ == "__main__":
    main()

# Optimized solution
import sys
input = sys.stdin.readline
mod = 10**9+7

def main():
    N, X = list(map(int, input().split()))
    A = list(map(int, input().split()))
    A.sort()
    L = max(A[-1], X)

    dp = [[0]*N for _ in range(L+1)]
    a = A[0]
    for x in range(L+1):
        dp[x][0] = x%a

    dp1 = [0 for _ in range(L+1)] #累積和見たいな
    for n in range(1, N):
        a = A[n]
        for x in range(L+1):
            dp1[x] = ((n-1) * dp1[x] + dp[x][n-1]) % mod
            dp[x][n] = dp1[x%a]

    ans = 0
    t = 1
    for n in reversed(list(range(N))):
        ans = (ans + dp[X][n]*t) % mod
        t = t * n % mod

    print(ans)


if __name__ == "__main__":
    main()