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

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):

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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):

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)

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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: 
  ans = factorial(N) * factorial(M)
elif diff == 0: 
  ans = 2 * factorial(N) * factorial(M)
print(ans%mod)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):

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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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

ans = ((make_fact(N + 1, MOD) * make_fact(M + 1, MOD) % MOD) * c) % MOD
print(ans)

Solution (Python):
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

ans = ((make_fact(N + 1, MOD) * make_fact(M + 1, MOD) % MOD) * c) % MOD
print(ans)

Solution (Python):
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))

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

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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))

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

Solution (Python):

"""def fac(n):
	ret = 1
	for i in range(2,n+1):
		ret = (ret*i) % (10**9+7)
    return ret"""
def fac(n):
    ret = 1
    for i in range(2,n+1):
        ret = ret*i % (10**9+7)
    return ret
a,b = map(int,input().split())
mod = 10**9+7
if abs(a-b) >= 2:
	print(0)
elif abs(a-b) == 1:
	print(fac(a)*fac(b)%mod)
else:
	print(2*fac(a)*fac(b)%mod)

Solution (Python):
import sys
import math

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

Solution (Python):
import math

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


M = math.factorial(m)
N = math.factorial(n)


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

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

elif n != m:
    print(N * M % MOD)

Solution (Python):
import math
N, M = map(int, input().split())
ma = max(N,M)
mi = min(N,M)

ans = 0
if (ma - mi == 1):
  ans = math.factorial(ma) * math.factorial(mi)
elif (ma - mi == 0):
  ans = math.factorial(ma) * math.factorial(mi) * 2
print(ans % (10 ** 9 + 7))

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

Solution (Python):
import math

def reconclied(n , m):

    ans = 0
    mod = 10**9 + 7

    if n == m:
        ans = 2 * (math.factorial(n) * math.factorial(m))

    elif abs(n - m) == 1:
        ans = (math.factorial(n) * math.factorial(m))

    return ans % mod

def main():
    n , m = map(int , input().split())
    print(reconclied(n , m))

if __name__ == '__main__':
    main()

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

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

print(ans)

Solution (Python):
N,M = [int(i) for i in input().split()]
k = max(N,M)
l = min(N,M)
N = k
M = l
mod = 10**9+7
def frac(N):
    ans = 1
    ans1 = 1
    ans2 = 1
    ans3 = 1
    for i in range(2,N+1):
        if i % 3 == 0:
            ans1 *=i
            ans1 %= mod
        if i % 3 == 1:
            ans2 *=i
            ans2 %= mod
        if i % 3 == 2:
            ans3 *=i
            ans3 %= mod
    ans = ans1*ans2*ans3 % mod
    return ans
if N - M > 1:
    print(0)
    exit()
if N == M:
    F = frac(N)
    f = F * F % mod
    print((2*f) % mod)
    exit()
if N == M+1:
    F1 = frac(M)
    F2 = frac(N)
    print((F1*F2) % mod)
    exit()

Solution (Python):
N, M = map(int, input().split())
mod = 10**9+7
if min(N, M) + 1 < max(N, M):
    ans = 0
elif min(N, M) == max(N, M):
    ans = 1
    for i in range(1, N+1):
        ans *= i
        ans %= mod
    ans *= ans
    ans %= mod
    ans *= 2
    ans %= mod
else:
    ans = 1
    minNM = min(N, M)
    for j in range(1, 1+minNM):
        ans *= j
        ans %= mod
    ans *= ans
    ans %= mod
    ans *= minNM+1
    ans %= mod
print(ans)

Solution (Python):
N,M=map(int,input().split())
md=10**9+7
def P(m):
  ret=1
  for i in range(1,m+1):
    ret = (ret*i)%md
  return ret

if N<M:
  N,M=M,N
MK = P(M)
if N==M:
  ans=(2*(MK**2))%md
elif N==M+1:
  ans=(MK**2)%md
  ans=(ans*N)%md
else:
  ans=0
print(ans)

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

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

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

if abs(N - M) >= 2:
  print(0)
else:
  if N == M:
    print((permutation(N) * permutation(M) * 2) % MOD)
  else:
    N, M = max(N, M), min(N, M)
    print((permutation(N) * permutation(M)) % MOD)

Solution (Python):
mod = 10 ** 9 + 7

fact = [1]
for i in range(10**5+5):
    new_fact = fact[-1]*(i+1)%mod
    fact.append(new_fact)
N,M = map(int,input().split())
if abs(N-M) > 1:
    ans = 0
elif abs(N-M) == 1:
    ans = fact[N] * fact[M] % mod
elif abs(N-M) == 0:
    ans = fact[N] * fact[M] * 2 % mod
print(ans)

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

Solution (Python):

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

L = sorted([N, M])

a2, a1 = L
if a1 == a2:
    for i in range(a1):
        ans *= (a1 - i)**2
        ans %= mod
        
    print((ans*2) % mod)
else:
    for i in range(a2):
        ans *= a2 - i
        ans %= mod
        ans *= a2 - i
        ans %= mod
        
    ans *= a1
    print(ans % mod)

Solution (Python):
import math

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

Solution (Python):


N,M=map(int,input().split())
ans=1
MOD=10**9+7
for i in range(1,N+1):
    ans*=i
    ans=ans%MOD
for j in range(1,M+1):
    ans*=j
    ans=ans%MOD

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

Solution (Python):

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

if abs(N-M)==0:
    p=2
elif abs(N-M)==1:
    p=1
else:
    p=0

P=[1]

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

ans=p*P[N]*P[M]%MOD
print(ans)

Solution (Python):
def kaijo(x):
    v=1
    for i in range(1,x+1):
        v*=i
    return v


n,m=map(int,input().split())
mod=10**9+7
if abs(n-m)>=2:
    ans=0
else:
    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:
        ans*=2
        ans%=mod

print(ans)

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

diff=abs(n-m)
if diff < -1 or diff > 1:
    print(0)
    exit()

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

from math import factorial
res =  factmod(n,7+10**9)*factmod(m,7+10**9)
if diff==0:
    res*=2
res = res%(7+10**9)
print(res)

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

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

	print(ans % MOD)

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

if abs(n-m) <= 1:
  sumN = 1
  sumM = 1 
  for i in range(2,n+1):
    sumN = sumN*i % (10**9+7) 
  for i in range(2,m+1):
    sumM = sumM*i % (10**9+7) 
  if n==m:
    print(2*sumM*sumN % (10**9+7))
  else:
    print(sumM*sumN % (10**9+7))
else:
  print(0)

Solution (Python):
n, m = map(int, input().split())
ans = 0
d = [0] * (max([n,m])+1)
def factorial(n):
    global d
    d[0] = 1
    for i in range(1, n+1):
        d[i] = ((i+1) * d[i-1]) % (10** 9 +7)
factorial(max(m,n))
if abs(n-m) == 1:
    ans = d[n-1] * d[m-1]
elif n == m:
    ans = 2 * d[n-1] * d[m-1]
print(ans % (10 ** 9 + 7))

Solution (Python):
import math

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

d = 10 ** 9 + 7

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

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

Solution (Python):
import math
n,m = map(int,input().split())
diff = abs(n-m)
count1 = 1
count2 = 1
a = 1

if diff >= 2:
    print(0)
elif diff == 1:
    ans = math.factorial(max(n,m))
    print(ans*(ans//max(n,m))%(10**9+7))
else:
    ans = math.factorial(n)
    print(ans*ans*2%(10**9+7))

Solution (Python):
import math
n,m = map(int,input().split())
diff = abs(n-m)
count1 = 1
count2 = 1
a = 1

if diff >= 2:
    print(0)
elif diff == 1:
    ans = math.factorial(max(n,m))
    print(ans*(ans//max(n,m))%(10**9+7))
else:
    ans = math.factorial(n)
    print(ans*ans*2%(10**9+7))

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

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

Solution (Python):
n,m=map(int,input().split())
if abs(n-m)>1:
    print(0)

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

    if n==m:
        ans*=2
        ans%=1000000007
    
    print(ans)

Solution (Python):

"""def fac(n):
	ret = 1
	for i in range(2,n+1):
		ret = (ret*i) % (10**9+7)
    return ret"""
def fac(n):
    ret = 1
    for i in range(2,n+1):
        ret = ret*i % (10**9+7)
    return ret
a,b = map(int,input().split())
mod = 10**9+7
if abs(a-b) >= 2:
	print(0)
elif abs(a-b) == 1:
	print(fac(a)*fac(b)%mod)
else:
	print(2*fac(a)*fac(b)%mod)

Solution (Python):
import math
MOD = 1000000007
m,n=map(int,input().split())
f=abs(m-n)
if f == 0:
  ans=2*math.factorial(m)*math.factorial(n)%MOD
  print(ans)
  exit()
elif f == 1:
  ans=math.factorial(m)*math.factorial(n)%MOD
  print(ans)
  exit()
else:
  print(0)
  exit()

Solution (Python):
import math
def P(n, r):
    return math.factorial(n)//math.factorial(n-r)
n,m = map(int,input().split())
mod = 10**9+7
if n == m:
    ans = P(n,n) * P(m,m) * 2
    print(ans%mod)
elif abs(n-m) > 1:
    print(0)
else:
    ans = P(n,n)*P(m,m)
    print(ans%mod)

Solution (Python):
from math import factorial

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

if abs(n-m)>1:
  print(0)
elif n==m:
  print(2*pow(factorial(n), 2, mod_n)%mod_n)
else:
  print(factorial(n)*factorial(m)%mod_n)

Solution (Python):



import math


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


sub = abs(n - m)

if sub >= 2:
    ans = 0
elif sub == 1:
    ans = (math.factorial(n) * math.factorial(m)) % (10 ** 9 + 7)  
else:
    ans = (math.factorial(n) * math.factorial(m) * 2) % (10 ** 9 + 7)


print(ans)