Problem:


Score : 300 points

Problem Statement

Snuke has N dogs and M monkeys. He wants them to line up in a row.

As a Japanese saying goes, these dogs and monkeys are on bad terms. ("ken'en no naka", literally "the relationship of dogs and monkeys", means a relationship of mutual hatred.) Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys.

How many such arrangements there are? Find the count modulo 10^9+7 (since animals cannot understand numbers larger than that).
Here, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished.

Constraints

1 ≤ N,M ≤ 10^5

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of possible arrangements, modulo 10^9+7.

Sample Input 1

2 2

Sample Output 1

8

We will denote the dogs by A and B, and the monkeys by C and D. There are eight possible arrangements: ACBD, ADBC, BCAD, BDAC, CADB, CBDA, DACB and DBCA.

Sample Input 2

3 2

Sample Output 2

12

Sample Input 3

1 8

Sample Output 3

0

Sample Input 4

100000 100000

Sample Output 4

530123477



Sample input:
2 2


Sample output:
8

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

from math import factorial
mod =(10**9) + 7
# v_n = 1
# for i in range(1,n+1):
#     v_n*= i%mod
# v_m = 1
# for j in range(1,m+1):
#     v_m*= j%mod

v_n = factorial(n)
v_m = factorial(m)
if n==m:
    value = (v_n *v_m * 2)%mod
    print(value)

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

else:
    ans = (v_n * v_m)%mod
    print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    mod=10**9+7
    n,m=map(int,readline().split())
    
    if n==m:
        ans=2*(f(n,mod))**2
    elif n==m-1 or m==n-1:
        ans=f(n,mod)*f(m,mod)
    else:
        ans=0
    ans%=mod
    print(ans)
    
def f(n,mod):
    res=1
    for i in range(1,n+1):
        res*=i
        res%=mod
    return res
    
if __name__=="__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
minmax = [n, m]
result = abs(n-m)
limit = 10**9+7

def lp(animal):
    row = 1
    for i in range(2, animal+1):
        row = row % limit * i 
    return row

if result >= 2:
    print(0)
    exit()

row = lp(min(minmax))
if n == m:
    print(row * row * 2 % limit)
else:
    print(row * row * max(minmax) % limit)

Submission status: Accepted

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

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

result = 1
for i in range(1,n+1):
  result = (result *i) % p
for j in range(1,m+1):
  result = (result *j)  % p
  
if n == m:
  result = (result *2) % p

print(result)

Submission status: Accepted

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

mod = 10**9 + 7

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

elif abs(n-m) == 1:
    a = max(n,m)
    b = min(n,m)
    ans = 1
    for i in range(1,a+1):
        ans *= (i%mod)
        ans %= mod
    for i in range(1,b+1):
        ans *= (i%mod)
        ans %= mod
    print(ans%mod)

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

Submission status: Accepted

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

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

if m>n:
    n,m = m,n
    
f = fact(m)
if m==n:
    print(f * f * 2 % MOD)
else:
    print(f * f * n % MOD)

Submission status: Accepted

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

mod = 10**9 + 7

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

Submission status: Accepted

Solution code:
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
# sys.setrecursionlimit(int(pow(10, 2)))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]
# @lru_cache(None)






n,m=l()
if not (n==m or n+1==m or n==m+1):
    print(0)
    exit()
nf=1
mf=1
for i in range(1,n+1):
    nf=(nf*i)%mod
for i in range(1,m+1):
    mf=(mf*i)%mod
if(m==n):
    mf*=2
print((mf*nf)%mod)

Submission status: Accepted

Solution code:
def nPr(n,r,mod):
    if r==0: return 1
    if r==1: return n
    ans=1
    for i in range(r):
        ans=(n-i)*ans%mod
    return ans%mod
    
n,m=map(int,input().split())

mod=10**9+7
dog=nPr(n,n,mod)
mon=nPr(m,m,mod)

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
P = 10**9 + 7


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

    if abs(N - M) > 1:
        ans = 0
    else:
        n = 1
        for i in range(1, N + 1):
            n = (n * i) % P
        m = 1
        for i in range(1, M + 1):
            m = (m * i) % P

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np

n=10**6
nsq=10**3
mod=10**9+7
fact=np.arange(n,dtype=np.int64).reshape(nsq,nsq); fact[0,0]=1
for n in range(1,nsq):
    fact[:,n]*=fact[:,n-1]; fact[:,n]%=mod
for n in range(1,nsq):
    fact[n]*=fact[n-1,-1];fact[n]%=mod
fact=fact.ravel()

n,m=map(int,input().split())
p=fact[n]
q=fact[m]
if abs(n-m)>=2:
    print(0)
elif abs(n-m)==1:
    print((p*q)%(10**9+7))
else:
    print(2*(p*q)%(10**9+7))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#2問目
import math
MAX = 10 ** 9 + 7
N, M = map(int, input().split())
if(abs(N - M) > 1):
    print(0)
elif(abs(N - M) == 0):
    #犬と猿が同数いる時
    print((((math.factorial(N) % MAX) * (math.factorial(M) % MAX) * 2)) % MAX)
else:
    #犬と猿の差が1の時
    print(((math.factorial(N) % MAX) * (math.factorial(M) % MAX)) % MAX)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N, M = map(int, readline().split())
    if abs(N-M)>1:
        print('0')
    elif N==M:
        fact=[1]*(N+1)
        for i in range(N):
            fact[i+1] = fact[i]*(i+1)%MOD
        ans = 2*fact[N]*fact[M]%MOD
        print(ans)
    else:
        fact = [1] * (N + 3)
        for i in range(N+2):
            fact[i + 1] = fact[i] * (i + 1)%MOD
        ans = fact[N] * fact[M]%MOD
        print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    m,n = map(int,input().split())
    if abs(m-n) > 1:
        print(0)
        return
        
    c = 1
    w = 10**9+7
    for i in range(2, min(m, n)+1):
        c = (c * i) % w
    
    if m == n:
        nc = (c * 2) % w
    else:
        nc = (c * max(m, n)) % w
    
    print(c*nc%w)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# region header
import sys
import math
from bisect import bisect_left, bisect_right, insort_left, insort_right
from collections import defaultdict, deque, Counter
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from heapq import heappop, heappush
from itertools import accumulate, groupby, product, permutations, combinations, combinations_with_replacement
from math import ceil, floor, factorial, log, sqrt, sin, cos
from operator import itemgetter
from string import ascii_lowercase, ascii_uppercase, digits
sys.setrecursionlimit(10**6)
INF = float('inf')
MOD = 10 ** 9 + 7
def rs(): return sys.stdin.readline().rstrip()
def ri(): return int(rs())
def rf(): return float(rs())
def rs_(): return [_ for _ in rs().split()]
def ri_(): return [int(_) for _ in rs().split()]
def rf_(): return [float(_) for _ in rs().split()]
def divisors(n, sortedresult=True):
    div = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            div.append(i)
            if i != n // i:
                div.append(n//i)
    if sortedresult:
        div.sort()
    return div
# endregion

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

N, M = ri_()
if N == M:
    print((2 * (factorialmod(N) ** 2)) % MOD)
elif N == M + 1 or N == M - 1:
    print((factorialmod(N) * factorialmod(M)) % MOD)
else:
    print(0)

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7
fact_len = 10 ** 5
fact = [-1] * (fact_len + 2)
for i in range(fact_len + 1):
    fact[i + 1] = fact[i] * (i + 1)
    fact[i + 1] %= MOD

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

Submission status: Accepted

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

mod = 10**9 + 7

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

elif abs(n-m) == 1:
    a = max(n,m)
    b = min(n,m)
    ans = 1
    for i in range(1,a+1):
        ans *= (i%mod)
        ans %= mod
    for i in range(1,b+1):
        ans *= (i%mod)
        ans %= mod
    print(ans%mod)

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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *
		
#階乗#
lim = 10**5+1  #必要そうな階乗の限界を入力
fact = [1] * (lim+1)
for n in range(1, lim+1):
	fact[n] = n * fact[n-1] % mod

N, M = MAP()

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

ans = (fact[N]*fact[M])%mod

if N == M:
	ans = 2*ans%mod

print(ans)

Submission status: Accepted

Solution code:
from math import factorial

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

combination_num = 0
# if N = M, the number of combination is 2N!M!.
if N == M:
  combination_num = 2 * factorial(N) * factorial(M)
# if |N - M| = 1, the number of combination is M!N!.
elif abs(N - M) == 1:
  combination_num = factorial(N) * factorial(M)
# if none of the above conditions,
# can't line up. so not change combination_num from 0.

print(combination_num % THE_NUM)

Submission status: Accepted

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

p = 10 ** 9 + 7
N = 10 ** 5  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
	fact.append((fact[-1] * i) % p)
	inv.append((-inv[p % i] * (p // i)) % p)
	factinv.append((factinv[-1] * inv[-1]) % p)

if n==m:
	#2つ交互にならべる
	#oxoxoxoxoxox...ox これを2倍したのが答え.
	print(fact[n]*fact[n]*2 % p)
elif n+1==m:
	#犬が1匹多い
	#oxoxoxoxox....oxo.
	print(fact[n]*fact[m] % p)
elif n==m+1:
	print(fact[n]*fact[m] % p)
else:
	print(0)

Submission status: Accepted

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

N, M = min(N, M), max(N, M)

if M-N >= 2:
  print(0)
  exit()
  
frac = [1]*(M+1)
for i in range(M):
  frac[i+1] = frac[i] * (i+1) % 1000000007
  
if M-N == 1:
  ans = frac[M] * frac[N] % 1000000007
  print(ans)
else:
  ans = 2 * frac[M] * frac[M] % 1000000007
  print(ans)

Submission status: Accepted

Solution code:
from math import factorial

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

Submission status: Accepted

Solution code:

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

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

n_res = 1
for i in range(1,N+1):
    n_res = i * n_res % MOD

m_res = 1
for i in range(1,M+1):
    m_res = i * m_res % MOD

if N == M:
    print((n_res **2 * 2) % MOD)
else:
    print((n_res * m_res) % MOD)

Submission status: Accepted

Solution code:
def factorial_(a):
    mem = 1
    for i in range(1, a+1):
        mem = (mem*i)%(10**9 + 7)
    return mem

N, M = map(int, input().split())
a = max([N, M])
b = min([N, M])

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

Submission status: Accepted

Solution code:
def fact(n):
    val = 1
    for i in range(2, n + 1):
        val *= i
        val %= 10**9 + 7
    return val

x, y = map(int,input().split())
ans = 1

if abs(x-y) > 1:
    print(0)

elif x == y:
    print((fact(x)**2 *2) % (10**9+7))

else:
    print((fact(x) * fact(y)) % (10**9+7))

Submission status: Accepted

Solution code:
import math

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)%(10**9 + 7))
  
elif n+1 == m:
  print((math.factorial(m) * math.factorial(n))%(10**9 + 7))
elif m+1 == n:
  print((math.factorial(n) * math.factorial(m))%(10**9 + 7))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import math

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

if n+1 < m or m+1 < n:
    print(0)
    sys.exit()

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

print(ans%max)

Submission status: Accepted

Solution code:
def factmod(n):
    tmp = 1
    for i in range(1,n+1):
        tmp *= i
        tmp %= (10**9+7)
    return tmp   
N,M = list(map(int,input().split()))
if abs(N-M) > 1:
    print(0)
    exit()
if N == M:
    print((2*(factmod(N)*factmod(M))%(10**9+7)))
else:
    print(factmod(N)*factmod(M)%(10**9+7))

Submission status: Accepted

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
import math
# import networkx as nx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n,m=n1()

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

Submission status: Accepted

Solution code:
import math
n, m = map(int, input().split())
diff = abs(n - m)

# 階乗は、factorial

if (diff > 1):
    print(0)
else:
    x = math.factorial(n)
    y = math.factorial(m)

    if (n == m):
        tt = 2
    else:
        tt = 1

    ans = tt * (x * y) % (10 ** 9 + 7)

    print(ans)

Submission status: Accepted

Solution code:
import math

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

if abs(N - M) > 1:
    print(0)
elif abs(N - M) == 1:
    f = math.factorial(min(N, M))
    res = f % mod
    res *= f * max(N, M) % mod
    print(res % mod)
else:
    f = math.factorial(N)
    res = 2 * f % mod
    res *= f
    print(res % mod)

Submission status: Accepted

Solution code:
#dpでできないかな？
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

factmod=[0 for i in range(100000)]
factmod[0]=1
for i in range(100000-1):
    factmod[i+1]=factmod[i]*(i+2)%mod
n,m=MI()
if n>m+1 or m>n+1:
    print(0)
    sys.exit()
elif n==m:
    ans=2*factorial(n)**2
else:
    if m>n:
        m,n=n,m
    ans=factorial(n)*factorial(m)
print(ans%mod)

Submission status: Accepted

Solution code:
#dpでできないかな？
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

'''def factmod(n):
    u=1
    for i in range(1,n+1):
        u=u*i%mod
    return u'''
factmod=[0 for i in range(100000)]
factmod[0]=1
for i in range(100000-1):
    factmod[i+1]=factmod[i]*(i+2)%mod
#print(factmod)
        
n,m=MI()
if n>m+1 or m>n+1:
    print(0)
    sys.exit()
elif n==m:
    ans=2*factmod[n-1]**2
else:
    if m>n:
        m,n=n,m
    ans=factmod[n-1]*factmod[m-1]
print(ans%mod)

Submission status: Accepted

Solution code:
def main():
    n, m = list(map(int, input().split()))
    mod = 1000000007
    def factorial(num, mod):
        ans = 1
        if num <= 1: return ans
        for i in range(2, num + 1):
            ans = (ans * i) % mod
        return ans

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




if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

def permutation(n, x, mod=10**9+7):
    # nPx
    # 順列
    # ex) permutaion(5, 2) = 20
    tmp = 1
    for i in range(n, n-x, -1):
        tmp = (tmp * i) % mod
    return tmp

N, M = map(int, sys.stdin.readline().strip().split())

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

Submission status: Accepted

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

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

if n == m:
    tmp = 1
    for i in range(1,n+1):
        tmp = (tmp*i)%mod
    ans = (2*tmp*tmp)%mod
else:
    tmp = 1
    for i in range(1,min(n,m)+1):
        tmp = (tmp*i)%mod
    ans = (tmp*tmp*max(n,m))%mod

print(ans)

Submission status: Accepted

Solution code:
import math
a,b=map(int,input().split())
A=math.factorial(a)
B=math.factorial(b)
m=10**9+7
if abs(a-b)<=1:
    if a==b:
        print(2*A*B%m)
    else:
        print(A*B%m)
else:
    print(0)

Submission status: Accepted

Solution code:
import math 

divisor = (10 ** 9) + 7

N, M = [int(i) for i in input().split()]

output = 0

n_fact = math.factorial(N) 
m_fact = math.factorial(M) 

diff = abs(N-M)

if diff == 0 :
    output = n_fact * m_fact * 2 % divisor
elif diff == 1:
    output = n_fact * m_fact % divisor 

print(output)

Submission status: Accepted

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


def main():
    MOD = 10**9 + 7
    N, M = list(map(int, input().split(' ')))
    if abs(N - M) > 1:
        print(0)
    elif abs(N - M) == 0:
        print((2 * fact(N, MOD) * fact(M, MOD)) % MOD)
    else:
        print((fact(N, MOD) * fact(M, MOD)) % MOD)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    MOD = 10 ** 9 + 7

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

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

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

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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


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

MAD = 10**9+7
if abs(N-M) >= 2:
    print(0)

elif N == M:
    print((2*factorial(N)*factorial(M)) % MAD)

else:
    print((factorial(N)*factorial(M)) % MAD)

Submission status: Accepted

Solution code:
from math import factorial

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

a = 10**9+7
if abs(N-M) >= 2:
    print(0)

elif N == M:
    print((2*factorial(N)*factorial(M)) % a)

else:
    print((factorial(N)*factorial(M)) % a)

Submission status: Accepted

Solution code:
[n,m] = [int(i) for i in input().split()]
ls = [1]
mod = 10 ** 9 + 7
for i in range(1,10**5+1):
    ls.append((ls[-1]*i) % mod)

if abs(n-m) >= 2:
    print(0)
else:
    ans = (ls[n] * ls[m]) % mod
    if n == m:
        ans *= 2
        ans %= mod
    print(ans)

Submission status: Accepted

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

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


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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

large = max(n, m)
small = min(n, m)

if large == small:
    ans = 2 * large**2 * math.factorial(large - 1)**2 % (10**9 + 7)
else:
    ans = large * math.factorial(small)**2 % (10**9 + 7)

print(ans)

Submission status: Accepted

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

import math

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

Submission status: Accepted

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

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

x = fact[N] * fact[M]

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

print(ans)

Submission status: Accepted

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

if n == m:
    print( (2 * (math.factorial(n)%MOD)**2) % MOD)
elif abs(n-m) == 1:
    print((max(n, m)*(math.factorial(min(n, m))%MOD)**2) % MOD)
else:
    print(0)

Submission status: Accepted

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

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

p = 10**9+7
fact =[1,1]
for i in range(2, max(n,m) + 1):
    fact.append((fact[-1] * i) % p)
    
if n ==m:
    res = fact[n]*fact[m]*2

else:
    res = fact[n]*fact[m]

print(res%p)

Submission status: Accepted

Solution code:
# ABC065 C Reconciled?

import math

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

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