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.



Sample input:
2 19
3 7


Sample output:
3

Submission status: Wrong Answer

Solution code:
n,x = map(int,input().split())
li_s = list(map(int,input().split()))

li_s = sorted(li_s)

import bisect
import math
ans = 0
num = -1
def be(li_s,x,num):
  global ans
  i = bisect.bisect_right(li_s,x) - 1
  if i == -1:
    ans += math.factorial(n-num)*x
    return
  for j in range(i+1):
    x = x % li_s[j]
    num += 1 
    be(li_s,x,num)

be(li_s,x,num)
print(ans%100000007)

Submission status: Wrong Answer

Solution code:
n,x =  list(map(int,input().split()))
s = list(map(int,input().split()))
s.sort(reverse=True)
dp = [[0 for j in range(x+1)] for i in range(n+1)]
dp[0][x]=1
for i in range(n):
    for j in range(1,x+1):
        dp[i+1][j]=dp[i+1][j]+dp[i][j]*(n-i-1)
        dp[i+1][j%s[i]]=dp[i+1][j%s[i]]+dp[i][j]

answer=0
for j in range(1,x+1):
    answer+=dp[n][j]*j
    
print(answer)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import math

n1, n2 = map(int, input().split())
s = [int(i) for i in input().split()]

s.sort()

min_s = s[0]

i = n1-1
fac = math.factorial(i)

ss = sum([n2 % s[i] for i in range(len(s)) if n2 % s[i] < min_s]) * fac

l = [n2 % s[i] for i in range(len(s)) if n2 % s[i] > min_s]

while (l != []):
    ll = []
    i -= 1
    fac = math.factorial(i)
    for n2 in l:
        ss += sum([n2 % s[i] for i in range(len(s)) if s[i] < n2 and n2 % s[i] < min_s])*fac
        li = [n2 % s[i] for i in range(len(s)) if s[i] < n2 and n2 % s[i] > min_s]
        ll.extend(li)
    l = ll

ss%=10**9+7

print(ss)

Submission status: Wrong Answer

Solution code:
import bisect

N, X = map(int, input().split())
S = [int(s) for s in input().split()]
S.sort(reverse=True)
NN = 10 ** 9 + 7

fact = [1]
for i in range(1, N+1):
    fact.append((fact[i-1] * i) % NN)

dp = {}

def f(i, x):
    if i == N:
        return x
    if (i, x) in dp.keys():
        return dp[(i, x)]

    result = f(i + 1, x % S[i]) / (N - i) + f(i + 1, x) * (N - i - 1) / (N - i)
    dp[(i, x)] = result
    return result

print(int(f(0, X) * fact[N]) % NN)

Submission status: Wrong Answer

Solution code:
import bisect

N, X = map(int, input().split())
S = [int(s) for s in input().split()]
S.sort(reverse=True)
NN = 10 ** 9 + 7

fact = [1]
for i in range(1, N+1):
    fact.append((fact[i-1] * i) % NN)

def f(i, x):
    if i == N:
        return x

    return f(i + 1, x % S[i]) / (N - i) + f(i + 1, x) * (N - i - 1) / (N - i)

print(int(f(0, X) * fact[N]) % NN)

Submission status: Wrong Answer

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

MOD = 10 ** 9 + 7
S.sort()

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

F = [1] * (N + 1)
for i in range(1, N + 1):
  F[i] = F[i - 1] * i % MOD
IF = [0] * (N + 1)
IF[N] = power(F[N], MOD - 2)
for i in range(N - 1, -1, -1):
  IF[i] = IF[i + 1] * (i + 1) % MOD

def count(x, num):
  Ln = num
  Ans = 0
  if Ln == 0:
    return x
  if S[0] > x:
    return x * F[Ln] % MOD
  M = -1
  for i in range(Ln):
    if S[i] > x:
      M = i
      break
    else:
      n = Ln - i - 1
      Ans = Ans + count(x % S[i], i) * F[n] * F[i + n] * IF[n] * IF[i] % MOD
  if M > 0 and num == N:
    k = N - M
    Ans = Ans + count(x, M) * F[k] * F[k + M - 1] * IF[k - 1] * IF[M] % MOD
  return Ans % MOD 

print(int(count(X, N) % MOD))

Submission status: Wrong Answer

Solution code:
import itertools
n, x = (int(i) for i in input().split()) 
s = [int(i) for i in input().split()]
e = [int(i) for i in range(n)]
result = 0
 
for i in itertools.permutations(e) :
 y=x
 for j in range(n) : y=y%s[i[j]]
 result += y
 
print(result%(10^9+7))

Submission status: Wrong Answer

Solution code:
N, X = map(int, input().split())
S = [int(x) for x in input().split()]
S.sort(reverse=True)
p = 10 ** 9 + 7
P = [1] * (N + 1)
for i in range(1, N):
  P[i + 1] = P[i] * (i + 1)
  P[i + 1] %= p
C = 0
def xUpdate(X, S):
  cnt = 0
  d = 0
  if len(S) == 0:
    return X
  if S[-1] > X:
    return X * P[len(S)]
  for i, s in enumerate(S):
    if s > X:
      d += 1
    else:
      cnt += xUpdate(X % s, S[d:i] + S[i+1:N]) * P[d] % p
  return cnt % p
print(xUpdate(X, S))

Submission status: Wrong Answer

Solution code:
import itertools
n, x = (int(i) for i in input().split()) 
s = [int(i) for i in input().split()]
e = [int(i) for i in range(n)]
result = 0

for i in itertools.permutations(e) :
 y=x
 for j in range(n) : y=y%s[i[j]]
 result += y%(10^9+7)

print(result)

Submission status: Wrong Answer

Solution code:
import math

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

memo = {}
MOD = 10 ** 9 + 7

def mod(x, ss):
    if (x, tuple(ss)) in memo:
        print('hit')
        return memo[(x, tuple(ss))]

    if len(ss) == 1:
        return x % ss[0]

    if x < ss[0]:
        res = x * math.factorial(len(ss))
        memo[(x, tuple(ss))] = res
        return res

    total = 0
    for s in ss:
        ss2 = ss[:]
        ss2.remove(s)
        total = (total + mod(x % s, ss2)) % MOD

    memo[(x, tuple(ss))] = total
    return total

print(mod(X, Ss))

Submission status: Wrong Answer

Solution code:
import math

def calcmod(moduro, s, numbers, n, cnt):
    ans = 0
    while True:
            if numbers[moduro] == 0:
                ans += moduro * math.factorial(cnt)
                break
            elif moduro == 0:
                break
            elif moduro == 1:
                ans += moduro * math.factorial(cnt)
                break
                
            else:
                cnt -= 1
                moduros = [moduro%s[i] for i in range(numbers[moduro])]
                for i in range(numbers[moduro]):
                    ans += calcmod(moduros[i], s, numbers, n, cnt)
                break
    return ans

def main():
    mod = 10 ** 9 + 7
    n, x = [int(i) for i in input().split()]
    s = [int(i) for i in input().split()]

    s.sort()
    numbers = [0 for i in range(10**5 + 1)]
    #numbers = [0 for i in range(23)]
    cnt = 0
    for i in range(n):
        numbers[s[i]] = 1
    for i in range(len(numbers)-1):
        numbers[i+1] += numbers[i]

    ans = 0
    moduro = [x%s[i] for i in range(n)]

    for i in range(len(moduro)):
        ans += calcmod(moduro[i], s, numbers, n, n-1)
    print(ans%mod)

main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import itertools
import sys
import math
from functools import lru_cache

# 1整数
# n = int(input())

# 空白区切り2変数
from queue import Queue
from operator import mul
from functools import reduce
from functools import lru_cache
input = sys.stdin.readline

# N個整数
n, x = list(map(int, input().split()))

# 1整数
# n = int(input())

# 空白区切りリスト整数
s = list(map(int, input().split()))
##とりあえずソート
s.sort(reverse=True)

zz = 1000000007


# N!を求める
@lru_cache(maxsize=1000)
def dn(n):
    if n >= 1:
        return 1
    return (n * dn(n - 1))%zz


# 黒板にkokubanが書かれているときに i番目以降を適用した場合の答え
@lru_cache(maxsize=2100000)
def amari(i, kokuban):
    if i == n:
        return kokuban
    if kokuban == 1:
        return dn(n-i)
    ret = 0

    ret += amari(i + 1, kokuban % s[i])
    bairitu = 1
    for di in range(1, n - i):
        bairitu *= n - i - di
        ret += bairitu * amari(i + di + 1, kokuban % s[i + di])
    return ret % zz


print(amari(0, x))

Submission status: Wrong Answer

Solution code:
from math import factorial
import sys
sys.setrecursionlimit(100000)
N, X = map(int, input().split())
S = [int(s) for s in input().split()]
S.sort()
mod = 7 + 10**9
mins = S[0]
fact = [1] * (N+1)
for i in range(1, N+1):
    fact[i] = (fact[i-1] * i) % mod

def modop(x, i, n, memo):
    if i == 0: return ((x % S[i]) * fact[n-1]) % mod
    try:
        return memo[(x, i, n)]
    except KeyError:
        nextx = x % S[i]
        ans = 0
        if nextx < mins: ans = (nextx * fact[n-1]) % mod
        else:
            for j in range(i):
                ans += modop(nextx, j, n-1, memo)
                ans %= mod
        memo[(x, i, n)] = ans
        return ans
memo = {}
Ans = 0
for i in range(N):
    Ans += modop(X, i, N, memo)
    Ans %= mod
print(Ans)

Submission status: Wrong Answer

Solution code:
import math

def calcmod(moduro, s, numbers, n, cnt):
    ans = 0
    while True:
            if numbers[moduro] == 0:
                ans += moduro * math.factorial(cnt)
                break
            elif moduro == 0:
                break

            else:
                cnt -= 1
                moduros = [moduro%s[i] for i in range(numbers[moduro])]
                for i in range(numbers[moduro]):
                    ans += calcmod(moduros[i], s, numbers, n, cnt)
                break
    return ans

def main():
    mod = 10 ** 9 + 7
    n, x = [int(i) for i in input().split()]
    s = [int(i) for i in input().split()]

    s.sort()
    numbers = [0 for i in range(10**5 + 1)]
    #numbers = [0 for i in range(23)]
    cnt = 0
    for i in range(n):
        numbers[s[i]] = 1
    for i in range(len(numbers)-1):
        numbers[i+1] += numbers[i]

    ans = 0
    moduro = [x%s[i] for i in range(n)]

    for i in range(len(moduro)):
        ans += calcmod(moduro[i], s, numbers, n, n-1)
    print(ans%mod)

main()

Submission status: Wrong Answer

Solution code:

n,x = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort(reverse = True)
li = [("",x)]
if n == 1:
    print(x % lis[0])
    exit()
if n == 2:
    print((x % lis[0]) % lis[1] + x % lis[1])
    exit()
for i in range(n):
    l = []
    if i != n-1:
        for num,nu in li:
            l.append((num,nu))
            l.append((num+str(i+1),nu % lis[i]))
    else:
        for num,nu in li:
            l.append((num+str(i+1),nu % lis[i]))
    li = l
li.sort()

num = [1]

for i in range(2,n):
    nu = []
    nnu = []
    h = 0
    for j in range(len(num)):
        nnu.append(num[j])
        if h == 0:
            h = num[-1] * i
            nu.append(i)
        else:
            nu.append(h // num[-(1+j)])
    num = nnu + nu

ans = 0

cou = 0
for tt,t in li:
    ans += t * num[cou//2]
    cou += 1
    ans %= 10 ** 9 + 7
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import itertools
import sys
import math
from functools import lru_cache

# 1整数
# n = int(input())

# 空白区切り2変数
from queue import Queue
from operator import mul
from functools import reduce
from functools import lru_cache
input = sys.stdin.readline

# N個整数
n, x = list(map(int, input().split()))

# 1整数
# n = int(input())

# 空白区切りリスト整数
s = list(map(int, input().split()))
##とりあえずソート
s.sort(reverse=True)

zz = 1000000007


# N!を求める
@lru_cache(maxsize=1000)
def dn(n):
    if n >= 1:
        return 1
    return n * dn(n - 1)


# 黒板にkokubanが書かれているときに i番目以降を適用した場合の答え
@lru_cache(maxsize=100000)
def amari(i, kokuban):
    if i == n:
        return kokuban
    if kokuban == 1:
        return dn(n-i)
    ret = 0

    ret += amari(i + 1, kokuban % s[i])
    bairitu = 1
    for di in range(1, n - i):
        bairitu *= n - i - di
        bairitu %= zz
        ret += bairitu * amari(i + di + 1, kokuban % s[i + di])
    return ret % zz


print(amari(0, x))

Submission status: Wrong Answer

Solution code:
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

MOD = 10**9+7
PPP = [1] * 201
for i in range(2,201):
    PPP[i] = PPP[i-1]*i % MOD
def P(n, r):
    return PPP[i] * modinv(n-r) % MOD

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

from functools import lru_cache

@lru_cache(maxsize=None)
def solve(x, idx):
    if idx==0:
        return x % MOD
    n = S[idx]
    ans = 0
    i = 0
    while S[i] < n:
        ans += solve(x%S[i], i) * P(idx+1, idx+1-i) % MOD
        ans %= MOD
        i += 1

    return ans

print(solve(X, N-1))

Submission status: Wrong Answer

Solution code:
import math

N, X = [int(i) for i in input().split()]
S = [int(i) for i in input().split()]

S.sort()

result = 0
for i in range(0, N):
    count = 0
    if i >= 1:
        for j in range(0, i):
            mod = X % S[i]
            mod = mod % S[j]
            for k in range(0, j):
                mod = mod % S[j - k]
            count += mod * math.factorial(N - 1 - j)
        result += count
    else:
        result += X % S[i] * math.factorial(N - 1)
    
print(result % 1000000007)

Submission status: Wrong Answer

Solution code:
import copy
import math

n,x = map(int,raw_input().split())
s = map(int,raw_input().split())

s.sort(reverse=True)
ans = 0

def dfs(xx,ss,l):
  ans = 0
  if len(ss)==1:
    return (xx % ss[0]) * math.factorial(l)

  for q in ss:
    x2 = xx % q
    s2 = copy.deepcopy(ss)
    s2.remove(q)

    for r in s2:
      if r > x2:
        s2.remove(r)
    if len(s2) == 0:
      ans += math.factorial( l-1 ) * x2
    elif x2 < min(s2):
      ans += math.factorial( l-1 ) * x2
    else:
      ans += dfs(x2,s2,l-1)
  return ans

print dfs(x,s,len(s)) % (10**9 + 7)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
from collections import deque
from math import factorial
mod = 1000000007


def comb(a, b):
    ans = 1
    for i in range(a, a - b, -1):
        ans *= i
    return ans // factorial(b)


def perm(a, b):
    return factorial(a) // factorial(a - b)


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

ans = 0
q = deque()
q.append((X, 1, N))
while len(q):
    x, fact, right = q.pop()
    leftp1 = bisect_left(S, x + 1, 0, right)
    if leftp1 <= 0:
        ans += x * fact * factorial(right)
        ans %= mod
    for i in range(leftp1):
        q.append((x % S[i], fact * perm(right - 1, right - i - 1) % mod, i))
        # print(q)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
import copy

def xxx(X, S):
    #print(X, S)
    if X < S[0]:
        #print(X * math.factorial(len(S)))
        return X * math.factorial(len(S))
    elif len(S) > 1:
        if X < S[1]:
            #print((X%S[0]) * math.factorial(len(S)-1) * (len(S)-1) )
            return (X%S[0]) * math.factorial(len(S)-1) * (len(S)-1) 
    
    count = math.factorial(len(S)-1) * (X%S[0])
    
    for i in range(1, len(S)):
        a = X%S[i]
        #count += math.factorial(len(S)-i) * a
        
        next_S = copy.deepcopy(S)
        next_S.pop(i)
        count += xxx(a, next_S)
        
    #print(count)
    return count


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

print(xxx(X, S)%(10 ** 9 +7))

Submission status: Wrong Answer

Solution code:
from math import factorial
import sys
sys.setrecursionlimit(100000)
N, X = map(int, input().split())
S = [int(s) for s in input().split()]
S.sort()
mod = 7 + 10**9
mins = S[0]
fact = [1] * (N+1)
for i in range(1, N+1):
    fact[i] = (fact[i-1] * i) % mod

def modop(x, y, i, n):
    if y == mins: return ((x % y) * fact[n-1]) % mod
    nextx = x % y
    if nextx < mins: return (nextx * fact[n-1]) % mod
    ans = 0
    for j in range(i):
        ans += modop(nextx, S[j], j, n-1)
        ans %= mod
    return ans

Ans = 0
for i in range(N):
    Ans += modop(X, S[i], i, N)
    Ans %= mod
print(Ans)

Submission status: Wrong Answer

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

MOD = 10 ** 9 + 7
S.sort()

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

F = [1] * (N + 1)
for i in range(1, N + 1):
  F[i] = F[i - 1] * i % MOD
IF = [0] * (N + 1)
IF[N] = power(F[N], MOD - 2)
for i in range(N - 1, -1, -1):
  IF[i] = IF[i + 1] * (i + 1) % MOD

def count(x, L):
  Ln = len(L)
  Ans = 0
  if Ln == 0:
    return x
  if L[0] > x:
    return x * F[Ln] % MOD
  for i in range(Ln):
    if L[i] > x:
      break
    else:
      n = Ln - i - 1
      Ans = Ans + count(x % L[i], L[:i]) * F[n] * F[i + n] * IF[n] * IF[i] % MOD
  return Ans % MOD

print(int(count(X, S) % MOD))

Submission status: Wrong Answer

Solution code:
from math import factorial
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def ni(): return int(stdin.readline())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
N, X = tuple(li())
nums = list(li())
nums.sort()
MAGIC = 10 ** 9 + 7
total = 0
def helper(ops, target, facter):
    if not ops: return
    
    L = len(ops)
    global total
    for i, op in enumerate(ops):
        # factorial of nums > op
        new_f = factorial(L-1 - i)
        down_factor = new_f * facter
        if i == 0:
            res = target % op * down_factor
            total += res
            total = total % MAGIC
            
        helper(ops[:i], target % op, down_factor)
helper(nums, X, 1)
print(total)

Submission status: Wrong Answer

Solution code:
from math import factorial
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def ni(): return int(stdin.readline())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
N, X = tuple(li())
nums = list(li())
nums.sort()
MAGIC = 10 ** 9 + 7
total = 0
def helper(ops, target, facter):
    if not ops: return
    print(ops, target, facter)
    L = len(ops)
    global total
    for i, op in enumerate(ops):
        # factorial of nums > op
        new_f = factorial(L-1 - i)
        down_factor = new_f * facter
        if i == 0:
            res = target % op * down_factor
            total += res
            total = total % MAGIC
            print(op, res)
        helper(ops[:i], target % op, down_factor)
helper(nums, X, 1)
print(total)

Submission status: Wrong Answer

Solution code:
import math
N, X = map(int, input().split())
S = list(map(int, input().split()))

ans = 0


def solve(layer, now_X, remain):
    global ans

    if len(remain) == 1:
        ans += now_X % remain[0]
    else:
        for i, s in enumerate(remain):
            next_X = now_X % s
            next_remain = remain[:]
            next_remain.pop(i)
            if next_X < min(next_remain):
                ans += math.factorial(len(next_remain)) * next_X
            else:
                solve(layer=layer+1, now_X=next_X, remain=next_remain)

solve(layer=1, now_X=X, remain=S)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
N,X = map(int,input().split())
L = list(map(int,input().split()))
L.sort()
p = L[0]
ans = 0
for i in range(N):
  while X%L[i] > p:
    L[i] = X%L[i]
  L[i] = X%L[i]
for i in range(N):
  if L[i] != p:
    if L[i] != 0:
      ans += L[i]*math.factorial(N-1)%(10**9+7)
print(ans%(10**9+7))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import itertools
import sys
import math
from functools import lru_cache

# 1整数
# n = int(input())

# 空白区切り2変数
from queue import Queue
from operator import mul
from functools import reduce
from functools import lru_cache
input = sys.stdin.readline

# N個整数
n, x = list(map(int, input().split()))

# 1整数
# n = int(input())

# 空白区切りリスト整数
s = list(map(int, input().split()))
##とりあえずソート
s.sort(reverse=True)

zz = 1000000007


# N!を求める
@lru_cache(maxsize=1000)
def dn(n):
    if n >= 1:
        return 1
    return n * dn(n - 1)


# 黒板にkokubanが書かれているときに i番目以降を適用した場合の答え
@lru_cache(maxsize=2100000)
def amari(i, kokuban):
    if i == n:
        return kokuban
    if kokuban == 1:
        return dn(n-i)
    ret = 0

    ret += amari(i + 1, kokuban % s[i])
    bairitu = 1
    for di in range(1, n - i):
        bairitu *= n - i - di
        ret += bairitu * amari(i + di + 1, kokuban % s[i + di])
    return ret % zz


print(amari(0, x))

Submission status: Wrong Answer

Solution code:
N, X = map(int, input().split())
S = sorted(list(map(int, input().split())))
MIN = S[0]
MOD = 10 ** 9 + 7


factorial = [1]
for i in range(1, N+2):
    factorial.append((factorial[-1] * i) % MOD)


stack = [(X, 0)]
ans = 0
while stack:
    X, cnt = stack.pop()
    for s in S:
        next_X, next_cnt = X % s, cnt + 1
        if next_X < MIN:
            ans += next_X * factorial[N - next_cnt]
            ans %= MOD
        else:
            if X == next_X:
                break
            stack.append((next_X, next_cnt))

print(ans % MOD)

Submission status: Wrong Answer

Solution code:
import math
 
def aaa(sn,x,kotae):
    print(sn, x, kotae)
    #xによる
    for snn in range(len(sn)):
        more=len(sn)-1
        amari=x%sn[snn]
        if amari>=sn[0]:
            wo=aaa(sn[:snn]+sn[snn+1:],amari,kotae)
            kotae=wo
        else:
            #print('B:', kotae,amari*math.factorial(more))
            kotae+=(amari*math.factorial(more))%((10**9)+7)
    return kotae
 
def main():
    kotae=0
    rr=input().split(" ")
    n=int(rr[0])
    x=int(rr[1])
    rr=input().split(" ")
    sn=[]
    for r in rr:
        sn.append(int(r))
    sn=sorted(sn)
    kotae=aaa(sn,x,kotae)
    print(kotae)
 
main()

Submission status: Wrong Answer

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

MOD = 10 ** 9 + 7
S.sort()

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

F = [1] * (N + 1)
for i in range(1, N + 1):
  F[i] = F[i - 1] * i % MOD
IF = [0] * (N + 1)
IF[N] = power(F[N], MOD - 2)
for i in range(N - 1, -1, -1):
  IF[i] = IF[i + 1] * (i + 1) % MOD

def count(x, L):
  Ln = len(L)
  Ans = 0
  if Ln == 0:
    return x
  if L[0] > x:
    return x * F[Ln] % MOD
  for i in range(Ln):
    if L[i] > x:
      break
    else:
      n = Ln - i - 1
      Ans += count(x % L[i], L[:i]) * F[n] * F[i + n] * IF[n] * IF[i] % MOD
  return Ans % MOD

print(count(X, S))

Submission status: Wrong Answer

Solution code:
N, X = map(int, input().split())
SS = input()
S = SS.split(" ")
count = 0
print(S)

for i in range(N):
    S[i] = int(S[i])

def copy(s):
    ss = []
    for i in s:
        ss.append(i)
    
    return ss

def a(S, n, X):
    global count
    for i in range(n):
        s = copy(S)
        b = s.pop(i)
        if len(s) == 0:
            count += X % b
        else:
            a(s, n - 1, X % b)

a(S, N, X)

print(count % (10 ** 9 + 7))

Submission status: Wrong Answer

Solution code:
import math

def search(s, n, x, count):
    if(len(s) == n):
        if(count == len(s)): return x
        else: return math.factorial((len(s)-count)) * x

    ans = 0
    for i in range(n, len(s), 1):
        if(x >= s[i]):
            ans += search(s, i+1, x%s[i], count+1)
        elif(i == len(s)-1):
            if(count == len(s)): ans += x
            else: ans += math.factorial((len(s)-count)) * x

    return ans



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

s = sorted(s)[::-1]

ans = search(s, 0, x, 0)

print(ans % int(1e9+7))

Submission status: Wrong Answer

Solution code:
# encoding: utf-8
import sys
input = sys.stdin.readline

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

S.sort(key=lambda x: -x)
N_mod = S[-1]

tab = [1]
tmp = 1
for i in range(N):
    tmp = (tmp * (i + 1)) % (10 ** 9 + 7)
    tab.append(tmp)

# print("#", S)
# print("#", tab)
def count(pos, tmp, cnt):
    if tmp == N_mod:
        return 0
    elif tmp < N_mod:
        return (tmp * tab[N - cnt]) % (10 ** 9 + 7)
    else:
        # print("##", pos, tmp, cnt)
        ret = 0
        if pos < N - 1:
            ret = (ret + count(pos + 1, tmp, cnt)) % (10 ** 9 + 7)
        if tmp >= S[pos]:
            ret = (ret + count(pos + 1, tmp % S[pos], cnt + 1)) % (10 ** 9 + 7)
        # print("##", ret)
        return ret

print(count(0, X, 0))

Submission status: Wrong Answer

Solution code:
# D
N, a = map(int, input().split())
b_list = list(map(int, input().split()))

LARGE = 10**9 + 7

modulo = [0]*(201)
modulo[0] = 1
for i in range(1, 201):
    modulo[i] = i * modulo[i-1] % LARGE

def remove_list(b_list, i):
    return b_list[:i] + b_list[i+1:]
    
def solve(a, b_list):
    b_list_l = [b for b in b_list if b <= a]
    
    if len(b_list_l) == 1:
        #print(1, a, b_list_l[0])
        return ((a % b_list_l[0]) * modulo[len(b_list) - 1]) % LARGE
    elif len(b_list_l) == 0:
        #print(2, a, a)
        return a * modulo[len(b_list)] % LARGE
    else:
        #print(a, b_list)
        return modulo[len(b_list) - len(b_list_l)] * sum([solve(a % b_list_l[i], remove_list(b_list_l, i)) for i in range(len(b_list_l))]) % LARGE
    
print(solve(a, b_list))

Submission status: Wrong Answer

Solution code:
# D
N, a = map(int, input().split())
b_list = list(map(int, input().split()))

LARGE = 10**9 + 7

modulo = [0]*(201)
modulo[0] = 1
for i in range(1, 201):
    modulo[i] = i * modulo[i-1] % LARGE

def remove_list(b_list, i):
    return b_list[:i] + b_list[i+1:]
    
def solve(a, b_list):
    b_list_l = [b for b in b_list if b <= a]
    
    if len(b_list_l) == 1:
        #print(1, a, b_list_l[0])
        return (a % b_list_l[0]) * modulo[len(b_list) - 1]
    elif len(b_list_l) == 0:
        #print(2, a, a)
        return a * modulo[len(b_list)] % LARGE
    else:
        #print(a, b_list)
        return modulo[len(b_list) - len(b_list_l)] * sum([solve(a % b_list_l[i], remove_list(b_list_l, i)) for i in range(len(b_list_l))]) % LARGE
    
print(solve(a, b_list))

Submission status: Wrong Answer

Solution code:
import bisect

N, X = map(int, input().split())
S = [int(s) for s in input().split()]
S.sort()
NN = 10 ** 9 + 7

fact = [1]
for i in range(1, N+1):
    fact.append((fact[i-1] * i) % NN)

def f(n, s):
    if len(s) == 0:
        return 0
    if len(s) == 1:
        result = (n % s[0]) % NN
    elif n < s[0]:
        result = (n * fact[len(s)]) % NN
    elif n >= s[-1]:
        result = sum([f(n % s[i], s[:i] + s[i+1:]) for i in range(len(s))]) % NN
    else:
        ib = bisect.bisect_right(s, n)
        size_r = len(s[ib:])
        s_l = s[:ib]
        result = (fact[size_r] * f(n, s_l)) % NN
    return result

print(f(X, S))

Submission status: Wrong Answer

Solution code:
from math import factorial
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def ni(): return int(stdin.readline())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
N, X = tuple(li())
nums = list(li())
nums.sort()
MAGIC = 10 ** 9 + 7
total = 0
def helper(ops, target):
    if not ops: return
    print(ops, target)
    L = len(ops)
    global total
    for i, op in enumerate(ops):
        
        # factorial of nums > op
        res = target % op * factorial(L-1 - i) if i < L -1 or L == 1 else 0
        print(op, res)
        helper(ops[:i], target % op)
        total += res
        total = total % MAGIC
helper(nums, X)
print(total)

Submission status: Wrong Answer

Solution code:
n,x = map(int,input().split())
S = list(map(int,input().split()))

S.sort()

minS = S[0]

prod = 1
for i in range(1,n+1):
    prod *= i




remain = [x]
cnt = n
res = 0
D = 10**9 + 7
while remain:
    prod//=cnt
    mo =[]
    for num in remain:
        for s in S:
            if s > num:
                break
            m = num%s
            if m < minS:
                res = (res + (m*prod)%D)%D
            else:
                mo.append(m)
    cnt -= 1
    remain = mo
    #print(remain)

print(res)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here
import math

def aaa(sn,x,kotae):
    #print(sn, x, kotae)
    #xによる
    for snn in range(len(sn)):
        more=len(sn)-1
        amari=x%sn[snn]
        if amari>=sn[0]:
            wo=aaa(sn[:snn]+sn[snn+1:],amari,kotae)
            kotae=wo
        else:
            #print('B:', kotae,amari*math.factorial(more))
            kotae+=(amari*math.factorial(more))%((10**9)+7)
    return kotae

def main():
    kotae=0
    rr=input().split(" ")
    n=int(rr[0])
    x=int(rr[1])
    rr=input().split(" ")
    sn=[]
    for r in rr:
        sn.append(int(r))
    sn=sorted(sn)
    print(sn)
    kotae=aaa(sn,x,kotae)
    print(kotae)

main()

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

import math

mod = 10**9+7

n,x = na()
ss = na()

ss.sort()
ans = 0

for i in range(n):
  a = x
  for j in range(i+1):
    a %= ss[i-j]
  a = a * math.factorial(n-i-1) % mod
  ans += a
  ans %= mod

print(ans)

Submission status: Wrong Answer

Solution code:
N,Xorigin = (int(i) for i in input().split())
hoge = input().split()
hoge = [int(s) for s in hoge]
minimum = min(hoge)
result = 0
import itertools
for i in itertools.permutations(hoge):
    X = Xorigin
    i = list(i)
    for j in i:
        X = X % j
        if X < minimum:
            break
        elif X == minimum:
            X = 0
            break
    result = result + X
print(result)

Submission status: Wrong Answer

Solution code:
import bisect

N, X = map(int, input().split())
S = [int(s) for s in input().split()]
S.sort()
NN = 10 ** 9 + 7

fact = [1]
for i in range(1, N+1):
    fact.append((fact[i-1] * i) % NN)

def f(n, s):
    if len(s) == 0:
        return 0
    if len(s) == 1:
        result = (n % s[0]) % NN
    elif n < s[0]:
        result = n * fact[len(s)] % NN
    else:
        ib = bisect.bisect_right(s, n)
        result_r = f(n, s[ib:])
        result_l = sum([f(n % s[i], s[:i] + s[i+1:]) for i in range(len(s[:ib]))]) % NN
        result = (result_r + result_l) % NN
    return result

print(f(X, S))

Submission status: Wrong Answer

Solution code:
import math
n,x=map(int,input().split())
s=list(map(int,input().split()))
s.sort()
p=10**9+7
r=(((s[0]-1)*s[0])//2)%p
t=math.factorial(n-1)%p
print(r*t)

Submission status: Wrong Answer

Solution code:
import math

N, X = map(int, input().split())
s = map(int, input().split())
s = sorted(s)[::-1]
result = 0

def under(x, s):
    return [n for n in s if n <= x]
    
def do(x, s):
    global result
    for n in s:
        a = x % n
        new_s = under(a, s)
        if len(new_s) == 0:
            result += a * math.factorial(len(s) - 1)
        else:
            do(a, new_s)

do(X, s)
print(result % (10**9 + 7))