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:
n,m = map(int,input().split())
import math

if abs(m-n) == 1:
    ans = math.factorial(n) * math.factorial(m)
elif n == m:
    ans = 2*math.factorial(n) * math.factorial(m)
else:
    ans = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

g = 0
if n == m:
    g = 2
if abs(n - m) == 1:
    g = 1

N = 10 ** 5 + 10
fact = [0] * (N)
fact[0] = 1
for i in range(1, N):
    fact[i] = (fact[i-1] * i) % mod

print(g * fact[n] * fact[m] % mod)

Submission status: Accepted

Solution code:
from math import factorial

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

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

Submission status: Accepted

Solution code:
from math import factorial

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

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

Submission status: Accepted

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

if N==M:
    print((2*((factorial(N))%P)*((factorial(M))%P))%P)
elif min(N,M)+1==max(N,M):
    print((((factorial(N))%P)*((factorial(M))%P))%P)
else:
    print("0")

Submission status: Accepted

Solution code:
import sys
from math import factorial
sys.setrecursionlimit(100000)
def modfact(x,p):
    if x==1:
        return 1
    else:
        return (x%p)*modfact(x-1,p)
N,M=map(int,input().split())
P=10**9+7
"""
if N==M:
    print((2*modfact(N,P)*modfact(M,P))%P)
elif min(N,M)+1==max(N,M):
    print((modfact(N,P)*modfact(M,P))%P)
else:
    print("0")
"""
if N==M:
    print((2*((factorial(N))%P)*((factorial(M))%P))%P)
elif min(N,M)+1==max(N,M):
    print((((factorial(N))%P)*((factorial(M))%P))%P)
else:
    print("0")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import factorial

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

if abs(n-m) >= 2:
    print(0)
else:
    n_per = factorial(n) // factorial(n - n)
    m_per = factorial(m) // factorial(m - m)
    if n != m:
        print((n_per * m_per) % (10**9 + 7))
    else:
        print((n_per * m_per *2) % (10**9 + 7))

Submission status: Accepted

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

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


MOD = 10 ** 9 + 7
class modint:
    def __init__(self, x):
        self.x = x.x if isinstance(x, modint) else x % MOD
    def __str__(self): return str(self.x)
    __repr__ = __str__
    def __int__(self): return self.x
    __index__ = __int__
    def __add__(self, other): return modint(self.x + modint(other).x)
    def __sub__(self, other): return modint(self.x - modint(other).x)
    def __mul__(self, other): return modint(self.x * modint(other).x)
    def __pow__(self, other): return modint(pow(self.x, modint(other).x, MOD))
    def __floordiv__(self, other): return modint(self.x * pow(modint(other).x, MOD - 2, MOD))
    def __eq__(self, other): return self.x == modint(other).x
    def __ne__(self, other): return self.x != modint(other).x
    def __inv__(self): return pow(self.x, MOD - 2, MOD)

def fact(n):
    r = n
    for i in range(n - 1, 1, -1):
        r *= i
    return r

fn = fact(modint(N))
fm = fact(modint(M))

if N == M:
    print(fn * fm * 2)
else:
    print(fn * fm)

Submission status: Accepted

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



def factorial(n,mod):
    fac = [0]*(n+1)
    fac[0] = fac[1] = 1
    for i in range(2,n+1):
        fac[i] = fac[i-1]*i % mod
    return fac

fs = factorial(max(N,M),MOD)

if abs(N-M) >= 2:
    print(0)
    exit()
elif abs(N-M) == 1:
    print((fs[N]*fs[M])%MOD)
else:
    print((2*fs[N]**2)%MOD)

Submission status: Accepted

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007

#+++++

def main():
	n, m = IN()
	if abs(n-m) > 1:
		return 0
		
	(n,m) = (n,m) if n<=m else (m,n)
	pn = 1
	for i in range(1,n+1):
		pn=pn*i%mod
	if n==m:
		pn = (pn * pn * 2) % mod
	else:
		pn = (pn * pn * m) % mod
	return pn
	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

Solution code:
nm = list(map(int, input().split()))
mod = 10 ** 9 + 7
def fact(a):
  ret = 1
  while a > 0:
    ret = ret * a % mod
    a -= 1
  return ret

nm.sort()
if nm[1] - nm[0] > 1:
  print(0)
elif nm[1] - nm[0] == 1:
  print(fact(nm[1]) * fact(nm[0]) % mod)
else:
  print(2* fact(nm[1]) * fact(nm[0]) % mod)

Submission status: Accepted

Solution code:
import math
n,m = [int(x) for x in input().split()]

INF = 10**9 + 7

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

Submission status: Accepted

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(l)
  
  if n == m:
    return(2*f*f%mod)
  else:
    return(f*f*(l+1)%mod)

print(main())

Submission status: Accepted

Solution code:
MOD = 10**9 + 7

def factorial(x):
	retval = 1
	for i in range(2,x+1):
		retval = (retval * i)% MOD
	return retval

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

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


if N==M:
    ans = (factorial(N)*factorial(M)*2)%MOD
else:
    ans = (factorial(N)*factorial(M))%MOD
print(ans)

Submission status: Accepted

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

from math import factorial
ans = 0
div = int(1e9 + 7)

diff = abs(N - M)
if diff < 2:
  x = factorial(N) % div
  y = factorial(M) % div
  ans = (x * y) % div

if diff == 0:
  ans = (ans * 2) % div
print(ans)

Submission status: Accepted

Solution code:
import math

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

rev = 10**9+7

if n-m==0:
    ans = 2 * math.factorial(n) * math.factorial(m)
elif (abs(n-m)==1):
    ans = 1 * math.factorial(n) * math.factorial(m)
else:
    ans = 0
print(ans%rev)

Submission status: Accepted

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

ansN=1
ansM=1

if N-1==M or N==M-1 or N==M:
    for i in range(2,N+1):
        ansN*=(i%mod)
        ansN%=mod
    for g in range(2,M+1):
        ansM*=(g%mod)
        ansM%=mod
    if N==M:
        print((ansM*ansN*2)%mod)
    else:
        print((ansM*ansN)%mod)
else:
    print(0)

Submission status: Accepted

Solution code:
def main():
  import math 
  mf=math.factorial
  n,m=map(int,input().split())
  mod=pow(10,9)+7
  print(0 if abs(n-m)>=2 else ((mf(n)%mod)*(mf(m)%mod)*(2 if abs(n-m)==0 else 1))%mod)
if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
divisor = 10**9 + 7

def frac(n):
  sum = 1
  for i in range(1,n+1):
    sum = sum * i % divisor
  return sum

import sys,fractions
N,M=map(int,input().split(' '))
# abs(N-M) > 1なら無理
if (abs(N-M) > 1):
  print(0)
  sys.exit()

# それぞれの階乗を求める
frac_n = frac(N)
frac_m = frac(M)

if N == M:
  ans = frac_m * frac_n * 2
else:
  ans = frac_n * frac_m

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

Submission status: Accepted

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

N,M = list(map(int, input().rstrip().split()))
#-----

divisor = 10**9 + 7

cnt=1
diff = abs(N-M)
if diff >=2:
    print("0")
    sys.exit()
else:
    for num in [N,M]:
        for i in range(1,num+1):
            cnt = (cnt * i) % divisor

    if diff == 0:
        cnt = (cnt * 2) % divisor

print(cnt)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from functools import reduce
import sys

MOD = 1000000007  # type: int


def solve(N: int, M: int):
    if M > N:
        M, N = N, M
    if M + 1 < N:
        print(0)
        return
    mf = reduce(lambda a, b: a * b % MOD, range(1, M + 1), 1)
    if M == N:
        ans = mf * mf % MOD * 2 % MOD
    else:
        ans = mf * mf % MOD * N % MOD
    print(ans)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    solve(N, M)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

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

rev= 10**9+7

if (n - m ==0):
    ans = 2 * math.factorial(n) * math.factorial(m)
elif (abs(n - m) == 1):
    ans = 1 * math.factorial(n) * math.factorial(m)
else:
    ans = 0
print(ans%rev)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

from math import factorial

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

Submission status: Accepted

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)
    exit()
elif abs(n-m)>1:
    a=0
print(a%mod)

Submission status: Accepted

Solution code:

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

a=math.factorial(n) * math.factorial(m) 
if abs(n-m) == 0:
    a *= 2
elif abs(n-m) > 1:
    a = 0
print(a%1000000007)

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
mod = 10**9 + 7

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

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

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

if N != M :
    ans = (factorial(N, mod) * factorial(M, mod)) % mod
else :
    ans = (2 * factorial(N, mod) * factorial(M, mod)) % mod

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

if abs(n-m)==1:
    dog=1
    cat=1
    for i in range(1,n+1):
        dog*=i
        dog=dog%mod
    for i in range(1,m+1):
        cat*=i
        cat=cat%mod
    print(dog*cat%mod)
else:
    dog=1
    cat=1
    for i in range(1,n+1):
        dog*=i
        dog=dog%mod
    for i in range(1,m+1):
        cat*=i
        cat=cat%mod
    print(dog*cat*2%mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from functools import reduce

N, M = map(int, input().split())
MOD = 10 ** 9 + 7
if abs(N - M) >= 2:
    print(0)
elif abs(N - M) == 1:
    n = reduce(lambda x, y: x * y % MOD, range(1, N + 1))
    m = reduce(lambda x, y: x * y % MOD, range(1, M + 1))
    print(n * m % MOD)
else:
    n = reduce(lambda x, y: x * y % MOD, range(1, N + 1))
    m = reduce(lambda x, y: x * y % MOD, range(1, M + 1))
    print(n * m * 2 % MOD)

Submission status: Accepted

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

Submission status: Accepted

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)
elif L == 0:
    E = 2*math.factorial(N)*math.factorial(M)
    print(E % r)
else:
    E = math.factorial(N)*math.factorial(M)
    print(E % r)

Submission status: Accepted

Solution code:
from math import factorial

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

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

Submission status: Accepted

Solution code:
MOD = 10**9+7

def factorial_mod(n, mod=MOD):
    ret = 1
    for k in range(1,n+1):
        ret = (ret*k) % mod
        
    return ret

N,M = map(int,input().split())
N_fact = factorial_mod(N)
M_fact = factorial_mod(M)
ret = (N_fact*M_fact)%MOD

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

Submission status: Accepted

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

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

if abs(N - M) >= 2:
    print(0)
elif abs(N - M) == 0:
    print((fact[N] * fact[M] * 2) % MOD)
else:
    print((fact[N] * fact[M]) % MOD)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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


def main():
    mod=10**9+7
    N,M=MI()
    
    def fact(n):
        temp=1
        for i in range(1,n+1):
            temp*=i
            temp%=mod
        return temp
    
        
    if abs(N-M)>=2:
        print(0)
    else:
        ans=fact(N)*fact(M)
        if N==M:
            ans*=2
        print(ans%mod)

main()

Submission status: Accepted

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

MOD = 10**9+7

def fact(a,MOD):
    x = 1
    ans = 1
    for i in range(a):
        ans = (ans * x)%MOD
        x += 1
    return ans

def comb(a,b,MOD):
    max = (a-b,b-a)
    return fact(a,MOD) * pow(fact(b,MOD),MOD-2,MOD) * pow(fact(max,MOD),MOD-2,MOD)
  
if abs(N-M) > 1:
    print(0)
elif abs(N-M) == 1:
    print((fact(N,MOD)*fact(M,MOD))%MOD)
else:
    print((fact(N,MOD)*fact(M,MOD)*2)%MOD)

Submission status: Accepted

Solution code:
from collections import deque
import sys
input = sys.stdin.readline
import bisect as bi
from operator import mul
from functools import reduce
from math import factorial

def main():
    n, m = map(int, input().split())
    if abs(n-m) > 1:
        print(0)
        return

    ns = factorial(n)
    ms = factorial(m)

    pattern = 2 if n == m else 1

    print((ns * ms * pattern) % (10**9 + 7))

main()

Submission status: Accepted

Solution code:
import math

a, b = map(int, input().split())
fa = math.factorial(a)
fb = math.factorial(b)
mod = 10**9 + 7
fa %= mod
fb %= mod
k = fa*fb

if abs(a-b) >= 2:
  print(0)
elif abs(a-b) == 1:
  print(k%mod)
elif abs(a-b) == 0:
  print((2*k) % mod)

Submission status: Accepted

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

if abs(n-m) > 1:
    print(0)
else:
    nf, mf = 1, 1
    for i in range(1, min(n, m)+1):
        nf = (nf*i) % MOD

    if n != m:
        mf = max(n, m) * nf
    else:
        mf = nf

    if n == m:
        print(nf * mf * 2 % MOD)
    else:
        print(nf * mf % MOD)

Submission status: Accepted

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

if abs(n-m) > 1:
    print(0)
else:
    nf = 1
    for i in range(1, n+1):
        nf *= i
        nf %= INF

    mf = 1
    for i in range(1, m+1):
        mf *= i
        mf %= INF

    if n == m:
        print(nf * mf * 2 % INF)
    else:
        print(nf * mf % INF)

Submission status: Accepted

Solution code:
from math import factorial
N, M = map(int, input().split())
diff = abs(N-M)
ans = -1
mod = 10**9+7
if diff >= 2:
  ans = 0
elif diff == 1: # oxoxo
  ans = factorial(N) * factorial(M)
elif diff == 0: # oxox / xoxo
  ans = 2 * factorial(N) * factorial(M)
print(ans%mod)

Submission status: Accepted

Solution code:
MOD=10**9+7

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

N,M=map(int,input().split())
ans=0
if N==M:
  ans=MF[N]*MF[M]*2
elif abs(N-M)==1:
  ans=MF[N]*MF[M]
print(ans%MOD)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#065_C
n,m=map(int,input().split())
mod=10**9+7
f=[i for i in range(max(n,m)+1)]
for i in range(1,max(n,m)):
    f[i+1]=(f[i+1]*f[i])%mod
ans=0

if abs(n-m)==1:
    ans=(f[n]*f[m])%mod
elif n==m:
    ans=(f[n]**2)*2%mod
    
print(ans)

Submission status: Accepted

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) * factorial(m) * 2) % mod
elif diff == 1:
    ans = factorial(n) * factorial(m) % mod
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

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


def fact(x):
    return math.factorial(x)


if n == m:
    ans = (fact(n) ** 2) * 2 % mod
elif n == m-1 or n == m + 1:
    ans = fact(n) * fact(m) % mod
else:
    ans = 0
print(ans)

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7
dog, cat = map(int, input().split())
pd = 1
pc = 1
ans = 0
if abs(dog - cat) > 1:
    ans = 0
elif abs(dog - cat) == 1:
    for i in range(1, min(dog, cat)+1):
        pd = pd * i % MOD
    pc = pd * max(dog, cat) % MOD
    ans = pd * pc % MOD
else:
    for i in range(1, dog+1):
        pd = pd * i % MOD
    ans = pd * pd % MOD
    ans = ans * 2 % MOD
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

diff = N - M
if diff < 0:
    diff = -diff
if diff > 1:
    print(0)
    exit()

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

ans = perm(N, mod)
ans = (ans * perm(M, mod)) % mod
if diff == 0:
    ans = (ans * 2) % mod
print(ans)

Submission status: Accepted

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

MOD = 10**9 + 7

if abs(n - m) >= 2:
    print(0)
else:    
    f1 = 1
    for i in range(n):
        f1 = f1*(n-i)%MOD
    f2 = 1
    for i in range(m):
        f2 = f2*(m-i)%MOD
        
    if abs(n - m) == 1:
        print(f1*f2%MOD)
        
    elif n == m:
        print(f1*f2*2%MOD)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
mod=10**9+7
from math import factorial as f

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

Submission status: Accepted

Solution code:
import math

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

if abs(n - m) >= 2:
    ans = 0
else:
    ans = math.factorial(n) * math.factorial(m)
    if n == m:
        ans *= 2

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
n, m = map(int, input().split())
ans = int()
if n == m :
	ans = 2
elif abs(n-m) == 1:
	ans = 1
else :
	ans = 0

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

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

print(ans)

Submission status: Accepted

Solution code:
from math import factorial


def main():
    dog, monkey = map(int, input().split())
    answer = 0
    mod = 10 ** 9 + 7
    if dog == monkey or dog + 1 == monkey or dog - 1 == monkey:
       answer = (factorial(dog) % mod) * (factorial(monkey) % mod) % mod
       if dog == monkey:
            answer = answer * 2 % mod
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def I(): return int(input())
def MI(): return map(int, input().split())
def LMI(): return list(map(int, input().split()))
MOD = 10 ** 9 + 7


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

f = [1]
for n in range(1, max(N, M) + 1):
    f.append(f[n - 1] * n % MOD)
print(f[N] * f[M] * (2 if abs(N - M) == 0 else 1) % MOD)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
if(abs(N-M)>=2):
    print(0)
elif(abs(N-M)==1):
    h=max(N,M)
    t=1
    m=1
    for i in range(1,h+1):
        t=(t*i)%1000000007
        if(i==h-1):
            m=t
    print((t*m)%1000000007)
elif(N==M):
    t=1
    for i in range(1,N+1):
        t=(t*i)%1000000007
    print((t**2*2)%1000000007)

Submission status: Accepted

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(2, n+1):
        ans = ans*i % mod
    for i in range(2, m+1):
        ans = ans*i % mod
else:
    ans = 1
    for i in range(2, n+1):
        ans = ans*i % mod
    for i in range(2, m+1):
        ans = ans*i % mod
print(ans)

Submission status: Accepted

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

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

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

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

else:
    x = 2
    for i in range(1,n+1):
        x = x*i**2 % (10**9 + 7)
print(x)

Submission status: Accepted

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

if n==m:
    n=max(n,m)
    m=min(n,m)
    ans = math.factorial(n)*2*math.factorial(m)
    print(ans%mod)
else:
    ans = math.factorial(n)*math.factorial(m)
    print(ans%mod)

Submission status: Accepted

Solution code:
n, m = list(map(int, input().split()))
ans = 1
mod = 10**9+7
def a(x, y):
    return x*y%mod

if n==m:
    for i in range(1, n+1):
        ans = a(ans, i)
    print((ans**2*2)%(mod))
elif abs(n-m)==1:
    for i in range(1, n+1):
        ans = a(ans, i)
    for i in range(1, m+1):
        ans = a(ans, i)
    print(ans)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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):
        ans = a(ans, i)
    print((ans**2*2)%(10**9+7))
elif abs(n-m)==1:
    for i in range(1, n+1):
        ans = a(ans, i)
    for i in range(1, m+1):
        ans = a(ans, i)
    print(ans%(10**9+7))
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
import math
import sys
from collections import Counter
from fractions import gcd
from functools import reduce
import sys
sys.setrecursionlimit(4100000)

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

Submission status: Accepted

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


def comb(x):
    a = 1
    for i in range(1, x+1):
        a *= i
        a %= 1000000007
    return a

if abs(n-m) > 1:
    print(0)
elif n == m:
    print((2*comb(n)**2)%1000000007)
else:
    print((comb(n) * comb(m))%1000000007)

Submission status: Accepted

Solution code:
import numpy as np


def cumprod(arr, MOD):
    L = len(arr)
    Lsq = int(L**.5 + 1)
    arr = np.resize(arr, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        arr[:, n] *= arr[:, n - 1]
        arr[:, n] %= MOD
    for n in range(1, Lsq):
        arr[n] *= arr[n - 1, -1]
        arr[n] %= MOD
    return arr.ravel()[:L]


def make_fact(U, MOD):
    x = np.arange(U, dtype=np.int64)
    x[0] = 1
    fact = cumprod(x, MOD)
    return fact[-1]


N, M = map(int, input().split())
if N > M:
    N, M = M, N
if N + 1 < M:
    print(0)
    exit()
elif N + 1 == M:
    c = 1
else:
    c = 2
MOD = 10**9 + 7
# print("make_fact(N, MOD)", make_fact(N + 1, MOD), N)
ans = ((make_fact(N + 1, MOD) * make_fact(M + 1, MOD) % MOD) * c) % MOD
print(ans)

Submission status: Accepted

Solution code:
from math import factorial

N, M = map(int, input().split())
if abs(N - M) > 1:
    ret = 0
elif N == M:
    ret = factorial(N) * factorial(M) * 2
else:
    ret = factorial(N) * factorial(M)

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

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

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

Submission status: Accepted

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) * math.factorial(M)) * 2
else:
  if not N == M - 1 and not N - 1 == M:
    print(0)
    sys.exit()

  ans = math.factorial(N) * math.factorial(M)

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