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:
BIGMOD = 10**9+7

def modPow(x,n):
    ans = 1
    while n >= 1:
        if bin( n & 1)==bin(1):
            ans = (ans*x) % BIGMOD
        x = x * x % BIGMOD
        n = n>>1
    return ans % BIGMOD

def modFact(N):
    fact = []
    fact.append(1) #0! = 1
    for i in range(1,N+1):
        a = fact[i-1]*i % BIGMOD
        fact.append(a)
    return fact

def modInvFact(fact):
    invFact = []
    for i in range(1,N+2):
        a = modPow(fact[i-1],BIGMOD-2)
        invFact.append(a)
    return invFact

def modPlus(a,b):
    return (a + b) % BIGMOD

def modMinus(a,b):
    if a-b >= 0:
        return (a - b) % BIGMOD
    else:
        return (a - b + BIGMOD)
    
def modMulti(a,b):
    return a * b % BIGMOD

# fact(n),invFact(r),invFact(n-r)
def nCr(fact,invFact,n,r):
    return (fact[n] * invFact[r] * invFact[n-r] ) %BIGMOD

N,M = map(int,input().split())
fact = modFact(max(N,M))
factN = fact[N]
factM = fact[M]
ans = 0
if abs(N-M) == 0:
    ans = modMulti(factN,factM)*2 % BIGMOD
elif abs(N-M) == 1:
    ans = modMulti(factN,factM)
else:
    ans = 0
    
print(ans)

Submission status: Accepted

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

MOD = int(1e9) + 7

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

Submission status: Accepted

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

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

    MOD = int(1e9) + 7

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC65 C

import math
import sys
sys.setrecursionlimit(100000000)
n,m = map(int,input().split())
mod = 10**9+7

x = max(n,m)
y = min(n,m)
if n-1 > m or m-1 > n:
    print(0)
    quit()

def P(n,r):
    return math.factorial(n)//math.factorial(n-r)
def C(n,r):
    return P(n,r)//math.factorial(r)

a = math.factorial(x)
b = P(y,x-1)
if y-(x-1) == 0:
    c = 1
else:
    c = 2*math.factorial(y-(x-1))
print(a*b*c % mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

if N == M:
    print(2 * math.factorial(N) * math.factorial(M) % MOD)
elif abs(M - N) == 1:
    print(math.factorial(N) * math.factorial(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)
        return
    ans = 1
    for i in range(1, N + 1):
        ans = ans * i % 1000000007
    for j in range(1, M + 1):
        ans = ans * j % 1000000007
    if N == M:
        ans = ans * 2 % 1000000007
    print(ans)


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()
elif abs(N-M)==1:
    print(math.factorial(N)*math.factorial(M)%mod)
else:
    print(2*math.factorial(N)*math.factorial(M)%mod)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(200000)
import math

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

def fac(n):
    if n == 1:
        return 1
    return n*fac(n-1)
#print(fac(3))
    
ans = 0
fn = math.factorial(n)
fm = math.factorial(m)
if n==m:
    ans = fn*fm*2#両側
elif abs(n-m) == 1:
    ans = fn*fm
else:
    ans = 0
print(ans%MOD)

Submission status: Accepted

Solution code:
import math


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


if max(N,M) - min(N,M) >= 2:

    print(0)

elif max(N,M) - min(N,M) == 1:

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

else:

    print ( 2 * math.factorial(N)*math.factorial(M) % (10**9+7) )

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
from math import factorial
def mul(a, b):
    mod = 1000000007
    return ((a % mod) * (b % mod)) % mod
N,M = map(int,input().split())
if abs(N-M) >= 2:
    print(0)
elif N == M:
    k = factorial(N) % (10**9+7)
    ans = mul(k,k)
    print(mul(2,ans))
else:
    k = factorial(M) % (10**9+7)
    l = factorial(N) % (10**9+7)
    ans = mul(k,l)
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def kaijo(num, mod):
    now_num = 1
    for i in range(1, num + 1):
        now_num *= i
        now_num %= mod
    return now_num

def main():
    dog, monky = map(int, input().split())
    mod = 10 ** 9 + 7

    if abs(dog - monky) > 1:
        print(0)
    else:
        dog_num = kaijo(dog, mod)
        monky_num = kaijo(monky, mod)

        if dog == monky:
            ans = dog_num * monky_num * 2
        else:
            ans = dog_num * monky_num
        print(ans % mod)




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
MOD1 = 1000000000 + 7
def factorial_mod(n, mod):
    import math
    return math.factorial(n) % mod
n,m = map(int, input().split())
if abs(n-m) > 1:
    print(0)
elif n == m:
    res = factorial_mod(m, MOD1) * factorial_mod(n, MOD1)
    res *= 2
    res %= MOD1
    print(res)
else:
    res = factorial_mod(m, MOD1) * factorial_mod(n, MOD1)
    res %= MOD1
    print(res)

Submission status: Accepted

Solution code:
import math

i = list(map(int,input().split()))
N = i[0]
M = i[1]

MOD = 1000000000 + 7

diff = abs(max(M,N) - min(M,N))

if diff >= 2:
    print(0)
elif diff == 1:
    ans = (math.factorial(max(M,N)) % MOD) * (math.factorial(min(M,N)) % MOD)
    print(ans % MOD)
else:
    ans = (math.factorial(max(M,N)) % MOD) * (math.factorial(min(M,N)) % MOD) * 2
    print(ans % MOD)

Submission status: Accepted

Solution code:
from math import factorial
N,M=map(int,input().split())
MOD=10**9+7
if N==M: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:
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=(int(i) for i in input().split())
#print(n,m)

mod=10**9+7
ans=1

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

Submission status: Accepted

Solution code:
n,m=(int(i) for i in input().split())
#print(n,m)

mod=10**9+7
ans=1

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

Submission status: Accepted

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


def factorial(x):
    ans = 1
    for i in range(2, x+1):
        ans = (ans * i) % (10**9+7)
    return ans


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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC065C - Reconciled? (ARC076C)
def factorial_mod(x: int) -> int:
    MOD = 10 ** 9 + 7  # change the value if needed
    ret = 1
    for i in range(1, x + 1):
        ret = (ret * i) % MOD
    return ret


def main():
    n, m = list(map(int, input().rstrip().split()))
    MOD = 10 ** 9 + 7
    n, m = min(n, m), max(n, m)
    dif = m - n
    if dif > 1:  # must be ...DDM...
        print(0)
    else:
        if dif == 0:  # start with either D or M (DM..., MD...)
            ans = (factorial_mod(n) ** 2 * 2) % MOD  # n == m -> (n!)^2 * 2
        else:  # sandwich (DMD or MDM)
            ans = (factorial_mod(n) ** 2 * m) % MOD  # n + 1 == m -> (n!)^2 * m
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math

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

l = abs(N - M)

# 列を作れないとき
if l >= 2:
    print(0)
    exit()

if l == 1:

    if N < M:
        _n = math.factorial(N)
        _m = _n * (M)
        ans = _n * _m
        print(ans%(10**9+7))
        exit()

    else:
        _m = math.factorial(M)
        _n = _m * (N)
        ans = _n * _m
        print(ans%(10**9+7))
        exit()



_n = math.factorial(N)
# _m = factorial(M)

ans = (_n * _n * 2) % (10**9+7)
print(ans)

Submission status: Accepted

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

def solve(k):
    ans = 1
    for i in range(1,k+1):
        ans = (ans*i)%(10**9+7)
    return ans


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

Submission status: Accepted

Solution code:
import math

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

if abs(N - M) > 1:
    print(0)
    exit()
else:
    x = math.factorial(min(N, M))
    if N == M:
        ans = 2 * x * x % (10 ** 9 + 7)
    else:
        ans = x * x * max(N, M) % (10 ** 9 + 7)
    print(ans)

Submission status: Accepted

Solution code:
import math
n,m = map(int,input().split())
if abs(n-m) != 0 and abs(n-m) != 1 :
    print(0)
else:
    x = math.factorial(min(m,n))

    if abs(n-m) == 0:
        print((2 * x * x)%(10**9+7))
    else:
        print((x * x * max(n,m))%(10**9+7))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
MOD=10**9+7
if abs(N-M)>=2:
    print(0)
    quit()
if abs(N-M)==1:
    a=1
else:
    a=2
def factorials(M):#M!までのリストｘと其の逆元のリストyを返すmod INF
    INF=int(1e9+7)
    fac=[0]*(M+1)
    finv=[0]*(M+1)
    inv=[0]*(M+1)
    fac[0]=fac[1]=1
    finv[0]=finv[1]=1
    inv[1]=1
    for i in range(2,M+1):
        fac[i]=(fac[i-1]*i)%INF
        inv[i]=INF-(inv[INF%i]*(INF//i))%INF
        finv[i]=finv[i-1]*inv[i]%INF
    return fac,finv
fac,finv=factorials(max(N,M))
print((a*fac[N]*fac[M])%MOD)

Submission status: Accepted

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

dec = abs(N-M)

if dec > 1:
    ans = 0
else:
    idx = max(N,M)
    dp = [1 for _ in range(idx+1)]
    dp[0] = 1
    for i in range(idx):
        dp[i+1] = dp[i]*(i+1)
        dp[i+1] %= dev

    if dec == 1:
        ans = dp[idx]*dp[idx-1]
        ans %= dev
    else:
        ans = 2*dp[idx]*dp[idx]
        ans %= dev

print(int(ans))

Submission status: Accepted

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

def main():
    n = N
    m = M
    d = 1000000007
    if abs(n - m) > 1:
        print(0)
        return
    n, m = (m, n) if n > m else (n, m)
    ans = 1
    for i in range(1, n + 1):
        ans *= i ** 2
        ans = ans % d
    last_n = 2 if n == m else m
    ans = ans * last_n % d
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)

MOD = 1000000007  # type: int

def solve(N: int, M: int):
    def fact(n):
        ret = 1
        for i in range(2, n + 1):
            ret *= i
            ret %= MOD
        return ret
    if abs(N - M) > 1:
        ret = 0
    else:
        ret = fact(N) * fact(M)
        if N == M:
            ret *= 2
    print(ret % MOD)
    return


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:
N,M=map(int,input().split())
MOD=10**9+7

frac=[0]*max(N+1,M+1)
frac[0]=1

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

for i in range(1,max(N+1,M+1)):
	frac[i]=(frac[i-1]*i)%MOD
	
if N==M:
	print((2*frac[N]*frac[M])%MOD)
else:
	print((frac[N]*frac[M])%MOD)

Submission status: Accepted

Solution code:
# 差が1か0のみ隣り合わない 適当に毎回割る再帰

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
if abs(N-M)>1:
    print(0)
    quit()
if abs(N-M)==1:
    a=1
else:
    a=2
MOD=10**9+7
def factorials(M):#M!までのリストｘと其の逆元のリストyを返すmod INF
    INF=int(1e9+7)
    fac=[0]*(M+1)
    finv=[0]*(M+1)
    inv=[0]*(M+1)
    fac[0]=fac[1]=1
    finv[0]=finv[1]=1
    inv[1]=1
    for i in range(2,M+1):
        fac[i]=(fac[i-1]*i)%INF
        inv[i]=INF-(inv[INF%i]*(INF//i))%INF
        finv[i]=finv[i-1]*inv[i]%INF
    return fac,finv
def nCr(n,r):
    INF=int(1e9+7)
    if n<r or r<0 or n<0 :
        return 0
    return fac[n]*(finv[r]*finv[n-r]%INF)%INF

fac,finv=factorials(max(N,M))

print((a*fac[N]*fac[M])%MOD)

Submission status: Accepted

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

mod = 10**9+7
import math
if N != M:
    tmp = math.factorial(min(N,M))
    print(tmp**2 * max(N,M) % mod)
else:
    tmp = math.factorial(N)
    print(tmp**2 * 2 % mod)

Submission status: Accepted

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

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

x = fact[N] * fact[M]

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

print(answer)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

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

if max(N, M) > min(N, M) + 1:
  answer = 0
elif N == M: 
  answer = math.factorial(N) * math.factorial(M) * 2
elif max(N, M) == min(N, M) + 1:
  answer = math.factorial(N) * math.factorial(M)

print(answer % MOD)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from math import factorial
mod = 1000000007
n,m = (int(i) for i in input().split())
if abs(n-m) >= 2:
    print(0)
    exit()
if n == m:
    ans = factorial(n)*factorial(m)*2
    print(ans%mod)
else:
    ans = factorial(n)*factorial(m)
    print(ans%mod)

Submission status: Accepted

Solution code:
nm = input().split(" ");
n = int(nm[0]);
m = int(nm[1]);
mod = 10**9+7
if n - m == 1 or m - n == 1:
	ansn = 1;
	for i in range(n) :
		ansn = (ansn % mod)*((n-i) % mod);
		ansm = 1;
	for j in range(m) :
	 	ansm = (ansm % mod)*((m-j) % mod);
	print((ansn*ansm) % mod);
elif n == m :
	ansn = 1;
	for i in range(n) :
		ansn = (ansn % mod)*((n-i) % mod);
		ansm = 1;
	for j in range(m) :
  		ansm = (ansm % mod)*((m-j) % mod);
	print((ansn*ansm*2) % mod);
else :
	print(0);

Submission status: Accepted

Solution code:
from math import factorial as f
N, M = sorted(list(map(int, input().split())))

MOD = 10**9 + 7
gap = M - N
if gap >= 2:
	ans = 0
else:
	ans = f(M) * f(N)
	if gap == 0:
		ans *= 2 % MOD
print(ans % MOD)

Submission status: Accepted

Solution code:
from math import factorial as f
N, M = sorted(list(map(int, input().split())))

gap = M - N
if gap >= 2:
	ans = 0
else:
	ans = f(M) * f(N)
	if gap == 0:
		ans *= 2
print(ans%(10**9+7))

Submission status: Accepted

Solution code:
import math
n,m=map(int,input().split())
if n<m-1 or m<n-1:
  print(0)
else:
  mod=10**9+7
  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:
n,m=map(int,input().split())
if abs(n-m)>1:print(0)
else:
	mod=10**9+7
	x=1 if abs(n-m)==1 else 2
	p=[1]
	for i in range(1,max(n,m)+1):
		p.append(p[ i-1]*i%mod)
	print(x*p[n]*p[m]%mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools

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

n_p = 1
m_p = 1

for i in range(1,n+1):
    n_p *= i
    n_p %= (10**9+7)

for i in range(1,m+1):
    m_p *= i
    m_p %= (10**9+7)


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

elif abs(n-m) == 1:
    print((n_p*m_p)%(10**9+7))

else:
    print((2*n_p*m_p)%(10**9+7))

Submission status: Accepted

Solution code:
import math

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

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

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

	if(N==M):
		ans *= 2
	ans %= (10 ** 9 +7)

	print(ans)

Submission status: Accepted

Solution code:
import math
a=list(map(int,input().split()))
ans=0
if max(a)-min(a)>=2:
    print(0)
else:
    if a[0]==a[1]:
        ans=math.factorial(max(a))
        ans=(ans%1000000007)*(ans%1000000007)*2
    else:
        ans=math.factorial(max(a))
        ans1 = math.factorial(min(a))
        ans=(ans%1000000007)*((ans1)%1000000007)
    print(int(ans%1000000007 ))

Submission status: Accepted

Solution code:
import math

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

mod = int(1e9+7)

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

print(ans)

Submission status: Accepted

Solution code:
import math

a, b = map(int, input().split())

if a == b:
    print((2 * math.factorial(a) * math.factorial(b)) % 1000000007)
elif abs(a-b) == 1:
    print((math.factorial(a)*math.factorial(b)) % 1000000007)
else:
    print(0)

Submission status: Accepted

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


if not (abs(N - M) == 0 or abs(N - M) == 1):
    print(0)
    exit()
elif abs(N - M) == 0:
    tmp = 2
else:
    tmp = 1

for i in range(1,N+1):
    tmp = (tmp * i) % 1000000007

for i in range(1, M+1):
    tmp = (tmp * i) % 1000000007

print(tmp)

Submission status: Accepted

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

ans = 0

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

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

Submission status: Accepted

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

if abs(N - M) > 1:
    print(0)
else:
    multi = 2 if N == M else 1
    # 犬と猿で独立に並べ方を数える。
    dog  = math.factorial(N)
    monkey = math.factorial(M)
    print(multi * dog * monkey % 1000000007)

Submission status: Accepted

Solution code:
import sys
import math

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

n = math.factorial(N)
m = math.factorial(M)
MOD = 10**9 + 7

if N == M:
    print(n * m * 2 % MOD)
else:
    print(n * m % MOD)

Submission status: Accepted

Solution code:
def fct(n):
	v = 1
	for i in range(1, n+1):
		v = (v * i) % 1000000007
	return v
N, M = map(int, input().split())
ans = 0
if N == M:
	v = fct(N)
	ans = (v*v*2)%1000000007
elif abs(N-M) == 1:
	ans = (fct(N)*fct(M))%1000000007
print(ans)

Submission status: Accepted

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

inf = 10**9+7
def f(num,total):
	for n in range(1,num+1):
		total *= n
		total %= inf
	return total

if abs(n-m) > 1:
	ans = 0
elif abs(n-m) == 1:
	ans = f(n,1)*f(m,1)
	ans %= inf
else:
	ans = f(n,1)*f(m,1)*2
	ans %= inf
print(ans)

Submission status: Accepted

Solution code:
import math

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

if 2 <= abs(N - M):
	print(0)
	exit()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import factorial

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

if abs(N - M) == 0:
	print(((factorial(N) ** 2) * 2) % INF)
else:
	print((factorial(N) % INF) * (factorial(M) % INF) % INF)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
m,d=map(int,input().split())
def fac(n):
  a=1
  if n==1:
    return a
  else:
    a=n*fac(n-1)
    return a%(10**9+7)
if abs(m-d)>=2:
  print(0)
elif abs(m-d)==1:
  print((fac(m)*fac(d))%(10**9+7))
else:
  print(2*(fac(m)**2)%(10**9+7))

Submission status: Accepted

Solution code:
M, N = map(int, input().split())
def fraction(N):
  a = N
  for x in range(N-1,1,-1):
    a = a * x % (1000000007)
  return a

p = abs(M - N)
if p < 2:
  print(fraction(M) * fraction(N) * (2 - p) % (1000000007))
else:
  print(0)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

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

def main():
  _n,_m=LI()

  ans=1
  ans2=1
  if abs(_n-_m)>1:
    return 0
    exit()
  else:
    n=_n
    m=_m
    while True:
      if n==0 or m==0:
        break
      ans*=n
      n-=1
      ans%=mod
      ans*=m
      m-=1
      ans%=mod
    n=_n
    m=_m
    while True:
      if n==0 or m==0:
        break
      ans2*=n
      n-=1
      ans2%=mod
      ans2*=m
      m-=1
      ans2%=mod
    if abs(_n-_m)==1:
      return ans%mod
    return (ans+ans2)%mod

print(main())

Submission status: Accepted

Solution code:

a, b = [ int(v) for v in input().split() ]
mod = 10**9+7
ans = 0
if a == b:
	p = 1
	for i in range(a):
		p = ( (i+1) * p ) % mod
	ans = (p*p*2)%mod

elif abs(a - b) == 1:
	if a < b:
		a, b = b, a
	p = 1
	for i in range(b):
		p = ( (i+1) * p ) % mod
	ans = (p*p*a)%mod
print(ans)

Submission status: Accepted

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

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

def f(n):
    r = 1
    for i in range(1, n + 1):
        r = r * i % (10 ** 9 + 7)
        
    return r
    
if N == M:
    print(f(N) * f(M) * 2 % (10 ** 9 + 7))
elif abs(N - M) == 1:
    print(f(N) * f(M) % (10 ** 9 + 7))
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def factorial_mod(n, mod):
    a = 1
    for i in range(1, n + 1):
        a *= i
        a %= mod
    return a


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

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

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

Submission status: Accepted

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


def n_factorial_mod(n, mod):
    ret = 1
    while n != 1:
        ret = ret * n % mod
        n -= 1
    return ret


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

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()

num = 10**9 + 7


def bikkuri(n):
    rt = 1
    for i in range(n, 0, -1):
        rt *= i
        rt = rt % num
    return rt


ans = (bikkuri(M)**2) % num

if N == M:
    ans *= 2
else:
    ans *= (M + 1)

ans %= num
print(ans)

Submission status: Accepted

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

import itertools
from collections import Counter
from collections import defaultdict
import bisect
import math
import heapq

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

    mod = 10**9 + 7

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

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

    if N == M:
        print((n * m * 2) % mod)
    else:
        print((n * m) % mod)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def fact(n):
  v = 1
  for i in range(2, n + 1):
    v = v * i % 1000000007
  return v

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

Submission status: Accepted

Solution code:
#C

from math import factorial
MOD = 10**9 + 7

n,m = (int(num) for num in input().split())

if n-m == 1 or n-m == -1:
    print(((factorial(n)%MOD)*(factorial(m)%MOD))%MOD)
elif n==m:
    print(((factorial(n)%MOD)*(factorial(m)%MOD)*2)%MOD)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

def solve():
    N, M = list(map(int, input().split()))
    MOD = 10**9+7
    """
    ありうるパターンは、
    
    ・NとMが同数
    ・NとMの差が1

    （例）
    ・2 2
    犬 猿 犬 猿
    この場合、犬と猿の位置を入れ替えたパターンもあるので×2

    ・2 3
    犬 猿 犬 猿 犬

    組み合わせの数は、順列の公式を使う
    
    ・順列の公式
    異なるn個のものからr個を取り出し並べる場合の数は
    nPr = n(n-1)(n-2)...(n-r+1) = n!/(n-r)!
    """

    def permutation_formula(n, r):
        return factorial(n) // factorial(n-r)

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


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

fac=[0]*(1+max(N,M))
fac[1]=1

for i in range(2, len(fac)):
	fac[i] = i * fac[i-1] % MOD

ans = fac[N] * fac[M] % MOD

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a,b=list(map(int,input().strip().split()))
if a-b>1 or b-a>1:
	print(0)
else:
	ans=1
	if a==b:
		ans=ans*2
	for i in range(1,a+1,1):
		ans=ans*i%1000000007
	for i in range(1,b+1,1):
		ans=ans*i%1000000007
	print(ans)