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

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

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

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

Solution (Python):
import math

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

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

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

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

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

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

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

def f(n):
    ans = 1

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

    return ans

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

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

    exit()

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

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

print(ans)

Solution (Python):


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


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


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


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

print(ans)

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

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

ans = 1
div = 10**9+7

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

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

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

Solution (Python):


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

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

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

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

print(ans)

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

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

    return ans


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

print(ans)

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

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

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

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

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

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

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

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

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

mod = 10**9 + 7
l = n+m
ans = 1
b = max(n, m)
s = min(n, m)
for i in range(l):
    if i % 2 == 0:
        tmp = b-i//2
        if tmp > 0:
            ans = (ans % mod) * (tmp % mod)
        else:
            print(0)
            exit()
    else:
        tmp = s-i//2
        if tmp > 0:
            ans = (ans % mod) * (tmp % mod)
        else:
            print(0)
            exit()
if l%2 == 0:    
    ans = (ans % mod) * (2 % mod)
print(ans%mod)

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

p=10**9+7
min_=min(N,M)
max_=max(N,M)
ans=1
for i in range(1,min_+1):
    ans=(ans*i)%p
    ans=(ans*i)%p

if abs_NM==1:
    ans=(ans*max_)%p
else:
    ans=(ans*2)%p

print(ans)

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

p=10**9+7
ans=1
for i in range(1,N+1):
    ans=(ans*i)%p
for i in range(1,M+1):
    ans=(ans*i)%p
if abs_NM==0:
    ans=(ans*2)%p

print(ans)

Solution (Python):
from math import factorial

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

Solution (Python):
from math import factorial

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

Solution (Python):
from math import factorial

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

Solution (Python):
import math
n,m = map(int,input().split())
if abs(n-m)>1:
    print(0)
elif abs(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))

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


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


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

if abs(N - M) < 2:
    ans += fact(N, MOD) * fact(M, MOD) % MOD
    if N == M:
        ans *= 2
        ans %= MOD
print(ans)

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

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

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

Solution (Python):
import math

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

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

dog = math.factorial(n)
monkey = math.factorial(m)
if abs(n-m) == 1:
    ans = dog*monkey % mod
elif n == m:
    ans = dog*monkey*2 % mod
print(ans%mod)

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

factorial=[1 for i in range(10**5+5)]
for i in range(1,10**5+5):
    if i==1:factorial[i]=1
    else:factorial[i] = factorial[i-1]*i % mod
      
if abs(N-M)>1:
  print(0)
  exit()

if N==M:
  print(factorial[M]**2*2%mod)
else:
  print(factorial[M]*factorial[N]%mod)

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

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

if abs(n-m)==1:
    ans=1
else:
    ans=2

while n>0:
    ans*=n
    ans%=mod
    n-=1

while m>0:
    ans*=m
    ans%=mod
    m-=1

print(ans)

Solution (Python):
def factrial(n):
    INF = 10 ** 9 + 7
    ret = 1
    for i in range(2, n + 1):  ret = (ret * i) % INF
    return ret

N, M = list(map(int,input().split()))
if abs(N - M) >= 2:  ans = 0
elif N == M:  ans = (2 * (factrial(N) ** 2)) % (10 ** 9 + 7)
else:  ans = factrial(N) * factrial(M)
print(ans % (10 ** 9 + 7))

Solution (Python):






MOD = 10**9 + 7

def modFact(x):
  if x < 1:
    raise ValueError
  res = 1
  for i in range(x):
    res = res * (i+1) % MOD
  return res


def solve(n,m):
  if abs(n-m) > 1:
    return 0
  elif abs(n-m) == 1:
    return modFact(n) * modFact(m) % MOD
  else:
    return modFact(n) * modFact(m) * 2 % MOD


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

if __name__ == '__main__':
  main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
mod = 10**9+7

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

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

if __name__ == "__main__":
    main()

Solution (Python):
def mod1(x):
  mod=10**9+7
  ans=1
  y=1
  while y<=x:
    ans=ans*y
    y+=1
    ans=ans%mod
    
  return ans%mod

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

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

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

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

x = 10**9+7

ans = 0
if n == m:
    ans += math.factorial(n) * math.factorial(m) * 2
    print(ans % x)
    exit()
elif n >= m and n -m == 1:
    ans += math.factorial(n) * math.factorial(m)
    print(ans % x)
    exit()
elif m >= n and m -n== 1:
    ans += math.factorial(n) * math.factorial(m)
    print(ans % x)
    exit()
else:
    print(ans % x)

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

sm=1 if n!=m else 2
while n>0:
  sm=(sm*n)%(10**9+7)
  n-=1
while m>0:
  sm=(sm*m)%(10**9+7)
  m-=1
print(sm)

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

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

fac = 1
num = 10**9 + 7
for i in range(1,N+1):
    fac *= i
    if fac > num:
        fac %= num

fac2 = 1
for i in range(1,M+1):
    fac2 *= i
    if fac2 > num:
        fac2 %= num

if N == M:
    print(fac**2*2%num)
else:
    print(fac*fac2%num)

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


def main():
    N, M = map(int, input().split())
    mod = 10 ** 9 + 7
    if abs(N - M) > 1:
        ans = 0
    elif abs(N - M) == 1:
        ans = (factorial(min(N, M))**2 * max(N, M)) % mod
    else:
        ans = (factorial(N)**2 * 2) % mod

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import math


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

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import math

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

def mod(num):
    return num % (10 ** 9 + 7)

def calc():
    dp[1] = 1
    for i in range(2, maxmn + 1):
        dp[i] = mod(dp[i-1] * i)

absnm = abs(N - M)

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

if absnm > 1:
    print(0)

else:
    calc()

    if absnm == 1:
        r = mod(dp[M] * dp[N])

    elif absnm == 0:
        r = mod(dp[M] * dp[N] * 2)

    else:
        r = 0

    print(r)

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

x = abs(N-M)
if x >= 2:
    print(0)
else:
    mod = 10**9+7
    a = 1
    for i in range(2, N+1):
        a *= i
        a %= mod
    b = 1
    for i in range(2, M+1):
        b *= i
        b %= mod
    
    if x == 0:
        print(a*b*2%mod)
    else:
        print(a*b%mod)

Solution (Python):
import math
def ModFact(N,Mod):
    Num = 1
    for T in range(2,N+1):
        Num = (Num*T)%Mod
    return Num
  
N,M = (int(T) for T in input().split())
Mod = 10**9+7
if N==M:
    print((2*ModFact(N,Mod)*ModFact(M,Mod))%Mod)
elif N==M+1 or M==N+1:
    print((ModFact(N,Mod)*ModFact(M,Mod))%Mod)
else:
    print(0)

Solution (Python):
n, m = map(int , input().split())
ans  = 1
mod = 10**9+7
for i in range(1, n+1):
	ans = (ans%mod)*(i%mod)
	ans = ans%mod
for i in range(1, m+1):
	ans = (ans%mod)*(i%mod)
	ans = ans%mod

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

elif abs(n-m)==0:
	ans = ans*2
	ans = ans%mod
	print(ans)

else:
	print(ans)

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

Solution (Python):
class Modulo():
    def __init__(self,a,n):
        self.a=a%n
        self.n=n

    def __str__(self):
        return "{} (mod {})".format(self.a,self.n)

    
    def __pos__(self):
        return self

    def __neg__(self):
        return  Modulo(-self.a,self.n)

    
    def __eq__(self,other):
        if isinstance(other,Modulo):
            return (self.a==other.a) and (self.n==other.n)
        elif isinstance(other,int):
            return (self-other).a==0

    def __neq__(self,other):
        return not(self==other)
    
    
    def __add__(self,other):
        if isinstance(other,Modulo):
            if self.n!=other.n:
                raise Modulo_Error("異なる法同士の演算です.")
            return Modulo(self.a+other.a,self.n)
        elif isinstance(other,int):
            return Modulo(self.a+other,self.n)

    def __radd__(self,other):
        if isinstance(other,int):
            return Modulo(self.a+other,self.n)
        
    
    def __sub__(self,other):
        return self+(-other)

    def __rsub__(self,other):
        if isinstance(other,int):
            return -self+other
        
    
    def __mul__(self,other):
        if isinstance(other,Modulo):
            if self.n!=other.n:
                raise Modulo_Error("異なる法同士の演算です.")
            return Modulo(self.a*other.a,self.n)
        elif isinstance(other,int):
            return Modulo(self.a*other,self.n)
        
    def __rmul__(self,other):
        if isinstance(other,int):
            return Modulo(self.a*other,self.n)
        
    
    def Modulo_Inverse(self):
        x0, y0, x1, y1 = 1, 0, 0, 1
        a,b=self.a,self.n
        while b != 0:
            q, a, b = a // b, b, a % b
            x0, x1 = x1, x0 - q * x1
            y0, y1 = y1, y0 - q * y1

        if a!=1:
            raise Modulo_Error("{}の逆数が存在しません".format(self))
        else:
            return Modulo(x0,self.n)

    
    def __truediv__(self,other):
        return self*(other.Modulo_Inverse())
    
    def __rtruediv__(self,other):
        return other*(self.Modulo_Inverse())

    
    def __pow__(self,m):
        u=abs(m)

        r=Modulo(1,self.n)

        while u>0:
            if u%2==1:
                r*=self
            self*=self
            u=u>>1

        if m>=0:
            return r
        else:
            return r.Modulo_Inverse()

    
    def sqrt(self):
        if self==0:
            return self
        elif self.n==2:
            return self
        elif self.n%4==3:
            return self**((self.n+1)//4)
        else:
            p=self.n
            u=2
            s=1
            while (p-1)%(2*u)==0:
                u*=2
                s+=1

            z=Modulo(2,p)
            while z**((p-1)//2)!=-1:
                z+=1

            q=(p-1)//u
            m=s
            c=z**q
            t=self**q
            r=self**((q+1)//2)

            while m>1:
                k=1
                d=t*t

                while d!=1:
                    k+=1
                    d*=d
                    print(m,k)
                b=Modulo(2,p)**(2**(m-k-1))
                c,t,r,m=b*b,t*b*b,r*b,k
            return r

N,M=map(int,input().split())
K=10**9+7
if abs(N-M)>=2:
    print(0)
elif abs(N-M)==1:
    N,M=max(N,M),min(N,M)
    P=Modulo(1,K)
    for i in range(1,M+1):
        P*=i
    print((P*P*N).a)
else:
    P=Modulo(1,K)
    for i in range(1,M+1):
        P*=i
    print((P*P*2).a)

Solution (Python):
n, m = map(int, input().split())
mod = 10**9+7
"""
abs(n-m)>1の時、必ず隣り合ってしまうので0
n>mとしても一般性を失わない
if n<m: n, m = m, n
n==mnの時は、どちらを左端に置くか選べるので2倍
"""

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

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

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

Solution (Python):
import sys
import itertools

import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines



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

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

def f(n):
    ret = 1
    while n > 1:
        ret *= n
        ret %= MOD
        n -= 1
    return ret

ans = 1
ans *= f(N)
ans *= f(M)
if N == M:
    ans *= 2
print(ans % MOD)

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

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

c = math.factorial(n) * math.factorial(m)
result = 2 * c if n == m else c
print(result % (10 ** 9 + 7))

Solution (Python):
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((2*(math.factorial(n)*math.factorial(m)))%(10**9+7))

Solution (Python):

n, m = map(int, input().split())
mod = 10**9 + 7
if -2 < n - m < 2:
    d = 1
    s = 1
    for i in range(1, n + 1):
        d = d * i % mod
    for i in range(1, m + 1):
        s = s * i % mod
    print((d * s * (1 + (n == m))) % mod)
else:
    print(0)

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

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

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

    COM_MAX = max(N, M)

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

    if N == M:
        ans = 2 * fac[N] * fac[M] % MOD
    else:
        ans = fac[N] * fac[M] % MOD

    print(ans)
    return


if __name__ == '__main__':
    main()

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

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

Solution (Python):
import sys
import math
n,m = map(int,input().split())
max = max(n,m)
min = min(n,m)
total = n+m
f_n = math.factorial(n)
f_m = math.factorial(m)
if max - min >= 2:
	print(0)
	sys.exit()
if total%2 == 0:
	ans = f_n*f_m*2
else:
	ans = f_n*f_m
if ans > 10**9+7:
	print(ans%(10**9+7))
else:
	print(ans)

Solution (Python):






import math





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





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

Solution (Python):
from math import factorial
n,m=map(int, input().split())
ans=0
if abs(n-m)==0:
    ans=factorial(n)*factorial(m)*2
elif abs(n-m)==1:
    ans=factorial(n)*factorial(m)

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

Solution (Python):
class Factorial():
    def __init__(self, mod=10**9 + 7):
        self.mod = mod
        self._factorial = [1]
        self._size = 1
        self._factorial_inv = [1]
        self._size_inv = 1

    def __call__(self, n):
        return self.fact(n)

    def fact(self, n):
        ''' n! % mod '''
        if n >= self.mod:
            return 0
        self._make(n)
        return self._factorial[n]
    
    def _make(self, n):
        if n >= self.mod:
            n = self.mod
        if self._size < n+1:
            for i in range(self._size, n+1):
                self._factorial.append(self._factorial[i-1]*i % self.mod)
            self._size = n+1

    def fact_inv(self, n):
        ''' n!^-1 % mod '''
        if n >= self.mod:
            raise ValueError('Modinv is not exist! arg={}'.format(n))
        self._make(n)
        if self._size_inv < n+1:
            self._factorial_inv += [-1] * (n+1-self._size_inv)
            self._size_inv = n+1
        if self._factorial_inv[n] == -1:
            self._factorial_inv[n] = self.modinv(self._factorial[n])
        return self._factorial_inv[n]
    
    @staticmethod
    def xgcd(a, b):
        '''
        Return (gcd(a, b), x, y) such that a*x + b*y = gcd(a, b)
        '''
        x0, x1, y0, y1 = 0, 1, 1, 0
        while a != 0:
            (q, a), b = divmod(b, a), a
            y0, y1 = y1, y0 - q * y1
            x0, x1 = x1, x0 - q * x1
        return b, x0, y0

    def modinv(self, n):
        g, x, _ = self.xgcd(n, self.mod)
        if g != 1:
            raise ValueError('Modinv is not exist! arg={}'.format(n))
        return x % self.mod

    def comb(self, n, r):
        ''' nCr % mod '''
        if r > n:
            return 0
        t = self(n)*self.fact_inv(n-r) % self.mod
        return t*self.fact_inv(r) % self.mod
    
    def comb_(self, n, r):
        '''
        nCr % mod
        when r is not large and n is too large
        '''
        c = 1
        for i in range(1, r+1):
            c *= (n-i+1) * self.fact_inv(i)
            c %= self.mod
        return c

    def comb_with_repetition(self, n, r):
        ''' nHr % mod '''
        t = self(n+r-1)*self.fact_inv(n-1) % self.mod
        return t*self.fact_inv(r) % self.mod

    def perm(self, n, r):
        ''' nPr % mod '''
        if r > n:
            return 0
        return self(n)*self.fact_inv(n-r) % self.mod

n, m = map(int, open(0).read().split())
if abs(n-m) > 1:
    print(0)
else:
    mod = 10**9 + 7
    fact = Factorial()
    t = fact(n) * fact(m) % mod
    if n == m:
        print(2*t % mod)
    else:
    	print(t)

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

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

mod = 10 ** 9 + 7

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

    if abs(n - m) == 1:
        print(res)
    else:
        print(res * 2 % mod)

Solution (Python):

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

Solution (Python):

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

S = math.factorial(min(N,M)) % 1000000007

ans = 0
if abs(N-M) > 1:
    ans = 0
elif abs(N-M) == 1:
    ans = S*S*max(N,M)
else:
    ans = S*S*2

ans = ans % 1000000007

print(ans)

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

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

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

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

Solution (Python):

import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil



import string

from bisect import bisect_left
MOD = int(1e9)+7
INF = float('inf')

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

def solve():
    n, m = [int(x) for x in input().split()]
    n, m = min(n,m), max(n,m)
    x = m - n
    if x > 1:
        print(0)
    elif x == 1:
        print((fmod(n) * fmod(m))%MOD)
    else:
        print((fmod(n) * fmod(m)*2)%MOD)


t = 1

for case in range(1,t+1):
    ans = solve()


"""

azyxwvutsrqponmlkjihgfedcb

"""