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: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N,M = map(int,input().split())
    if abs(N-M) > 1:
        return 0
    N_mlt = 1
    M_mlt = 1
    for i in range(1,N+1,1):
        N_mlt *= i
        N_mlt = N_mlt%1000000007
        #print(i,N_mlt)
    for i in range(1,M+1,1):
        M_mlt *= i
        M_mlt = M_mlt%1000000007
        #print(i,M_mlt)
    #print(N_mlt,M_mlt)
    ans = (N_mlt*M_mlt)%1000000007
    if abs(N-M)==0:
        ans = (ans*2)%1000000007
        return  ans
    else:
        return ans

print(main())

Submission status: Accepted

Solution code:
N,M=map(int, input().split())

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

p = 10 ** 9 + 7
n = 10 ** 5  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p) 階乗のmod
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)

if N==M:
    ans=2*fact[N]*fact[M] % p
else:
    big = max(N, M)
    small = min(N, M)
    ans = fact[N]*fact[M] % p

print(ans)

Submission status: Accepted

Solution code:
MOD = 10**9+7

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

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

#N=Mなら最後に2をかける
dog = 1
monkey = 1
for i in range(1,N+1):
    dog = (dog*i)%MOD
for j in range(1,M+1):
    monkey = (monkey*j)%MOD

ans =(dog*monkey)%MOD
if N==M:
    ans=(ans*2)%MOD

print(ans)

Submission status: Accepted

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


def permitation(x):
    res = 1
    for i in range(1, x + 1):
        res = res * i % mod
    return res


pn = permitation(n)
pm = permitation(m)

if n == m:
    res = pn * pm * 2 % mod
    print(res)
elif n == m - 1 or n == m + 1:
    res = pn * pm % mod
    print(res)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#79 C - Reconciled?
N,M = map(int,input().split())
MOD = 10**9 + 7

if abs(N-M) >= 2:
    ans = 0
else:
    # M! * N!
    Mfact = 1
    for m in range(1,M+1):
        Mfact = (Mfact * m)%MOD
    Nfact = 1
    for n in range(1,N+1):
        Nfact = (Nfact * n)%MOD
    ans = (Nfact*Mfact)%MOD
    if abs(N-M) == 0:
        ans = (ans * 2)%MOD
print(ans)

Submission status: Accepted

Solution code:
import math

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

if abs(n - m) >= 2:
    print(0)

elif abs(n - m) == 1:
    ans = math.factorial(n) * math.factorial(m)
    ans %= 10 ** 9 + 7
    print(ans)

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

N, M = map(int, input().split())
if abs(N - M) >= 2:
    print(0)
    exit()


N = math.factorial(N) % (10 ** 9 + 7)
M = math.factorial(M) % (10 ** 9 + 7)
if N == M:
    print((N * M * 2) % (10 ** 9 + 7))
else:
    print((N * M) % (10 ** 9 + 7))

Submission status: Accepted

Solution code:
N, M = [int(x) for x in input().split()]
MOD = 10**9+7

if max(N, M) - min(N, M) > 1:
    print(0)
    exit()


def floor(n):
    result = n
    for i in range(n-1, 0, -1):
        result *= i
        result %= MOD

    return result


r = floor(max(N, M))
t = floor(min(N, M))

if r == t:
    result = (r * t * 2) % MOD
else:
    result = (r * t) % MOD

print(result)

Submission status: Accepted

Solution code:
import sys
import math

mod = 10**9 + 7
N, M = map(int, sys.stdin.readline().split())
diff = abs(N - M)
if diff > 1:
    print(0)
    sys.exit()

n_p = 1
for i in range(1, N+1):
    n_p *= i
    n_p %= mod
m_p = 1
for i in range(1, M+1):
    m_p *= i
    m_p %= mod

if diff == 0:
    print(2 * n_p * m_p % mod)
else:
    print(n_p * m_p % mod)

Submission status: Accepted

Solution code:
import sys


def LI():
    return list(map(int, input().split()))


N, M = LI()
if abs(N-M) >= 2:
    print(0)
    sys.exit()
mod = pow(10, 9)+7
x = 1
if N == M:
    for i in range(1, N+1):
        x = (x*i) % mod
    ans = (x*x*2) % mod
else:
    A = max(N, M)
    B = min(N, M)
    for i in range(1, B+1):
        x = (x*i) % mod
    y = (x*A) % mod
    ans = (x*y) % mod
print(ans)

Submission status: Accepted

Solution code:
p = 10**9+7
N,M = map(int,input().split())
A = [1 for _ in range(N+1)]
for i in range(2,N+1):
    A[i] = (i*A[i-1])%p
B = [1 for _ in range(M+1)]
for i in range(2,M+1):
    B[i] = (i*B[i-1])%p
if abs(N-M)>1:
    ans = 0
elif abs(N-M)==1:
    ans = (A[N]*B[M])%p
elif abs(N-M)==0:
    ans = (2*A[N]*B[M])%p
print(ans)

Submission status: Accepted

Solution code:
def perm(n,r,p):
  if (r < 0) or (n < r):
    return 0
  return fac[n]*finv[n-r]%p

n = pow(10,5)*2+10
MOD = pow(10,9)+7

fac = [-1]*(n+1); fac[0] = 1; fac[1] = 1 #階乗
finv = [-1]*(n+1); finv[0] = 1; finv[1] = 1 #階乗の逆元
inv = [-1]*(n+1); inv[0] = 0; inv[1] = 1 #逆元
for i in range(2,n+1):
  fac[i] = fac[i-1]*i%MOD
  inv[i] = MOD - inv[MOD%i]*(MOD//i)%MOD
  finv[i] = finv[i-1]*inv[i]%MOD


N,M = map(int,input().split())
if abs(N-M) > 1:
  print(0);exit()
if N == M:
  ans = fac[N]*perm(M,N-1,MOD)*2
else:
  if M > N:
    N,M = M,N
  ans = fac[N]*perm(M,N-1,MOD)
print(ans%MOD)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7

def factorial(n, mod):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
        fact %= mod
    return fact

N, M = map(int,input().split())
if abs(N - M) > 1:
    print(0)
else:
    ans = 1
    ans *= factorial(N, mod)
    ans *= factorial(M, mod)
    ans *= 2 - abs(N - M)
    ans %= mod
    print(ans)

Submission status: Accepted

Solution code:
import sys

mod = 10**9 + 7
n = 10**6
 
fact = [1]*(n+1)
rfact = [1]*(n+1)
r = 1
for i in range(1, n+1):
    fact[i] = r = r * i % mod
rfact[n] = r = pow(fact[n], mod-2, mod)
for i in range(n, 0, -1):
    rfact[i-1] = r = r * i % mod

#nPk (mod MOD) を求める
def perm(n, k):
    return fact[n] * rfact[n-k] % mod
 
# nCk (mod MOD) を求める
def comb(n, k):
    return fact[n] * rfact[k] * rfact[n-k] % mod

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

if abs(n-m) > 1:
    print('0')
    sys.exit()

print(2*(perm(n,n)*perm(m,m))%mod) if n == m else print((perm(n,n)*perm(m,m))%mod)

Submission status: Accepted

Solution code:
MOD=1000000007

def fac(x):
    ret=1
    for i in range(1,x+1):
        ret*=i
        ret%=MOD
    return ret

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

if N==M:
    print(fac(N)**2*2%MOD)
elif abs(N-M)==1:
    print(fac(N)*fac(M)%MOD)
else:
    print(0)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n,m = readInts()
mod = 10**9+7
if abs(n-m)>1:
	print(0)
else:
	ans = 1
	for i in range(1,n+1,):
		ans = (ans*(i%mod))%mod
	for i in range(1,m+1):
		ans = (ans*(i%mod))%mod
	if abs(n-m)==0:
		ans = (ans*2)%mod
	print(ans)

Submission status: Accepted

Solution code:
import math
N, M = map(int, input().split())

mod = 10 ** 9 + 7

if abs(N - M) > 1:
  ans = 0
elif abs(N - M) == 1:
  ans = (math.factorial(N) % mod) * (math.factorial(M) % mod)
else:
  ans = (math.factorial(N) % mod) * (math.factorial(M) % mod) * 2
  
print(ans % mod)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    N,M = i_map()
    s = abs(N-M)
    z = 10**9 + 7

    if s >= 2:
        print(0)
        exit()

    if s == 0:
        a = math.factorial(N)
        print(a**2*2%z)
    else:
        a = math.factorial(N)
        b = math.factorial(M)
        print(a*b%z)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
with open(0) as f:
    N, M = map(int, f.read().split())
mod = 1000000007

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

if abs(N-M) > 1:
    ans = 0
elif N == M:
    ans = 2*factorial(M)**2 %mod 
else:
    N, M = max(N, M), min(N, M)
    ans = N * factorial(M)**2 %mod
print(ans)

Submission status: Accepted

Solution code:
MOD = 1_000_000_007
n, m = map(int,input().split())
if abs(n-m)>1:
    print(0)
else:
    if abs(n-m)==1:
        ans = 1
    else:
        ans = 2
    for i in range(n):
        ans = (ans*(i+1))%MOD
    for i in range(m):
        ans = (ans*(i+1))%MOD
    print(ans)

Submission status: Accepted

Solution code:
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import *

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def perm(n,mod=None):
    ans = 1
    for i in range(1,n+1):
        ans *= i
        if mod!=None:
            ans %= mod
    return ans
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x):
        self.setdefault(x,0)
        self[x] += 1

N,M = mint()
if abs(N-M)>1:
    print(0)
else:
    ans = perm(N,MOD)*perm(M,MOD)*(2-abs(N-M))
    print(ans%MOD)

Submission status: Accepted

Solution code:
def gyakugen(x,p):
  binp = str(bin(p-2))[::-1]
  ruijou = x
  
  result = 1
  for i in range(len(binp)-2):
    if binp[i] == "1":
      result *= ruijou
      result %= p
    ruijou *= ruijou
    ruijou %= mod
  return result

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

if abs(N-M) > 1 :
  print(0)
  exit()
elif N == M:
  ans = 1
  for i in range(1,N+1):
    ans *= i
    ans %= mod
    ans *= i
    ans %= mod
  ans *= 2
  ans %= mod
  print(ans)
else:
  ans = 1
  m = min(N,M)
  for i in range(1,m+1):
    ans *= i
    ans %= mod
    ans *= i
    ans %= mod
  ans *= max(N,M)
  ans %= mod
  print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(100000)

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

def kaijo(n):
    if n == 1:
        return 1
    return n * kaijo(n - 1) % ((10 ** 9) + 7)


if abs(n - m) > 1:
    print(0)
    exit()
a = kaijo(n)
b = kaijo(m)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
mod=10**9+7
n,m=map(int,input().split())
if n+1<m or m+1<n:
    print(0)
    exit()
elif n==m:
    print((2*math.factorial(n)**2)%mod)
else:
    print((math.factorial(n)*math.factorial(m))%mod)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
MAX=10**5+10
MOD = 10**9 + 7
def COMinit():
    """
    逆元テーブルをつくる前処理
    計算量はO(n)
    1 <= k <= n <= 1e7程度
    p: 素数
    """
    fac = [0]*MAX
    finv = [0]*MAX
    inv = [0]*MAX

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

fac, finv, inv = COMinit()
if abs(N-M)>=2:
    print(0)
elif abs(N-M)==1:
    ans = fac[N]*fac[M]%MOD
    print(ans)
else:
    ans = 2*fac[N]*fac[M]%MOD
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
if N==M+1:
    A=N
    B=M
    ans=1
elif N+1==M:
    A=M
    B=N
    ans=1
elif N==M:
    A=N
    B=M
    ans=2
else:
    print(0)
    exit()
ansa=1
ansb=1
for i in range(1,A+1):
    ansa=(ansa*i)%(10**9+7)
for j in range(1,B+1):
    ansb=(ansb*j)%(10**9+7)

print((ansa*ansb*ans)%(10**9+7))

Submission status: Accepted

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

mod = 10**9+7
ans = 1


if abs(n-m) >= 2:
    ans = 0
elif abs(n-m) == 1:
    for i in range(1, max(n, m)+1):
        ans *= i
        ans %= mod
    for j in range(1, min(n, m)+1):
        ans *= j
        ans %= mod
elif abs(n-m) == 0:
    for i in range(1, n+1):
        ans *= i
        ans %= mod
    ans **= 2
    ans*= 2
    ans %= mod


print(ans)

Submission status: Accepted

Solution code:
n,m=[int(i) for i in input().split()]
new_n=min(n,m)
new_m=max(n,m)
mod=10**9+7
def ans(n,m):

    if n==m:
        return 2*kaijyou(n,mod)*kaijyou(m,mod)
    elif m-n==1:
        return kaijyou(n,mod)*kaijyou(m,mod)
    else:
        return 0


def kaijyou(n,mod):
    ans=1
    for i in range(1,n+1):
        ans*=i
        ans=ans%mod
    return ans
print(ans(new_n,new_m)%mod)

Submission status: Accepted

Solution code:
#
# abc065 c
#
import sys
from io import StringIO
import unittest
import math


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)


def resolve():
    N, M = map(int, input().split())

    if abs(N-M) > 1:
        print("0")
    elif N == M:
        print(2*math.factorial(N)*math.factorial(M) % (10**9+7))
    else:
        print(math.factorial(N)*math.factorial(M) % (10**9+7))


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
MOD = 10 ** 9 + 7
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

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

def resolve():
    N, M = LI()

    if N == M:
        ans = 2 * pow(fact_mod(N, MOD), 2, MOD)
        ans %= MOD
        print(ans)
    elif abs(N - M) <= 1:
        ans = fact_mod(N, MOD) * fact_mod(M, MOD)
        ans %= MOD
        print(ans)
    else:
        print(0)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
t=10**9+7
import math
n=math.factorial(N)%t
m=math.factorial(M)%t

if 2<=abs(N-M):
    print(0)
elif abs(N-M)==1:
    print((n*m)%t)
else:
    print((n*m*2)%t)

Submission status: Accepted

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

if n < m: # n >= m
    n, m = m, n

if m + 1 == n or m == n:
    # m! * 1 or 2 * n!
    if m + 1 == n:
        ans = 1
    else:
        ans = 2
    k = 1
    for i in range(1, n + 1):
        k *= i
        if i == m:
            ans *= k
        k = k % (10**9 + 7)
    ans *= k
    print(ans % (10**9 + 7))
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
class Factorials:
    # nに十分大きい数を入力
    def __init__(self, n, mod):
        self.mod = mod

        # self.fac[i] ≡ i! (factorial:階乗)
        self.fac = [1]
        num = 1
        for i in range(1, n+1):
            num *= i
            num %= mod
            self.fac.append(num)

        # self.rec[i] ≡ 1 / i! (reciprocal:逆数)
        num = pow(num, mod-2, mod)
        self.rec = [1 for i in range(n+1)]
        self.rec[n] = num
        for i in range(n-1, 0, -1):
            num *= i + 1
            num %= mod
            self.rec[i] = num

    # comb(n, r) ≡ nCr
    def comb(self, n, r):
        return self.fac[n] * self.rec[r] * self.rec[n - r] % self.mod
    
    # perm(n, r) ≡ nPr
    def perm(self, n, r):
        return self.fac[n] * self.rec[n-r] % self.mod

    # fact(n) ≡ n!
    def fact(self, n):
        return self.fac[n]

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

if abs(n - m) > 1:
    print(0)
    exit()

fct = Factorials(n + 1, mod)

if abs(n - m) == 1:
    print(fct.fact(n) * fct.fact(m) % mod)

else:
    print(2 * fct.fact(n) * fct.fact(m) % mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def prepare(n, MOD):
 
    # 1! - n! の計算
    f = 1
    factorials = [1]  # 0!の分
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    # n!^-1 の計算
    inv = pow(f, MOD - 2, MOD)
    # n!^-1 - 1!^-1 の計算
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv
     
    return factorials, invs

MOD = 10**9+7
fact, fact_inv = prepare(2*10**5, MOD)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

n, m = map(int, input().split())
n_f = math.factorial(n)
m_f = math.factorial(m)
mod = 1000000007

if abs(n-m) > 1:
    print(0)
    exit()
if n == m:
    print((n_f * m_f * 2) % mod)
else:
    print((n_f * m_f) % mod)

Submission status: Accepted

Solution code:
import math
f=math.factorial
N,M=map(int,input().split())
print(max(2-abs(N-M),0)*f(N)*f(M)%(10**9+7))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
N,M=map(int,input().split())
p=10**9 +7
dog=math.factorial(N)
mon=math.factorial(M)

if N==M:
  ans = ((dog*mon)*2) % p
elif abs(N-M) == 1:
  ans = (dog*mon) % p
else:
  ans=0
print(ans)

Submission status: Accepted

Solution code:
MOD = int(1e9) + 7

def f(n):
    ans = 1

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

    return ans

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

if abs(n - m) > 1:
    print(0)

    exit()

ans = (f(n) * f(m)) % MOD

if n == m:
    ans = (ans * 2) % MOD

print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc065/tasks/arc076_a

n, m = [int(i) for i in input().split()]


def perm(x):
    a = 1
    for i in range(1, x + 1):
        a = (a * i) % (10**9 + 7)
    return a


if abs(m - n) > 1:
    print(0)
    quit()


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

print(ans)

Submission status: Accepted

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

if abs(n-m) >= 2:
    print(0)
    exit()

ans = 1
div = 10**9+7

for i in range(1, n+1):
    ans = (ans * i) % div
for i in range(1, m+1):
    ans = (ans * i) % div

if n != m:
    print(ans)
else:
    print((ans*2)%div)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#/usr/bin/env python

n, m = map(int, input().split())
MOD = int(1e9)+7
fac = [-1 for _ in range(100001)]
fac[1] = 1 

for i in range(1, 100000):
    fac[i+1] = ((i+1)*fac[i])%MOD

if abs(n-m) > 1:
    print(0)
    exit()

ans = 0 
if n == m:
    ans = (fac[n]*fac[m]*2)%MOD
else:
    ans = (fac[n]*fac[m])%MOD

print(ans)

Submission status: Accepted

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

def fact(n, mod):
    ans = 1
    for i in range(2, n + 1):
        ans = ans * i
        ans = ans % mod

    return ans


if abs(n - m) >= 2:
    print(0)
elif abs(n - m) == 1:
    print(fact(n,mod)*fact(m,mod) % mod)
elif abs(n - m) == 0:
    print(2 * fact(n, mod)*fact(m, mod) % mod)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
a = [0]*(max(N,M)+1)
a[0] = 1
a[1] = 1
for i in range(2,len(a)):
    a[i] = i * a[i-1] % 1000000007
    
if N==M:
    print(a[N]*a[M]*2 % 1000000007)
elif abs(N-M)==1:
    print(a[N]*a[M] % 1000000007)
else:
    print(0)

Submission status: Accepted

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

import math
if n == m:
    dog = math.factorial(n)
    mon = math.factorial(m)
    ans = dog * mon * 2
elif n == m + 1 or n == m - 1:
    dog = math.factorial(n)
    mon = math.factorial(m)
    ans = dog * mon
else:
    ans = 0

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

Submission status: Accepted

Solution code:
def factorial(a):
    result = 1
    for i in range(1,a+1):
        result *= i
        result %= 1000000007
    return result
N, M = map(int, input().split())
if N == M:
    ans = 2*(factorial(N)**2) %1000000007
elif abs(N-M) == 1:
    ans = max(N,M)*(factorial(min(N,M))**2) %1000000007
else:
    ans =0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math as m
N,M=map(int,input().split())
w=1000000007
if abs(N-M)<2:
    if N==M:
        print((m.factorial(N)**2*2)%w)
    else:
        print((m.factorial(N)*m.factorial(M))%w)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
a,b=map(int, input().split())

if abs(a-b) == 1:
    c = math.factorial(a)%(10**9+7)
    d = math.factorial(b)%(10**9+7)
    print(c*d%(10**9+7))
if abs(a-b) == 0:
    c = math.factorial(a)%(10**9+7)
    d = math.factorial(b)%(10**9+7)
    print(c*d*2%(10**9+7))
if abs(a-b) >= 2:
    print(0)

Submission status: Accepted

Solution code:
from math import factorial
N,M=map(int,input().split())
ans=0
if abs(N-M)>1:
    ans=0
else:
    if N==M:
        a=N
        b=M
        ans=factorial(a)*factorial(b)*2
    else:
        a=max(N,M)
        b=min(N,M)
        ans=factorial(a)*factorial(b)
print(ans%(10**9+7))

Submission status: Accepted

Solution code:
def main():
  N, M = [int(n) for n in input().split(" ")]

  if abs(N - M) >= 2:
    print(0)
  elif abs(N - M) == 1:
    print((factorial(N) * factorial(M)) % 1000000007)
  else:
    print((factorial(N) * factorial(M) * 2) % 1000000007)

def factorial(n):
  ret = 1
  for i in range(1, n + 1):
    ret = (ret * i) % 1000000007
  return ret
    
main()

Submission status: Accepted

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

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

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

if N == M:
  ans = math.factorial(N)
  ans %= mod
  ans *= math.factorial(M)
  ans %= mod
  ans *= 2
  ans %= mod
else:
  ans = math.factorial(M)
  ans %= mod
  ans *= math.factorial(N)
  ans %= mod
  

print(ans)

Submission status: Accepted

Solution code:
from math import factorial
def count(n, r):
    if n == r:
        return 2
    else:
        return 1

N, M = map(int, input().split())
mod = 10**9 + 7
print(((factorial(N) % mod) * (count(N, M) % mod) * (factorial(M) % mod)) % mod if abs(N - M) < 2 else 0)

Submission status: Accepted

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

if abs(n-m) > 1:
  print(0)
  exit()

mod = 10**9+7
ans = 1
for i in range(1,n+1):
  ans *= i
  ans %= mod
for i in range(1,m+1):
  ans *= i
  ans %= mod
if (n+m)%2 == 0:
  ans *= 2
  ans %= mod
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
n,m = map(int, input().split())
a = math.factorial(m)
b = math.factorial(n)
if 1 < abs(n-m):
    print(0)
elif abs(n-m) == 0:
    print((a*b*2)%1000000007)
elif abs(m-n) == 1:
    print((a*b)%1000000007)

Submission status: Accepted

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

def f(n):
    ans = n
    for i in range(1, n):
        ans *= i
        ans %= 1000000007
    return ans

if n == m:
    print(((f(n))**2 *2)%1000000007)
elif n == m+1 or m == n+1:
    print((f(n)*f(m))%1000000007)
else:
    print(0)