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())
R = 1000000007

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

print(ans)

Solution (Python):
import math
n, m = map(int, input().split())
R = 1000000007

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

print(ans)

Solution (Python):
import math

N, M = map(int, input().split())
p = 10**9 + 7
n_kai = math.factorial(N) % p
m_kai = math.factorial(M) % p
if abs(N - M) > 1:
    print(0)
elif abs(N - M) == 1:
    print(n_kai * m_kai % p)
else:
    print(2*n_kai*m_kai % p)

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

ans_mod=10**9+7
ans=0
if N == M+1 or M == N+1:
    ans=(math.factorial(N)*math.factorial(M))%ans_mod
elif N == M:
    ans=(math.factorial(N)*math.factorial(M)*2)%ans_mod
    
print(ans)

Solution (Python):
from math import factorial
n, m = [int(x) for x in input().split()]
temp = abs(n - m)
if temp > 1:
    print(0)
    exit()
mod = 10 ** 9 + 7
f_n = factorial(n) % mod
f_m = factorial(m) % mod
ans = f_n * f_m % mod
if temp == 0:
    ans = ans * 2 % mod
print(ans)

Solution (Python):
N, M = map(int, input().split())
ans = 0

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


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

print(ans)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


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

Solution (Python):
from math import factorial


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


if __name__ == "__main__":
    main()

Solution (Python):
n, m = map(int, input().split())
MOD = 1000000007
def nPk(n, k):
    r = 1
    while k > 0:
        r *= n
        r %= MOD
        n -= 1
        k -= 1
    return r
if abs(n - m) > 1:
    print(0)
elif n == m:
    print(nPk(n, n) * nPk(m, m) * 2 % MOD)
else:
    print(nPk(n, n) * nPk(m, m) % MOD)

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

if abs(n-m) > 1:
    print(0)
    exit(0)
else:
    n_cnt,m_cnt=1,1
    for i in range(1,n+1):
        n_cnt*=i
        n_cnt%=MOD
    for i in range(1,m+1):
        m_cnt*=i
        m_cnt%=MOD

    if abs(n-m) == 0:
        ans=n_cnt*m_cnt*2
    else:
        ans=n_cnt*m_cnt
print(ans%MOD)

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

n = 100010
frac = [1]*(n+1)
for i in range(n):
    frac[i+1] = (i+1)*frac[i]%mod
if N==M+1:
    print(frac[N]*frac[M]%mod)
elif N==M:
    print(frac[N]*frac[M]*2%mod)
else:
    print(0)

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

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

Solution (Python):
from math import factorial
a, b = [int(i) for i in input().split()]
if abs(a-b) > 1:
    print(0)
elif a == b:
    print((2 * factorial(a) * factorial(b)) % (10 ** 9 + 7))
else:
    print((factorial(max(a, b)) * factorial(min(a, b))) % (10 ** 9 + 7))

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

Solution (Python):
import math

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

Solution (Python):
from math import factorial as f

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

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

Solution (Python):
n,m=map(int,input().split())
if abs(n-m)>=2:
    print(0)
    exit()
mod=1000000007
if n==m:
    nf=1
    mf=1
    for i in range(n):
        nf=nf*(n-i)%mod
        mf=mf*(n-i)%mod
    print(int(2*nf*mf%mod))
else:
    if m>n:
        n,m=m,n
    nf=1
    mf=1
    for i in range(n):
        nf=nf*(n-i)%mod
        if i!=n-1:mf=mf*(m-i)%mod
    print(int(nf*mf%mod))

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

mod  = 10**9+7

ans = 1

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

print(ans)

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

if abs(n-m)>1:
    print(0)
    exit()
elif n == m:
    ans1 = math.factorial(n)
    ans2 = math.factorial(m)
    ans = 2 * ans1 * ans2 
else:
    ans1 = math.factorial(n)
    ans2 = math.factorial(m)
    ans = ans2*ans1

print(ans % div)

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

if abs(n-m)>1:
    print(0)
    exit()
elif n == m:
    ans1 = math.factorial(n)
    ans2 = math.factorial(m)
    ans = 2 * ans1 * ans2 
else:
    ans1 = math.factorial(n)
    ans2 = math.factorial(m)
    ans = ans2*ans1

print(ans % div)

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

if abs(n-m)>1:
    print(0)
    exit()
elif n == m:
    ans1 = math.factorial(n)
    ans2 = math.factorial(m)
    ans = 2 * ans1 * ans2 
else:
    ma = max(n,m)
    mi = min(n,m)
    ans1 = math.factorial(ma)
    ans2 = math.factorial(mi)
    ans = ans2*ans1

print(ans % div)

Solution (Python):
import math

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

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

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

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

mx = max(n, m)
mn = min(n, m)

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

Solution (Python):
def factorial(x, mod):
    ret = 1
    while x > 0:
        ret = (ret * x) % mod
        x -= 1
    return ret


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

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

Solution (Python):
from functools import reduce

def fac(n, p):
    return reduce(lambda x, y: x * y % p, range(1, n + 1))

N, M = [int(x) for x in input().split()]
p = 10 ** 9 + 7
if abs(N - M) >= 2:
    ans = 0
elif N == M:
    ans = 2 * fac(N, p) * fac(M, p) % p
else:
    ans = fac(N, p) * fac(M, p) % p

print(ans)

Solution (Python):
def fac(n, p):
    ret = 1
    for i in range(2, n + 1):
        ret = ret * i % p
    return ret

N, M = [int(x) for x in input().split()]
p = 10 ** 9 + 7
if abs(N - M) >= 2:
    ans = 0
elif N == M:
    ans = 2 * fac(N, p) * fac(M, p) % p
else:
    ans = fac(N, p) * fac(M, p) % p

print(ans)

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

def factorial(num):
    x=1
    for i in range(num,0,-1):
        x = x *i % mod

    return x
        
def permutations(num,r):
    x=1
    cnt=0
    for i in range(num,0,-1):
        if cnt>r:
            break

        x = x*i %mod
        cnt+=1

    return x

def solve():
    if abs(m-n)>1:
        print(0)
        return
    
    dog=factorial(n)

    if n==m:
        monkey=permutations(m,m-1)*2 % mod
    else:
        monkey=factorial(m)
    
    ans=dog * monkey % mod
    print(ans)
    
if __name__ == "__main__":
     solve()

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

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

Solution (Python):
import sys
sys.setrecursionlimit(500000)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]


def main():
    N, M = mi()
    fact = [1]
    for i in range(1, max(N, M)+3):
        fact.append((i*fact[-1])%MOD)
    if abs(N-M) == 1:
        print((fact[N]*fact[M])%MOD)
    elif N == M:
        print((2*fact[N]*fact[M])%MOD)
    else:
        print(0)



if __name__ == '__main__':
    main()

Solution (Python):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n, m = rm()
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 sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
def gcd(*numbers): reduce(math.gcd, numbers)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
count = 0
ans = 0
inf = float("inf")

n,m = I()

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

if abs(n-m) == 1:
    print(math.factorial(n)*math.factorial(m) % mod)
    sys.exit()

print(math.factorial(n)*math.factorial(m)*2 % mod)

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

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

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

print(ans)

Solution (Python):
import math

MOD = 10 ** 9 + 7
n, m = list(map(int, input().split()))
ptn = 1
if abs(n - m) >= 2:
    ptn = 0
else:
    ptn = math.factorial(n) * math.factorial(m)
    if abs(n - m) == 0:
        ptn *= 2

print(ptn % MOD)

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

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

Solution (Python):
def comb(n,k):
    nCk = 1
    MOD = 10**9+7

    for i in range(n-k+1, n+1):
        nCk *= i
        nCk %= MOD

    for i in range(1,k+1):
        nCk *= pow(i,MOD-2,MOD)
        nCk %= MOD
    return nCk
n,m=map(int,input().split())
if m>n:
    n,m=m,n
N,M=1,1
mod=10**9+7
for i in range(1,n+1):
    N*=i
    N%=mod
for i in range(1,m+1):
    M*=i
    M%=mod
if n>=m+2:
    print(0)
    exit()
if n==m+1:
    ans=1
if n==m:
    ans=2
ans*=N*M
print(ans%mod)

Solution (Python):
def factorial_mod(n,mod):
    ret = 1
    for i in range(1,n+1):
        ret *= i
        ret %= mod
    return ret
def comb_mod(n,r,mod):
    if r > n or r < 0:
        ret = 0
    else:
        fact_n  = factorial_mod(n, mod)
        fact_r  = factorial_mod(r, mod)
        fact_nr = factorial_mod(n-r, mod)
        ret = fact_n * pow(fact_r, mod-2, mod) * pow(fact_nr, mod-2, mod) % mod
    return ret
n,m = map(int,input().split())
c = abs(n-m)
mod = 10**9+7
if c >= 2:
    ans = 0
elif c == 1:
    ans = factorial_mod(n,mod)*factorial_mod(m,mod)
else:
    ans = 2*factorial_mod(n,mod)*factorial_mod(m,mod)
print(ans%mod)

Solution (Python):
mod=10**9+7
a,b=map(int,input().split())

if abs(a-b)>=2:
    print(0)
    exit()

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

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

if a==b:
    ans*=2
    ans%=mod

print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(1000000)

mod = 1000000007


def fac(n):
    v = 1
    for i in range(n):
        v = int(((i+1) * v) % mod)
    return v


n, m = map(int, input().split())
if abs(m-n) > 1:
    print(0)
    exit()
n = int(fac(n) % (mod))
m = int(fac(m) % (mod))
ans = int((n*m) % (mod))
if abs(n-m) == 0:
    ans = int((ans*2) % mod)
print(ans)

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

Solution (Python):
N,M=map(int,input().split())
twice=(N==M)+(abs(N-M)<=1)

n=m=1
while N:
    n*=N
    n%=10**9+7
    N-=1

while M:
    m*=M
    m%=10**9+7
    M-=1

print((n*m*twice)%(10**9+7))

Solution (Python):
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
sys.setrecursionlimit(10 ** 6)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n, m = MAP()
tmp = 1

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

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

print(ans)

Solution (Python):
import sys
import math
input = sys.stdin.readline

N,M = list(map(int,input().split()))
big = max(N,M)
small = min(N,M)
m=1000000007

if big == small:
    print(math.factorial(big)**2*2%m)
elif big == small + 1:
    print(math.factorial(big)*math.factorial(small)%m)
else:
    print(0)

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

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

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

Solution (Python):
from math import factorial as f;N,M=map(int,input().split());print(f(N)*f(M)*(2-(N+M)%2)%(10**9+7) if abs(N-M) < 2 else 0)

Solution (Python):
def s(n,m):
  f = 1
  for i in range(1,n+1):f*=i;f%=m
  return f
N,M=map(int,input().split());m=10**9+7
print(s(N,m)*s(M,m)*(2-(N+M)%2)%m if abs(N-M) < 2 else 0)

Solution (Python):
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')
MOD = 10 ** 9 + 7


n, m = LI()
if abs(n - m) > 1:
    print(0)
    sys.exit()

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

Solution (Python):
import sys, math
N, M = map(int, input().split())
MOD = 10 ** 9 + 7
if abs(N - M) > 1:
    print(0)
    sys.exit()

NN = math.factorial(N) % MOD
MM = math.factorial(M) % MOD
if N == M:
    print(2 * NN * MM % MOD)
else:
    print(NN * MM % MOD)

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


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

if abs(n - m) >= 2:
    print(0)
else:
    if n == m:
        ans = kaijou(n)**2 * 2 % MOD
    else:
        n, m = max(n, m), min(n, m)
        ans = kaijou(n) * kaijou(m) % MOD
    print(ans)

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

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

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

Solution (Python):
N,M=map(int,input().split())
ans=1
mod=10**9+7
if N>M+1 or M>N+1:
  print(0)
  exit()
for i in range(1,N+1):
  ans*=i
  ans%=mod
for j in range(1,M+1):
  ans*=j
  ans%=mod
if N==M:
  ans=ans*2%mod
print(ans)

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

MOD = 10**9 + 7
FAC = [1]
INV = [1]
for i in range(1,max(m,n)+1):
  FAC.append((FAC[i-1]*i) % MOD)
  INV.append(pow(FAC[-1],MOD-2,MOD))

def nPr(n,r):
  return FAC[n]*INV[n-r]

if abs(n-m) > 1:
  print(0)
else:
  ans = nPr(n,n)*nPr(m,m)
  ans %= MOD
  if n == m:
    ans *= 2
    ans %= MOD
  print(ans)

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

if abs(N-M) >= 2:
  print(0)
  exit()
  
N,M = max(N,M),min(N,M)
fctr_M = 1
for i in range(1,M+1):
  fctr_M *= i
  fctr_M %= MOD

ans = fctr_M * fctr_M
ans %= MOD
if N == M:
  ans *= 2
  ans %= MOD
else:
  ans *= N
  ans %= MOD

print(ans)

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

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

mod = 10**9+7
ans = 1

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


for j in range(1, m+1):
  ans *= j
  ans %= mod





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

Solution (Python):
import sys
import math
def kaijyo(n):
  return math.factorial(n)%(10**9+7)
N,M=map(int,input().split())
if abs(N-M)>1:
  print(0)
  sys.exit()
if N-M==-1:
  N,M=M,N
if N-M==1:
  print((kaijyo(N)*kaijyo(M))%(10**9+7))
  sys.exit()
else:
  print((2*kaijyo(N)*kaijyo(M))%(10**9+7))
  sys.exit()

Solution (Python):
import math

import queue
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n,m = map(int,ipt().split())
    mod = 10**9+7
    kj = [1,1]
    for i in range(2,max(m,n)+1):
        kj.append(kj[-1]*i%mod)
    if abs(n-m) >= 2:
        print(0)
    elif abs(n-m) == 1:
        print(kj[n]*kj[m]%mod)
    else:
        print((2*kj[n]**2)%mod)

    return

if __name__ == '__main__':
    main()

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

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

Solution (Python):
n, m = map(int, input().split())
mod = 10**9+7
facs = [1] * (max(n,m)+1)
nfac = 1
for i in range(1, max(n,m)+1):
    nfac = nfac * i % mod
    facs[i] = nfac
if abs(n - m) <= 1:
    print((2 if n==m else 1) * (facs[n] * facs[m]) % mod)
else:
    print(0)

Solution (Python):
import math

N_inu, M_saru = map(int, input().split())

d = abs(N_inu - M_saru)

if (d > 1):
    ans = 0

else:
    if (d == 1):
        ans = math.factorial(N_inu) * math.factorial(M_saru)
    else:
        ans = 2 * math.factorial(N_inu) * math.factorial(M_saru)

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

print(ans)

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue

from decimal import Decimal
from collections import defaultdict



sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)


MOD = 10 ** 9 + 7
MAX = float('inf')


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


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

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

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


if __name__ == '__main__':
    main()

Solution (Python):
MOD = 10**9 + 7
n, m = map(int, input().split())
if abs(n - m) > 1:
    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):
import math
N,M = map(int,input().split())
n = max(N,M)
m = min(N,M)
if abs(N-M) > 1:
  print(0)
  exit()
 
ncnt = math.factorial(n) % 1000000007
 
if abs(N-M) == 0:
  mcnt = 2 * ncnt
else:
  mcnt = math.factorial(m)
print(ncnt*mcnt % 1000000007)

Solution (Python):
import math
N,M = map(int,input().split())
n = max(N,M)
m = min(N,M)
if abs(N-M) > 1:
  print(0)
  exit()

ncnt = math.factorial(n) % 1000000007

if abs(N-M) == 0:
  mcnt = 2 * ncnt
else:
  mcnt = math.factorial(m)
print(ncnt*mcnt % 1000000007)

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, m = map(int, readline().split())
    diff = abs(n - m)

    if diff > 1:
        print(0)
    else:
        ans = n
        for i in range(1, n):
            ans *= i
            ans %= MOD
        for i in range(1, m + 1):
            ans *= i
            ans %= MOD

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


if __name__ == '__main__':
    main()

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
n,m = iim()
mod = 1000_000_007

def prepare(n, MOD):
     
    f = 1
    factorials = [1]  
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    
    inv = pow(f, MOD - 2, MOD)
    
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv
     
    return factorials, invs


fac,inv = prepare(max(n,m),mod)
if abs(n-m) == 1:
    print(fac[n]*fac[m]%mod)
elif abs(n-m) == 0:
    print(fac[n]*fac[m]*2%mod)
else:
    print(0)

Solution (Python):
import sys
import math
input = sys.stdin.readline


def log(*args):
    print(*args, file=sys.stderr)


def main():
    n, m = map(int, input().rstrip().split())
    if abs(n - m) >= 2:
        print("0")
        return
    if n == m:
        print(math.factorial(n) * math.factorial(m) * 2 % (pow(10, 9) + 7))
    else:
        print(math.factorial(n) * math.factorial(m) % (pow(10, 9) + 7))


if __name__ == '__main__':
    main()

Solution (Python):
n, m = map(int, input().split())
MOD = 10**9+7
ans = 1
if abs(n-m) == 0:
    for i in range(1, n+1):
        ans *= i
        ans %= MOD
    else:
        ans = 2 * pow(ans, 2, MOD) % MOD
elif abs(n-m) == 1:
    cnt_n = 1
    cnt_m = 1
    for i in range(1, n+1):
        cnt_n = cnt_n * i % MOD
    for i in range(1, m+1):
        cnt_m = cnt_m * i % MOD
    else:
        ans = cnt_n * cnt_m % MOD
else:
    ans = 0

print(ans)

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

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

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

if n == m:
   print(2*pow(ans, 2, mod)%mod)
else:
   print(pow(ans, 2, mod)*m%mod)

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

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

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

for j in range(1, m+1):
    ans *= j
    ans %= mod

if n == m:
    ans *= 2
    ans %= mod

print(ans)

Solution (Python):

def main():
    import sys
    imput = sys.stdin.readline
    
    
    
    n, m = map(int, input().split())

    
    
    
    
    
    
    

    
    
    mod = pow(10, 9) + 7
    dog = 1
    monkey = 1

    for i in range(1, n+1):
        dog *= i
        dog %= mod
    for j in range(1, m+1):
        monkey *= j
        monkey %= mod

    answer = 0
    
    if m - n == 1:
        answer = dog * monkey % mod
    elif n- m == 1:
        answer = dog * monkey % mod
    elif n - m == 0:
        answer = dog * monkey * 2 % mod

    
    print(answer)

    
if __name__ == "__main__":
    main()

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

m = 1000000007

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

k = min(N, M)
t = 1
for i in range(2, k + 1):
    t = t * i % m
t = t * t % m

if N == M:
    print(2 * t % m)
else:
    print(t * (k + 1) % m)

Solution (Python):
import math

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


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

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
import math

def main():
    n, m = input_list()
    if abs(n-m) > 1:
        print(0)
        exit()
    mod = 10**9 + 7
    diff = abs(n-m)
    if diff == 1:
        ans = math.factorial(n) * math.factorial(m)
        print(ans % mod)
    else:
        ans = 2 * (math.factorial(n) * math.factorial(m))
        print(ans % mod)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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


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


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()

Solution (Python):
def factorial(n, mod=10**9+7):
    a = 1
    for i in range(1,n+1):
        a = a * i % mod
    return a

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

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

print(ans)

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

    ans=(fact(n)*fact(m))%mod
    if n==m: print((2*ans)%mod)
    else: print(ans)

Solution (Python):
n,m = map(int, input().split())
ans=1
div=10**9+7

def func(ans,x):
  for i in range(1,x+1):
    ans*=i
    ans%=div
  return ans

if abs(n-m)>1:
  ans = 0
else:
  ans = func(ans,n)
  ans = func(ans,m)
  if abs(n-m)==0:
    ans*=2
    ans%=div

print(ans)

Solution (Python):
n,m = map(int, input().split())
mod = 1000000007
def fac(a,mod):
    res = 1
    for i in range(1,a+1):
        res *= i
        res %= mod
    return res
if n==m:
    print(fac(n,mod) * fac(m,mod) * 2 % mod)
elif (n==m+1):
    print(fac(n,mod) * fac(m,mod) % mod)
elif (n+1==m):
    print(fac(n,mod) * fac(m,mod) % mod)
else:
    print(0)

Solution (Python):
import math

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

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