Problem:


Score : 300 points

Problem Statement

Snuke has N dogs and M monkeys. He wants them to line up in a row.

As a Japanese saying goes, these dogs and monkeys are on bad terms. ("ken'en no naka", literally "the relationship of dogs and monkeys", means a relationship of mutual hatred.) Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys.

How many such arrangements there are? Find the count modulo 10^9+7 (since animals cannot understand numbers larger than that).
Here, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished.

Constraints

1 ≤ N,M ≤ 10^5

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of possible arrangements, modulo 10^9+7.

Sample Input 1

2 2

Sample Output 1

8

We will denote the dogs by A and B, and the monkeys by C and D. There are eight possible arrangements: ACBD, ADBC, BCAD, BDAC, CADB, CBDA, DACB and DBCA.

Sample Input 2

3 2

Sample Output 2

12

Sample Input 3

1 8

Sample Output 3

0

Sample Input 4

100000 100000

Sample Output 4

530123477



Sample input:
2 2


Sample output:
8

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())

if abs(n-m)>1:
    ans = 0
else:
    if m > n:
        n, m = m, n
        print(n, m)

    total_n = 1
    for i in range(1,n+1):
        total_n *= i
        if total_n >= 1000000007:
            total_n = total_n % 1000000007

    if n != m:
        ans = (total_n * total_n / n) % 1000000007
    else:
        ans = (total_n * total_n * 2) % 1000000007

print(int(ans))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())

if abs(n-m)>1:
    ans = 0
else:
    if m > n:
        n, m = m, n

    total_n = 1
    for i in range(1,n+1):
        total_n *= i
        if total_n >= 1000000007:
            total_n = total_n % 1000000007

    if n != m:
        total_m = total_n / n
        ans = (total_m * total_n) % 1000000007
    else:
        ans = (total_n * total_n * 2) % 1000000007

print(int(ans))

Submission status: Wrong Answer

Solution code:

def resolve():
	n, m = map(int, input().split())
	mod = 10**9+7
	if abs(n - m) > 1:
		print(0)
		return
	print((n % mod) * (m % mod) * 2 % mod)
resolve()

Submission status: Wrong Answer

Solution code:
from math import factorial
n,m = map(int,input().split())
if abs(n-m) >= 2:
  print(0)
elif abs(n-m) == 1:
  print((factorial(n)%(10**9+7))*(factorial(m)%(10**9+7)))
else:
  print((2*(factorial(n) % (10**9+7))**2) %(10**9+7))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
ans=1
if abs(n-m)>=2:
    print(0)
    exit()
for i in range(1,max(n,m)+1):
    ans*=i
    ans%=(10**9+7)
for i in range(1,min(n,m)+1):
    ans*=i
    ans%=(10**9+7)
ans=(ans*2)%(10**9+7)
print(ans)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
import math 
mod=10**9+7
if n==m:
    x=math.factorial(n)
    x=x%mod
    x=x**2
    x%=mod
    x*=2
    x%=mod
    print(x)
elif abs(n-m)==1:
    x=min(n,m)
    y=max(n,m)
    ans=math.factorial(x)%mod
    ans*=ans
    ans%=mod
    ans=ans*2*y
    ans%=mod
    print(ans)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
mod = 10 ** 9 + 7
if abs(n - m) > 1:
  print(0)
if n == m:
  ans = 2
  for i in range(1,n+1):
    ans *= i*i
    ans %= mod
  print(ans)
else:
  ans = 1
  for i in range(1,min(n,m) + 1):
    ans *= i*i
    ans %= mod
  ans *= max(n,m)
  ans %= mod
  print(ans)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
if abs(n-m)>1:
  print(0)
  exit()
import math
#print(math.factorial(4))
if abs(n-m)==1:
  n_=math.factorial(n)%(10**9+7)
  m_=math.factorial(m)%(10**9+7)
  print((n_*m_)%(10e+9+7))
if abs(n-m)==0:
  n_=math.factorial(n)%(10**9+7)
  m_=math.factorial(m)%(10**9+7)
  print((2*n_*m_)%(10**9+7))

Submission status: Wrong Answer

Solution code:
n,m = list(map(int, input().split()))
mod = 10**9+7

def factorial(n):
    val = 1
    for i in range(2, n + 1):
        val *= i
        val %= mod
    return val

if abs(n-m)>1:
    print(0)
elif abs(n-m)==1:
    num = factorial(max(m,n))
    print(num**2//max(m,n)%mod)
else:
    num = factorial(m)
    print(num**2*2%mod)

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
fact = [i for i in range(10**5+1)]
fact[0] = 1
for i in range(10 ** 5-1):
  fact[i+1] *= fact[i]
  fact[i+1] %= mod
n,m = map(int,input().split())
if abs(n-m)>1:
  print(0)
else:
  if n!=m:
    print(fact[n]*fact[m]%mod)
  else:
    print(fact[n]*fact[m]*2%mod)

Submission status: Wrong Answer

Solution code:
import math

def permutations(a,b):
    return math.factorial(a)//math.factorial(a-b)

n, m = map(int, input().split())
if abs(n-m)>2:
    print(0)
elif abs(n-m)==1:
    print(permutations(n,n)*permutations(m,m)%(10**9+7))
else:
    print(permutations(n,n)*permutations(m,m)*2%(10**9+7))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())

if n-m != 0 or abs(n-m) != 1:
  print(0)
else:
  print((2*n*m)%(int(1e9)+7))

Submission status: Wrong Answer

Solution code:
N = 10**6
mod = 10**9 + 7
fact = [1]
for i in range(1, N+1):
    fact.append(fact[-1]*i%mod)

n, m = map(int, input().split())
ans = 0
if n - m > 1 or m - n > 1:
    ans = 0
elif n - m == 0:
    ans = 2 * fact[n] * fact[m]
else:
    ans = fact[n] * fact[m]
print(ans)

Submission status: Wrong Answer

Solution code:
N = 10**6
mod = 10**9 + 7
fact = [1]
for i in range(1, N+1):
    fact.append(fact[-1]*i%mod)

n, m = map(int, input().split())
ans = 0
if n - m > 1 or m - n > 1:
    ams = 0
elif n - m == 0:
    ans = 2 * (fact[n])**2
else:
    ans = fact[n] * fact[m]
print(ans)

Submission status: Wrong Answer

Solution code:
import math
n,m = map(int,input().split())
if abs(n-m) > 1:
  print(0)
elif n == m:
  print((math.factorial(n)**2)*2 % 1000000007)
else:
  print(math.factorial(max(n,m))*math.factorial(min(n,m)) % 100000007)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
if abs(n-m) > 1:
  print(0)
elif n == m:
  cnt = 1
  for i in range(2,n+1):
    cnt *= i
  print((cnt**2)*2 % 1000000007)
else:
  a = max(n,m)
  cnt = 1
  for i in range(2,a+1):
    cnt *= i
  b = min(n,m)
  cnt2 = 1
  for i in range(2,b+1):
    cnt2 *= i
  print(cnt*cnt2 % 100000007)

Submission status: Wrong Answer

Solution code:
def resolve():
    '''
    code here

    '''
    import math

    N, M = [int(item) for item in input().split()]

    deff = N - M

    if deff == -1:
        N, M = M, N

    def factorial(a):
        if a <= 1:
            return 1
        return a*factorial(a-1)


    if deff == 0:
        temp = math.factorial(N)
        res = (temp**2 * 2) % (10**9+7)

    elif deff == 1:
        temp = math.factorial(N)
        res = (temp**2 // N) % (10**9+1)

    else:
        res = 0
    
    print(res)

if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    '''
    code here

    '''
    import sys
    sys.setrecursionlimit(10**5+5)

    N, M = [int(item) for item in input().split()]

    deff = N - M

    if deff == -1:
        N, M = M, N

    def factorial(a):
        if a <= 1:
            return 1
        return a*factorial(a-1)


    if deff == 0:
        temp = factorial(N)
        res = (temp**2 * 2) % (10**9+7)

    elif deff == 1:
        temp = factorial(N)
        res = (temp**2 // N) % (10**9+1)

    else:
        res = 0
    
    print(res)

if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    '''
    code here
    考え方がポイントの問題
    aの先頭からj番目にjを挿入する
    →先頭からj番目の数字がjのときにjの最大値を列から削除する

    '''
    import sys
    sys.setrecursionlimit(10**6)

    N, M = [int(item) for item in input().split()]

    deff = N - M

    if deff == -1:
        N, M = M, N

    def factorial(a):
        if a == 1:
            return 1
        return a*factorial(a-1)


    if deff == 0:
        temp = factorial(N)
        res = (temp**2 * 2) % (10**9+1)
    elif deff == 1:
        temp = factorial(N)
        res = (temp**2 // N) % (10**9+1)

    else:
        res = 0
    
    print(res)

if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
import sys
from io import StringIO
import unittest

import math

def resolve():
    n, m = map(int, input().split())
    div = 10**9 + 7

    # n! * m! * 2
    if abs(n - m) > 1:
        print(0)
    elif n % 2 == 0 and m % 2 ==0:
        print((math.factorial(n) * math.factorial(m) * 2) % div)
    else:
        print((math.factorial(n) * math.factorial(m)) % div)

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """2 2"""
        output = """8"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """3 2"""
        output = """12"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """1 8"""
        output = """0"""
        self.assertIO(input, output)
    def test_入力例_4(self):
        input = """100000 100000"""
        output = """530123477"""
        self.assertIO(input, output)

if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
MOD = 10**9+7

def factorial(n):
    ans=1
    for i in range(1, n+1):
        ans = (ans * i) % MOD
    return ans

if N == M:
    print((2* factorial(N) ** 2) % MOD)
elif N == M + 1:
    print(factorial(M)**2*N)
elif M == N + 1:
    print(factorial(N)**2*M)
else:
    print(0)

Submission status: Wrong Answer

Solution code:

hoge = 10 ** 9 + 7
def test():
	n, m = map(int, input().split())
	if abs(n-m) > 1:
		print(0)
		exit()
	i_n = n
	i_m = m
	np = 1
	mp = 1
	for i in range(1,i_n):
		np = np * i_n
	for i in range(1,i_m):
		mp = mp * i_m

	s = 2 if n == m else 1
	r = (mp % hoge) * (np % hoge) * (s % hoge)
	print(r)

if __name__ == '__main__':
    test()

Submission status: Wrong Answer

Solution code:
hoge = 10 ** 9 + 7
def test():
	n, m = map(int, input().split())
	if abs(n-m) > 1:
		print(0)
		exit()
	i_n = n
	i_m = m
	np = 1
	mp = 1
	while True:
		np = np * i_n
		mp = mp * i_m
		i_n -= 1
		i_m -= 1
		if i_n < 1:
			i_n = 1
		if i_m < 1:
			i_m = 1
		if i_n == 1 and i_m == 1:
			break
	s = 1
	r = (mp % hoge) * (np % hoge) * (s % hoge)
	print(r)

if __name__ == '__main__':
    test()

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
a = n-m
ans = 0
if abs(a) > 1:
    ans = 0
elif abs(a) == 1:
    x = 1
    if a < 0:
        s = n
    else:
        s = m
    for i in range(1,m+1):
        x *= i % (10**9+7)
    ans = x * (x *(i+1)) %(10**9+7)
else:
    x = 1
    for i in range(1,n+1):
        x *= i % (10**9+7)
    ans = 2*x **2  %(10**9+7)
print(ans)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
import math

if abs(n-m) > 1:
    print(0)
else:
    a=math.factorial(n)
    b=math.factorial(m)
    print((a*b)%(10**9+7))

Submission status: Wrong Answer

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
from collections import Counter, deque
#from scipy.misc import comb

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

if N < M: N,M = M,N

ans = 1
if N-M == 0:
    for i in range(1,M+1):
        ans *= i
        ans % MOD
    ans = (ans*ans*2)%MOD
elif N-M == 1:
    for i in range(1,M+1):
        ans *= i
        ans %= MOD
    ans = (ans*ans)%MOD
else: ans = 0

print(ans)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
from collections import Counter, deque
#from scipy.misc import comb

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

if N < M: N,M = M,N

ans = 1
if N-M == 0:
    for i in range(M):
        ans *= i
        ans % MOD
    ans = (ans*ans)%MOD
elif N-M == 1:
    for i in range(M):
        ans *= i
        ans %= MOD
    ans = (ans*ans*2)%MOD
else: ans = 0

print(ans)

Submission status: Wrong Answer

Solution code:
mod = 10**9+7

def mod_factorial(n):
  ret = 1
  for i in range(1, n+1):
    ret = ret * i % mod
  return ret
  
def main():
  n, m = map(int, input().split())
  if abs(n-m) > 1:
    return 0
  
  l = max(n, m)
  f = mod_factorial(l-1)
  
  if n == m:
    return(2*f*f%mod)
  else:
    return(f*f*l%mod)

print(main())

Submission status: Wrong Answer

Solution code:
mod = 10**9+7

def mod_factorial(n):
  ret = 1
  for i in range(1, n+1):
    ret = ret * i % mod
  return ret
  
def main():
  n, m = map(int, input().split())
  if abs(n-m) > 1:
    return 0
  
  l = min(n, m)
  f = mod_factorial(n)
  if n == m:
    return(2*f*f%mod)
  else:
    return(f*f*l%mod)

print(main())

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())

from math import factorial

if n==m:
	ans = factorial(n)*factorial(m)*2
else:
	ans = factorial(n)*factorial(m)
print(ans%(10**9+7))

Submission status: Wrong Answer

Solution code:
import math
n,m=map(int,input().split())
mod=10**9+7
a=math.factorial(n)*math.factorial(m)
if abs(n-m)==0:
    print(2*a%mod)
elif abs(n-m)>1:
    a=0
print(a%mod)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
mod=10**9+7
import math
a=math.factorial(n)*math.factorial(m)
if abs(n-m)==0:
    print(2*a%mod)
elif abs(n-m)>1:
    a=0
print(a%mod)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
mod=10**9+7
import math
a=math.factorial(n)*math.factorial(m)
if abs(n-m)==0:
    print(2*a%mod)
elif abs(n-m)>1:
    a=0
print(a)

Submission status: Wrong Answer

Solution code:
import math
N, M = [int(i) for i in input().split()]
r = pow(10, 9) + 7
L = abs(N - M)

if L >= 2:
    print(0)
else:
    E = math.factorial(N)*math.factorial(M)
    print(int(E % r))

Submission status: Wrong Answer

Solution code:
import math
N, M = [int(i) for i in input().split()]
r = pow(10, 9) + 7
L = abs(N - M)

if L >= 2:
    print(0)
else:
    E = 2*math.factorial(N)*math.factorial(M)
    print(int(E % r))

Submission status: Wrong Answer

Solution code:
N, M = [int(i) for i in input().split()]
r = pow(10, 9) + 7
L = abs(N - M)

if L >= 2:
    print(0)
else:
    E = 2*N*M
    print(int(E % r))

Submission status: Wrong Answer

Solution code:
N, M = [int(i) for i in input().split()]

L = abs(N - M)

if L >= 2:
    print(0)
else:
    E = 2*N*M
    print(E)

Submission status: Wrong Answer

Solution code:
N, M = [int(i) for i in input().split()]

L = abs(N - M)

if L >= 2:
    print(0)

Submission status: Wrong Answer

Solution code:
from math import factorial

n, m = [int(i) for i in input().split()]
mod = 10 ** 9 + 7

ans = 0
diff = abs(n - m)
if diff == 0:
    ans = (factorial(n) * 2) * 2 % mod
elif diff == 1:
    ans = factorial(n) * factorial(m) % mod
print(ans)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())

if abs(n-m)>=2:
    print(0)
elif abs(n-m) ==1:
    x =max(n,m)
    for i in range(1,min(n,m)+1):
        x = x*i**2%(10**9+7)
        print(x)
else:
    x =2
    for i in range(1,n+1):
        x = x*i**2%(10**9+7)
        print(x)

Submission status: Wrong Answer

Solution code:
n, m = list(map(int, input().split()))
ans = 1
a = lambda x, y: x*y % (10**9+7)
if n==m:
    for i in range(1, n+1):
        a(ans, i)
    print((ans**2*2)%(10**9+7))
elif abs(n-m)==1:
    for i in range(1, n+1):
        a(ans, i)
    for i in range(1, m+1):
        a(ans, i)
    print(ans%(10**9+7))
else:
    print(0)

Submission status: Wrong Answer

Solution code:
import sys
import math

N, M = map(int, input().split())

ans = 0

if (N + M) % 2 == 0:
  if N != M:
    print(0)
    sys.exit()

  ans = (math.factorial(N) * 2 * 2) % (10**9 + 7)

else:
  if not N == M - 1 and not N - 1 == M:
    print(0)
    sys.exit()

  ans = (math.factorial(N) * math.factorial(M)) % (10**9 + 7)

print(ans)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
if abs(n-m) > 1:
  print(0)
  exit()
  
mod = 10**9+7

if n==m:
  ans = 4
  for i in range(1,n+1):
    ans *= i
    ans %= mod
  print(ans%mod)
  
else:
  if m > n:
    n,m = m,n
    ans = 2
    a =1
    for i in range(1,m+1):
      a *= i
      a %= mod
      
    ans *= a*2*n
    ans %= mod
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math

n,m=map(int,input().split())
mod=10**9+7
if abs(n-m)>1:
	print("0")
elif (n==m):
	val=math.factorial(n)
	print((val*4)%mod)
else:
	valn=math.factorial(n)
	valm=math.factorial(m)
	print((valn*valm)%mod)

Submission status: Wrong Answer

Solution code:
from math import factorial
n, m = map(int,input().split())
if abs(n-m) >=2:
    print(0)
elif abs(n-m) == 1:
    print((factorial(n)*factorial(m)) % int(1e9)+7)
else:
    print((factorial(n)*factorial(m)*2) % (int(1e9)+7))

Submission status: Wrong Answer

Solution code:
import math

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

d = 10 ** 9 + 7

fn = math.factorial(n) % d
fm = math.factorial(m) % d

if abs(n - m) > 2:
	print(0)
elif abs(n - m) == 1:
	print((fn * fm) % d)
elif abs(n - m) == 0:
	print((2 * fn * fm) % d)

Submission status: Wrong Answer

Solution code:
from math import factorial
n,m=map(int,input().split())
mod=10**9+7
f=factorial(n)
if abs(n-m)>1:
  print(0)
elif abs(n-m)==1:
  print((f**2)%mod)
else:
  print((2*(f**2))%mod)

Submission status: Wrong Answer

Solution code:
from math import factorial
n,m=map(int,input().split())
mod=10**9+7
f=factorial(n)
if abs(n-m)>1:
  print(0)
elif abs(n-m)==1:
  print((f**2)%mod)
else :
  print((2*f**2)%mod)

Submission status: Wrong Answer

Solution code:
from math import factorial
n,m=map(int,input().split())
if abs(n-m)>2:
  print(0)
elif n==m:
  print(2*factorial(n)*factorial(m))
elif abs(n-m)==1:
  print(factorial(n)*factorial(m))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
def perm(n):
    a=1
    for i in range(1,n+1):
        a*=i*i
        a%=10**9+7
    return a

if n==m:
    print((perm(n)*2)%(10**9+7))
elif abs(n-m)==1:
    print(perm(n)%(10**9+7))
else:
    print(0)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
def perm(n):
    a=1
    for i in range(1,n+1):
        a=(a*i)%(10**9+7)
    return a

if n==m:
    print((perm(n)*4)%(10**9+7))
elif abs(n-m)==1:
    print((perm(n)*perm(m))%(10**9+7))
else:
    print(0)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from math import factorial
mod=10**9+7
n,m=map(int,input().split())
if n>m:
    n,m=m,n
if n+1==m:
    n=factorial(n)%mod
    m=factorial(m)%mod
    print(n*m)
elif n==m:
    n=factorial(n)%mod
    m=factorial(m)%mod
    print(2*n*m%mod)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
p = 10 ** 9 + 7
N = (10 ** 5)*2 +10  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

n,m = map(int,input().split())
if abs(n-m) > 1:
    print(0)
elif n==m:
    m1 = fact[n]
    print((m1**2)*2)
else:
    m1 = fact[n]
    m2 = fact[m]
    print(m1,m2)

Submission status: Wrong Answer

Solution code:
p = 10 ** 9 + 7
N = (10 ** 5)*2 +10  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

n,m = map(int,input().split())
if abs(n-m) > 1:
    print(0)
else:
    print(cmb(n+m,n,10**9+7))

Submission status: Wrong Answer

Solution code:
M,D=map(int,input().split())
MX = 10**9+7
 
import math
if abs(M - D) >= 2:
  print(0)
elif abs(M - D) == 1:
  ANS=math.factorial(M) * math.factorial(D)
  if ANS >= MX:
    ANS %= MX
  print(ANS)
else:
  ANS=math.factorial(M)
  if ANS >= MX:
    ANS %= MX
  print(ANS * ANS * 2)

Submission status: Wrong Answer

Solution code:
M,D=map(int,input().split())
import itertools
MI=min(M,D)
MX=max(M,D)

#A=len(list(itertools.permutations(range(MX), MX)))
#B=len(list(itertools.permutations(range(MI), MI)))
#C=len(list(itertools.permutations(range(MX+1), MI)))
import math
print(math.factorial(MX) * len(list(itertools.permutations(range(MX+1), MI))) - len(list(itertools.permutations(range(MI), MI))))
print(len(list(itertools.permutations(range(MX+1), MI))))

Submission status: Wrong Answer

Solution code:
import sys
n,m=map(int,input().split())
num=n
mum=m
if 2<=abs(n-m):
  print(0)
  sys.exit()
if abs(n-m)==1:
  while n>1:
    num*=n-1
    n-=1
    num=num%((10**9)+7)
  while m>1:
    mum*=m-1
    m-=1
    mum=mum%((10**9)+7)
  print(num*mum)
  sys.exit()
if abs(n-m)==0:
  while n>1:
    num*=n-1
    n-=1
    num=num%((10**9)+7)
  print((2*(num**2))%((10**9)+7))
  sys.exit()

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
num=n
mum=m
if 2<=abs(n-m):
  print(0)
if abs(n-m)==1:
  while n>1:
    num*=n-1
    n-=1
    num=num%((10**9)+7)
  while m>1:
    mum*=m-1
    m-=1
    mum=mum%((10**9)+7)
  print(num*mum)
if abs(n-m)==0:
  while n>1:
    num*=n-1
    n-=1
    num=num%((10**9)+7)
  print((2*(num**2))%((10**9)+7))

Submission status: Wrong Answer

Solution code:
from math import factorial

N,M = map(int, input().split())
mod = 10 ** 9 + 7

if abs(N-M) > 1:
    print(0)
    exit()

n_frac = factorial(N) % mod 
m_frac = n_frac
if N-M == 0:
    print((n_frac*m_frac)*2%mod)
else:
    if N > M:
        m_frac //= N
    else:
        m_frac *=  M
    print(n_frac*m_frac%mod)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)

N,M = map(int, input().split())

print(sys.getrecursionlimit())
if abs(N-M) > 1:
    print(0)
    exit()

def frac(n):
    if n == 1: return 1
    return n * frac(n-1)
if N-M == 0:
    print(frac(N)*frac(M)*2)
else:
    print(frac(N)*frac(M))

Submission status: Wrong Answer

Solution code:
MOD = 1000000007


def calc(num):
    ans = num
    num -= 1
    while num != 0:
        ans *= num
        ans %= MOD
        num -= 1
    return ans


N, M = map(int, input().split())

if abs(N-M) > 1:
    ans = 0
elif N == M:
    ans = ((calc(N) % MOD) * (calc(M) % MOD) * 2) % MOD
else:
    ans = (calc(N) % MOD) * (calc(M) % MOD)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
MOD = 1e9 + 7
N, M = map(int, input().split())
ans = 1
if abs(N - M) > 1:
    print(0)
    sys.exit()
for n in range(2, N + 1):
    ans *= n
    ans %= MOD
for m in range(2, M + 1):
    ans *= m
    ans %= MOD
if N == M:
    ans *= 2
    ans %= MOD
print(ans)

Submission status: Wrong Answer

Solution code:
N , M = map(int,input().split())
MOD = 10 ** 9 + 7
if abs(N - M) > 1:
    print("0")
    exit()
N = max(N , M)
M = min(N , M)
N_sum = 1
M_sum = 1
for i in range(N , 0, -1):
    N_sum *= i
    N_sum %= MOD
for i in range(M ,0, -1):
    M_sum *= i
    M_sum %= MOD
if N == M:
    N_sum *= M_sum
    N_sum *= 2
    print(N_sum % MOD)
else:
    M_sum *= N_sum
    print(M_sum % MOD)

Submission status: Wrong Answer

Solution code:
import sys
def extgcd(a,b):
    r = [1,0,a]
    w = [0,1,b]
    while w[2]!=1:
        q = r[2]//w[2]
        r2 = w
        w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
        r = r2
        w = w2
    return [w[0],w[1]]
def mod_inv(a,m):
    x = extgcd(a,m)[0]
    return (m+x%m)%m
n,m=map(int,input().split())
if abs(n-m)>1:
  print(0)
  sys.exit()
cnt1=1
for i in range(1,n):
  cnt1*=i
  cnt1=mod_inv(cnt1,(10**9)+7)
cnt2=cnt1*n
cnt2=mod_inv(cnt2,(10**9)+7)
cnt3=cnt2*m
cnt3=mod_inv(cnt3,(10**9)+7)
if n==m:
  print(2*(cnt2**2))
if n-m==1:
  print(cnt1*cnt2)
if m-n==1:
  print(cnt2*cnt3)

Submission status: Wrong Answer

Solution code:
import sys
def extgcd(a,b):
    r = [1,0,a]
    w = [0,1,b]
    while w[2]!=1:
        q = r[2]//w[2]
        r2 = w
        w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
        r = r2
        w = w2
    return [w[0],w[1]]
def mod_inv(a,m):
    x = extgcd(a,m)[0]
    return (m+x%m)%m
n,m=map(int,input().split())
if abs(n-m)>1:
  print(0)
  sys.exit()
cnt1=1
for i in range(1,n):
  cnt1*=i
  cnt1=mod_inv(cnt1,(10**9)+7)
cnt2=cnt1*n
cnt2=mod_inv(cnt2,(10**9)+7)
cnt3=cnt2*m
cnt3=mod_inv(cnt3,(10**9)+7)
if n==m:
  print(2*(cnt1**2))
if n-m==1:
  print(cnt1*cnt2)
if m-n==1:
  print(cnt2*cnt3)

Submission status: Wrong Answer

Solution code:
import sys
def extgcd(a,b):
    r = [1,0,a]
    w = [0,1,b]
    while w[2]!=1:
        q = r[2]//w[2]
        r2 = w
        w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
        r = r2
        w = w2
    return [w[0],w[1]]
def mod_inv(a,m):
    x = extgcd(a,m)[0]
    return (m+x%m)%m
n,m=map(int,input().split())
if abs(n-m)>1:
  print(0)
  sys.exit()
cnt1=1
for i in range(1,n):
  cnt1*=i
  cnt1=mod_inv(cnt1,(10**9)+7)
cnt2=cnt1*n
cnt2=mod_inv(cnt2,(10**9)+7)
cnt3=cnt2*m
cnt3=mod_inv(cnt3,(10**9)+7)
if n==m:
  print(2*cnt1)
if n-m==1:
  print(cnt1*cnt2)
if m-n==1:
  print(cnt2*cnt3)

Submission status: Wrong Answer

Solution code:
import sys
def extgcd(a,b):
    r = [1,0,a]
    w = [0,1,b]
    while w[2]!=1:
        q = r[2]//w[2]
        r2 = w
        w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
        r = r2
        w = w2
    return [w[0],w[1]]
def mod_inv(a,m):
    x = extgcd(a,m)[0]
    return (m+x%m)%m
n,m=map(int,input().split())
if abs(n-m)>1:
  print(0)
  sys.exit()
cnt=1
for i in range(1,n+1):
  cnt*=i
  cnt=mod_inv(cnt,(10**9)+7)
if n==m:
  print(2*((cnt**2)%((10**9)+7)))
if n-m==1:
  print((cnt*(cnt//n))%((10**9)+7))
if m-n==1:
  print((cnt*cnt*m)%((10**9)+7))

Submission status: Wrong Answer

Solution code:
import sys
n,m=map(int,input().split())
if abs(n-m)>1:
  print(0)
  sys.exit()
cnt=1
for i in range(1,n+1):
  cnt*=i
if n==m:
  print(2*((cnt**2)%((10**9)+7)))
if n-m==1:
  print((cnt*(cnt//m))%((10**9)+7))
if m-n==1:
  print((cnt*cnt*m)%((10**9)+7))

Submission status: Wrong Answer

Solution code:
import sys
n,m=map(int,input().split())
if abs(n-m)>1:
  print(0)
  sys.exit()
cnt=1
for i in range(1,n+1):
  cnt*=i
if n==m:
  print((cnt**2)%((10**9)+7))
if n-m==1:
  print((cnt*(cnt//m))%((10**9)+7))
if m-n==1:
  print((cnt*cnt*(n+1))%((10**9)+7))

Submission status: Wrong Answer

Solution code:
import sys
n,m=map(int,input().split())
if abs(n-m)>1:
  print(0)
  sys.exit()
cnt=1
for i in range(1,n+1):
  cnt*=i
if n==m:
  print((cnt**2)%((10**9)+7))
if n-m==1:
  print(cnt*(cnt//m)%(10**9)+7)
if m-n==1:
  print(cnt*cnt*(n+1)%(10**9)+7)

Submission status: Wrong Answer

Solution code:
# なぜかphpで解けなかったので
n, m = map(int, input().split())
INF = 10 ** 9 + 7
if n == m:
    x = 1
    for i in range(1, n + 1):
        x = (x * i) % INF

    print((x * x * 2) % INF)
elif abs(n - m) == 1:
    x = 1
    for i in range(1, max(n, m) + 1):
        x = (x * i) % INF
    print((x * int(x / max(n, m))) % INF)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
hoge=0
if(abs(m-n)>1):
  print(0)
elif(m==n):
  hoge=2
else:
  hoge=1
for i in range(n):
  hoge=hoge*(i+1)
  hoge=hoge%(10**9+7)
for i in range(m):
  hoge=hoge*(i+1)
  hoge=hoge%(10**9+7)
  
print(hoge)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
MOD = 10 ** 9 + 7
if abs(N - M) > 1:
  ans = 0
elif N == M:
  ans = 2
  for i in range(1, N + 1):
    ans = (ans * i * i) % MOD
else:
  A = max(N, M)
  ans = 1
  for i in range(1, A + 1):
    ans = (ans * i * i) % MOD
  ans = ans / A
print(int(ans % MOD))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
MOD = 10 ** 9 + 7
if abs(N - M) > 1:
  ans = 0
elif N == M:
  ans = 2
  for i in range(1, N + 1):
    ans = (ans * i * i) % MOD
else:
  A = max(N, M)
  ans = 1
  for i in range(1, A + 1):
    ans = (ans * i * i) % MOD
  ans = ans / A
print(ans % MOD)

Submission status: Wrong Answer

Solution code:
N, M = [int(_) for _ in input().split()]

if abs(N - M) > 1:
    print(0)
    exit()

if N > M:
    N, M = M, N
tmp = 1

MOD = 10 ** 9 + 7
for i in range(1, N + 1):
    tmp = (tmp * i) % MOD
ans = (tmp * 2) % MOD
if N < M:
    ans = (ans * M) % MOD
else:
    ans = (ans * tmp) % MOD

print(ans)

Submission status: Wrong Answer

Solution code:
from math import factorial

n, m = map(int, input().split())
if abs(n-m) > 1:
    print(0)
    exit()
if n % 2 == 0 and m % 2 == 0:
    print(2 * factorial(n) * factorial(m) % (10**9+7))
else:
    print(factorial(n) * factorial(m) % (10**9+7))

Submission status: Wrong Answer

Solution code:
import math

N,M = map(int,input().split())
ans = 1
if N-M >= 2 or M-N >= 2:
    print(0)
else:
    if N > M:
        for i in range(1,N+1):
            if i == N:
                ans *= i
            else:
                ans *= i**2
            ans = ans % 1000000007
    elif N < M:
        for i in range(1,M+1):
            if i == M:
                ans *= i
            else:
                ans *= i**2
            ans = ans % 1000000007
    elif N == M:
         for i in range(1,N+1):
                ans *= i**2
                if i == N:
                    ans *= 2
                ans = ans % 1000000007

Submission status: Wrong Answer

Solution code:
def kaijo(n):
    val = 1
    for i in range(2, n + 1):
        val *= i
    return val
N,M=map(int,input().split())
if N+1==M or M+1==N:
    print((kaijo(N)*kaijo(M))%(10**9+7))
elif N==M:
    print((kaijo(N)+kaijo(M))*2%(10**9+7))
else:
    print(0)

Submission status: Wrong Answer

Solution code:
import math 

INF = 10**9 + 7
n,m = map(int, input().split())
ans = 0
n,m = max(n,m),min(m,n)
a = math.factorial(n)%INF
ans = a*a
if n==m:
    ans = ans*2
    ans = ans%INF
elif n ==m+1:
    ans = ans//n
    ans = ans%INF
else:
    ans = 0
print(ans)

Submission status: Wrong Answer

Solution code:
import math 

INF = 10**9 + 7
n,m = map(int, input().split())
ans = 0
n,m = max(n,m),min(m,n)
a = math.factorial(n)%INF
ans = a*a
if n==m:
    ans = ans*2
    ans = ans%INF
elif n==m-1:
    ans = ans%INF
else:
    ans = 0
print(ans)

Submission status: Wrong Answer

Solution code:
import math 

INF = 10**9 + 7
n,m = map(int, input().split())
ans = 0
n,m = max(n,m),min(m,n)
a = math.factorial(n)%INF
if n==m:
    ans = (2*(a**2))%INF
elif n==m-1 or n-1 ==m:
    ans = (a*a//n)%INF
else:
    ans = 0
print(ans)

Submission status: Wrong Answer

Solution code:
from math import factorial
n,m=map(int,input().split())
a,b=max(n,m),min(n,m)
Q=10**9+7
if a-b>1:print(0);exit()
elif a+b%2==1:print((factorial(a)*factorial(b))%Q)
else:print((factorial(a)*factorial(b)*2)%Q)

Submission status: Wrong Answer

Solution code:
from math import factorial
n,m=map(int,input().split())
a,b=max(n,m),min(n,m)
Q=10**9+7
if abs(n-m)>1:print(0);exit()
elif a+b%2==1:print(factorial(a)*factorial(b)%Q)
else:print(factorial(a)*factorial(b)*2%Q)

Submission status: Wrong Answer

Solution code:
def fact(X):
    res = 1
    for i in range(1,X+1):
        res *= i
    return res

N, M = map(int,input().split())
N, M = max(N,M), min(N,M)
mod = 10**9 + 7
if N-M != 1 and N != M:
    ans = -1
elif N != M:
    ans = (fact(N)*fact(M))%mod
else:
    ans = (fact(N)*fact(M)*2)%mod
print(ans)