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:
n,m = map(int,input().split())
a = 10**9+7
if m > n:
    n,m = m,n
if not (0 <= n-m <= 1):
    print(0)
    exit()
 
if n == m:
    num = 2
    n = 1
else:
    num = 1
t = 1
for i in range(1,m+1):
    t *= i
    t %= a
print(num*t*t*n%a)

Submission status: Accepted

Solution code:
import math
n,m = map(int,input().split())
a = 1000000007
if not (0 <= abs(n-m) <= 1):
    print(0)
    exit()

if n == m:
    num = 2
else:
    num = 1
print(num*math.factorial(n)*math.factorial(m) % a)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
if abs(n-m)>=2:
    print(0)
else:
    tmpd=n
    tmpm=m
    dog=1
    while tmpd>0:
        dog=(dog*tmpd)%(10**9+7)
        tmpd-=1
    monkey=1
    while tmpm>0:
        monkey=(monkey*tmpm)%(10**9+7)
        tmpm-=1
    if abs(n-m)==1:
        print((dog*monkey)%(10**9+7))
    else:
        print((2*dog*monkey)%(10**9+7))

Submission status: Accepted

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


MOD = 1000000007

ans = 0
if abs( N - M ) <= 1: 
    if N == M: ans = ( N + M ) * N
    else:      ans = N * M
    
    while N > 1 and M > 1:
            ans *= ( N - 1 ) * ( M - 1 ) % MOD
            ans %= MOD
            N -= 1
            M -= 1

print( ans )

Submission status: Accepted

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


MOD = 1000000007
if N < M: N, M = M, N  #  常に N >= M とする

ans = 0
if N - M <= 1: 
    if N == M: ans = ( N + M ) * N
    else: ans = N * M
    
    while N > 1 and M > 1:
            ans *= ( N - 1 ) * ( M - 1 ) % MOD
            ans %= MOD
            N -= 1
            M -= 1

print( ans )

Submission status: Accepted

Solution code:
import math
n,m = (int(i) for i in input().split())
if abs(n-m) > 1:
    print(0)
    exit()

res = math.factorial(n) * math.factorial(m)
if n==m:
    res *= 2
print(res%(10**9+7))

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:
        print((factorial(n)**2*2)%(10**9+7))
    else:
        print((factorial(min(n,m))**2*max(n,m))%(10**9+7))

Submission status: Accepted

Solution code:
import itertools
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))

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:
    a = math.factorial(n)
    b = math.factorial(m)
    print((a * b) % ((10 ** 9)+7))
else:
    a = math.factorial(n)
    b = math.factorial(m)
    print((2 * a * b) % ((10 ** 9)+7))

Submission status: Accepted

Solution code:
# 問題文読み違えてた
# 同じのが隣り合ったらダメ
# 数が同じ -> 交互に並べる
# 差が1 -> 大きいほうが外側両端をとる
# 差がそれ以上 -> はいどうしたって無理
n, m = map(int, input().split())
if n == m:
    ans = 2  # 同じ数なので入れ替えたときのやつ
    for i in range(2, n+1):
        ans *= i
        ans %= 10**9+7
        ans *= i
        ans %= 10**9+7
    print(ans)
elif abs(n - m) == 1:
    ans = 1
    for i in range(2, n+1):
        ans *= i
        ans %= 10**9+7
    for i in range(2, m+1):
        ans *= i
        ans %= 10**9+7
    print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
def bikkuri(A):
    ans=1
    for i in range(A):
        ans*=i+1
        ans%=1000000007
    return ans

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

Submission status: Accepted

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

if abs(N-M)>=2:
  print 0
else:
  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:
    ans *= 2
    ans %= mod
  print ans

Submission status: Accepted

Solution code:
MOD = 10**9+7


def main():
    N, M = map(int, input().split())
    if abs(N-M) >= 2:
        print(0)
        return
    F = [1] * (max(N, M) + 1)
    for i in range(2, max(N, M) + 1):
        F[i] = F[i-1] * i % MOD
    if N == M:
        print((F[N] * F[M] * 2) % MOD)
    else:
        print((F[N] * F[M]) % MOD)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
A, B = map(int,input().split())
mod = 10**9+7
def factorical(n):
  ret = 1
  for i in range(1,n+1):
    ret *= i
    ret %= mod
  return ret%mod

a = factorical(A)
b = factorical(B)
if(abs(A-B) >= 2):
  print(0)
elif(A == B):
  print(a*b*2%mod)
else:
  print(a*b%mod)

Submission status: Accepted

Solution code:
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
input = sys.stdin.readline
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,M = IL()

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

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

print(ans)

Submission status: Accepted

Solution code:
import math
def f(x):
    return math.factorial(x) % (10**9 + 7)

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

if abs(a-b) <= 1:
    print((f(a)*f(b)) % (10**9 + 7) if abs(a-b) == 1 else (2*f(a)*f(b)) % (10**9 + 7))
else:
    print(0)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys

def main():
	N, M = map(int, input().split(" "))
	
	if N - M >= 2 or M - N >= 2:
		print(0)
		sys.exit()
		
	A_ans = 1
	for i in range(1, M+1):
		A_ans = (A_ans * i) % 1000000007
		
	B_ans = 1
	for i in range(1, N+1):
		B_ans = (B_ans * i) % 1000000007
	
	ans = (A_ans * B_ans) % 1000000007
	if N == M:
		ans = (ans * 2) % 1000000007
		
	print(ans)
		
if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
M = 1000000007

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

def solve(n, m):
    d = abs(n-m)

    if d > 1:
        return 0
    elif d == 1:
        return fact(n) * fact(m) % M
    else:
        return fact(n) * fact(m) * 2 % M

def fact(n):
    s = 1
    for i in range(2, n+1):
        s = (s * i) % M
    return s

main()

Submission status: Accepted

Solution code:
M = 1000000007

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

def solve(n, m):
    d = abs(n - m)

    if d > 1:
        return 0
    elif d == 1:
        return fact(n) * fact(m) % M
    else:
        return fact(n) * fact(m) * 2 % M

def fact(n):
    s = 1
    for i in range(2, n + 1):
        s = (s * i) % M
    return s

main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import factorial as f

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

if n==m:
    print(2*f(n)*f(m)%mod)
elif abs(n-m)<2:
    print(f(n)*f(m)%mod)
else:
    print(0)

Submission status: Accepted

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

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

if n==m:
    print(2*(a(n)**2)%mod)
elif n==m+1:
    print(a(n)*a(n-1)%mod)
elif m==n+1:
    print(a(m)*a(m-1)%mod)
else:
    print(0)

Submission status: Accepted

Solution code:
MOD = 10**9 + 7
def Mod_Permutation(n, k, mod):
    ret = 1
    for i in range(n - k + 1, n + 1):
        ret = ret * i % mod
    return ret
N, M = map(int, input().split())
if abs(N - M) > 1:
    print(0)
elif N == M:
    print(int(Mod_Permutation(N, N, MOD) * (2 * Mod_Permutation(N, M, MOD) % MOD) % MOD))
else:
    if N < M:
        T = N
        N = M
        M = T
    print(int(Mod_Permutation(N, N, MOD) * Mod_Permutation(M, M, MOD) % MOD))

Submission status: Accepted

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

MOD = 10 ** 9 + 7

if abs(n - m) > 1:
    print(0)
elif abs(n - m) == 1:
    if n < m:
        n, m = m, n
    temp = 1
    for i in range(1, n + 1):
        b = temp
        temp = temp * i % MOD
    print((temp * b) % MOD)
else:
    temp = 1
    for i in range(1, n + 1):
        temp = (temp * i) % MOD
    print((temp * temp * 2) % MOD)

Submission status: Accepted

Solution code:
a, b = map(int, input().split())

def A(n):
    ans = 1
    while(n != 1):
        ans = ans * n % 1000000007
        n -= 1
    return ans

if(a == b):
    t = A(a)
    print(t * t * 2 % 1000000007)
elif(a == b + 1 or b == a + 1):
    print(A(a) *A(b) % 1000000007)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

x = math.factorial(N) * math.factorial(M)

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

Submission status: Accepted

Solution code:
mod = 10**9 + 7
a, b  = map(int,input().split())
k = abs(a-b)
def f(i):
  dest = 1
  while i != 0:
    dest *= i
    dest %= mod
    i -= 1
  return dest
if k >= 2:
  print(0)
elif k == 1:
  print( f(a) * f(b) % mod)
elif k == 0:
  print( 2 * f(a) * f(b) % mod)

Submission status: Accepted

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

Submission status: Accepted

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

def fact(i):
    count = 1
    for p in range(2,i+1):
        count *= p
        count = count % (10**9+7)
    return count
if N+1 < M or N > M+1:
    print(0)
else:
    if (N+M) % 2 == 0:
        print(fact(N)*fact(M)*2 % (10**9 + 7))
    else:
        print(fact(N)*fact(M)%(10**9 + 7))

Submission status: Accepted

Solution code:
import math

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

if N in [M, M+1, M-1]:
    n = math.factorial(N) * math.factorial(M)
    if N == M:
        print(n*2 % (10**9+7))
    else:
        print(n % (10**9+7))
else:
    print(0)

Submission status: Accepted

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

def factorical(n):
  ret = 1
  for i in range(1,n+1):
    ret *= i
    ret %= 10**9+7
  return ret

diff = abs(N - M)
max_ = int(1e9+7)

if(diff >= 2):
  print(0)
elif(diff == 1):
  n = N % max_
  m = M % max_
  print((factorical(n)*factorical(m)) % max_)
else:
  n = N % max_
  m = M % max_
  print((2*factorical(n)*factorical(m)) % max_)

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = map(int,input().split())   |	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = list(map(int,input().split()))	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]


from collections import defaultdict, Counter
import math
from bisect import bisect_left, bisect_right
import numpy as np

mod = 1000000007

def mul(a, b):
    return ((a % mod) * (b % mod)) % mod

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

if n==m:
    a = math.factorial(n)
    print(mul(mul(a, a), 2))
elif n==m-1 or n-1==m:
    if n<m:
        a = math.factorial(n)
        b = a*m
    else:
        a = math.factorial(m)
        b = a*n
    print(mul(a, b))
else:
    print(0)

Submission status: Accepted

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

def factorial_mod(A):
    kaiji = 1
    for i in range(A):
        kaiji*=(i+1)
        kaiji = kaiji%1000000007
    return kaiji
    
if sa >= 2:
    print(0)
elif sa == 1:
    n = N%1000000007
    m = M%1000000007
    nn = factorial_mod(n)
    mm = factorial_mod(m)
    ans =(nn*mm)%1000000007
    print(ans)
else:
    n = N%1000000007
    m = M%1000000007
    nn = factorial_mod(n)
    mm = factorial_mod(m)
    ans = (2*nn*mm)%1000000007
    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 1
modulo = 10**9+7
if N==M:
    for i in range(N):
        ans *= (i+1)
        ans %= modulo
        ans *= (i+1)
        ans %= modulo
    ans *= 2
    ans %= modulo
else:
    A,B = max(M,N),min(N,M)
    if A-B==1:
        for i in range(N):
            ans *= (i+1)
            ans %= modulo
        for i in range(M):
            ans *= (i+1)
            ans %= modulo
    else:
        ans = 0

print(ans)

Submission status: Accepted

Solution code:
def solve(N, M):
    d = 10 ** 9 + 7
    N, M = min(N, M), max(N, M)
    if abs(N - M) >= 2:
        return 0
    elif abs(N - M) == 1:
        ans = M
    else:
        ans = 2
    for i in range(1, N + 1):
        ans *= pow(i, 2, d)
        ans %= d
    return ans


if __name__ == "__main__":
    N, M = tuple(map(int, input().split(" ")))
    print(solve(N, M))

Submission status: Accepted

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

Submission status: Accepted

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

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

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

    Nfact = 1
    MOD = 10**9 + 7

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

    Nfact2 = Nfact * Nfact % MOD

    if N == M:
        return Nfact2 * 2 % MOD
    else:
        return (N+1) * Nfact2 % MOD


def main():
    print(solve())


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

mod = 10**9 + 7
def kaijou(N):
	x = 1
	for i in range(N):
		x*=i+1
		x%=mod
	return x

kn = kaijou(N)
km = kaijou(M)
if abs(N-M) >=2:
	ans = 0

else:
	if abs(N-M) == 0:
		
		ans = kn*kn*2 % mod

	if abs(N-M) == 1:

		ans = kn * km % mod

print(ans)

Submission status: Accepted

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

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

if abs(n-m) >= 2:
    print(0)
else:
    if (n+m)%2 == 0:
        print(fn*fm*2%mod)
    else:
        print(fn*fm%mod)

Submission status: Accepted

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

g = [1, 1]
for i in range(2, M+1):
    g.append((g[-1] * i) % mod)

if M-N > 1:
    print(0)
elif M-N == 1:
    print(g[M]*g[N] % mod)
else:
    print(g[M]**2*2 % mod)

Submission status: Accepted

Solution code:
from math import factorial as F
N, M = map(int, input().split())
N, M = min(N, M), max(N, M)
mod = 10**9+7

if M-N > 1:
    print(0)
elif M-N == 1:
    print(F(M)*F(N) % mod)
else:
    print(F(M)**2*2 % mod)

Submission status: Accepted

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

g1 = [1, 1]
for i in range(2, M+1):
    g1.append((g1[-1] * i) % mod)

if M-N > 1:
    print(0)
elif M-N == 1:
    print(g1[M]*g1[N] % mod)
else:
    print(g1[M]**2*2 % mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7

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

def main(n, m):
    if n == m:
        return 2 * f(n) * f(m) % MOD
    if abs(n - m) == 1:
        return f(n) * f(m) % MOD
    return 0

if __name__ == '__main__':
    n, m = map(int, input().split())
    print(main(n, m))

Submission status: Accepted

Solution code:
import math
def a():return input()#s
def ai():return int(input())#a,n or k
def ma():return map(int, input().split())#a,b,c,d or n,k
def ms():return map(str, input().split())#,a,b,c,d
def lma():return list(map(int, input().split()))#x or y
def lms():return list(map(str, input().split()))#x or y
k= []
a,b = ma()
mod = 10**9 + 7
m = max(a,b)
n = min(a,b)
k=math.factorial(n)
if abs(a-b)>1:print(0)
elif m!=n:
    print(((k**2)*m)%mod)
else:
    print(((k**2)*2)%mod)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import math
n = sorted(list(int(i) for i in input().split()),reverse=True)
mod = 10**9+7
if n[0]-n[1] >= 2:
 #並べない
 ans = 0
elif n[0]-n[1] ==1:
 ans = ((math.factorial(n[0])%mod)*(math.factorial(n[1])%mod)%mod)
else:
 ans = (2*(math.factorial(n[0])%mod)*(math.factorial(n[1])%mod)%mod)    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
mod = 10**9+7

def fact(n):
    ans = 1
    while n > 0:
        ans = (ans*n) % mod
        n -= 1
    return ans

N,M = map(int,input().split())
if N-M < -1 or N-M > 1:
    print(0)
elif N-M == 0:
    tmp = fact(N)
    print(2*tmp*tmp % mod)
else:
    print(fact(N)*fact(M) % mod)

Submission status: Accepted

Solution code:
from sys import stderr, setrecursionlimit, exit
from math import factorial
setrecursionlimit(2147483647)
def getInt():
    return int(input())
def getInts():
    return [int(i) for i in input().split()]
def getIntLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInt())
    return res
def getIntsLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInts())
    return res
def debug(*args, **kwargs):
    print(*args, file=stderr, **kwargs)

mod = 10**9 + 7

n, m = getInts()
if abs(n-m) >= 2:
    print(0)
    exit()
else:
    a = factorial(min(n, m))
    if n == m:
        print((((a**2) % mod) * 2 ) % mod)
    else:
        print((((a**2) % mod) * max(m, n)) % mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

Submission status: Accepted

Solution code:
import math

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

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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


abc065_c()

Submission status: Accepted

Solution code:
def fact_10_9_7(x):
    ans = 1
    for i in range(1, x+1):
        ans = (i * ans) % (int(1e9) + 7)
    return ans

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

ans = 0
if N == M:
    ans = (fact_10_9_7(N) * fact_10_9_7(M) % (int(1e9) + 7)) * 2 % (int(1e9) + 7)
elif N == M+1 or N+1 == M:
    ans = fact_10_9_7(N) * fact_10_9_7(M) % (int(1e9) + 7)
print(ans)

Submission status: Accepted

Solution code:
MOD = 1000000007


def main():
    n, m = map(int, input().split())
    if abs(n - m) > 1:
        print(0)
        return

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

	ans = fact(n, p) * fact(m, p) % p
	print(ans*2%p if n==m else ans)

Submission status: Accepted

Solution code:
N, M = [int(_) for _ in input().split()]
mod = 10**9+7

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

if N == M:
	answer = 1
	for i in range(2, N+1):
		answer = answer * i
		answer = answer % mod
	answer = answer**2 * 2 % mod
	print (answer)

elif N == M+1:
	answer = 1
	for i in range(2, M+1):
		answer = answer * i 
		answer = answer % mod
	answer = answer**2 * N % mod
	print(answer)

else:
	print(0)

Submission status: Accepted

Solution code:
N, M = [int(_) for _ in input().split()]
mod = 10**9+7

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

if N == M:
	answer = 2
	for i in range(2, N+1):
		answer = answer * i * i
		answer = answer % mod
	print (answer)

elif N == M+1:
	answer = 1
	for i in range(2, N):
		answer = answer * i * i
		answer = answer % mod
	answer = answer * N % mod
	print(answer)

else:
	print(0)

Submission status: Accepted

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

elif abs(n-m) == 1:
    x = max(n, m)
    y = min(n, m)
    ans = 1
    for i in range(1, y+1):
        ans *= (i%p)**2%p
        ans %= p
    ans *= x%p
    print(ans%p)
else:
    print(0)

Submission status: Accepted

Solution code:
import math as m
N,M = map(int,input().split())
mod = 10**9 + 7
if not abs(N-M) <= 1:
    print(0)
    exit()
if N == M:
    num = pow(m.factorial(N),2,mod)
    ans = pow(2*num,1,mod)
else:
    num1 = pow(m.factorial(N),1,mod)
    num2 = pow(m.factorial(M),1,mod)
    ans = pow(num1*num2,1,mod)
print(ans)

Submission status: Accepted

Solution code:
#065-C
n,m=map(int,input().split())
if abs(n-m)>1:
    print(0)
else:
    ans=0
    f=[1 for i in range(max(n,m)+1)]
    for i in range(1,max(n,m)+1):
        f[i]=f[i-1]*i%(10**9+7)
    if m==n:
        ans=2*f[m]*f[n]%(10**9+7)
    else:
        ans=f[m]*f[n]%(10**9+7)
    print(ans)

Submission status: Accepted

Solution code:
import itertools
import math

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

al = [i for i in range(N)]
bl = [i for i in range(M)]

mod = (pow(10,9) + 7)
result = 0
if abs(N - M) > 1:
    result = 0
else:
    a_perm = math.factorial(N) % mod
    b_perm = math.factorial(M) % mod
    if abs(N - M) == 1:
        result = (a_perm * b_perm) % mod
    elif abs(N - M) == 0:
        result = ((a_perm * b_perm) * 2) % mod

print(result)

Submission status: Accepted

Solution code:
import itertools
import math

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

al = [i for i in range(N)]
bl = [i for i in range(M)]

mod = (pow(10,9) + 7)
result = 0
if abs(N - M) > 1:
    result = 0
else:
    a_perm = math.factorial(N)
    b_perm = math.factorial(M)
    if abs(N - M) == 1:
        result = (a_perm * b_perm) % mod
    elif abs(N - M) == 0:
        result = ((a_perm * b_perm) * 2) % mod

print(result)

Submission status: Accepted

Solution code:
from math import factorial

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

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:
    ans = factorial(N) * factorial(M)
    if N == M:
        ans = (2 * ans) % (10 ** 9 + 7)
    else:
        ans = ans % (10 ** 9 + 7)
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import factorial

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

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

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

else:
    print(0)

Submission status: Accepted

Solution code:
a=list(map(int,input().split()))
N=a[0]
M=a[1]
t=max(N,M)
fact=[1,]*(t+1)
for i in range(1,t+1):
    fact[i]=fact[i-1]*i % (10**9+7)
if abs(N-M) > 1:
    print("0")
elif N == M:
    print(str(2*fact[N]*fact[M] % (10**9+7)))
else:
    print(str(fact[N]*fact[M] % (10**9+7)))

Submission status: Accepted

Solution code:
N, M = [int(_) for _ in input().split()]
mod = 10**9+7


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


if N < M:
    N, M = M, N
if N == M:
    ans = 2*fact(N)**2 % mod
elif N == M+1:
    ans = N*fact(M)**2 % mod
else:
    ans = 0
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:
    print(math.factorial(n) * math.factorial(m) % mod)
elif n == m:
    print(2 * math.factorial(n) * math.factorial(m) % mod)

Submission status: Accepted

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

if abs(n-m) > 1:
    print(0)
    exit()
count = 1
for i in range(1, n + 1):  # n!
    count *= i
    count %= (10**9 + 7)
for i in range(1, m + 1):  # n!
    count *= i
    count %= (10 ** 9 + 7)
if n == m:
    count *= 2
print(int(count % (10**9 + 7)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
mod = 10**9+7
fac = [1]
for i in range(1,10**5+10):
	fac.append(fac[-1]*i%mod)
n,m = map(int,raw_input().split())
if abs(n-m) > 1:
	print "0"
elif abs(n-m) == 1:
	print fac[n]*fac[m]%mod
else:
	print 2*fac[n]*fac[m]%mod

Submission status: Accepted

Solution code:
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)) % (10**9+7))
elif abs(N-M) == 0:
    print((factorial(N)*factorial(M)*2) % (10**9+7))