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 math

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

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

    elif n == m:
        ans = math.factorial(n) * math.factorial(m) * 2
        print(ans%1000000007)

    else:
        ans = math.factorial(n) * math.factorial(m)
        print(ans%1000000007)



if __name__ == "__main__":
        main()

Submission status: Accepted

Solution code:
N, M = map(int, raw_input().split())
m = (10**9)+7
if abs(N-M)>1:
  print 0
else:
  s = 1
  for i in range(2, N+1):
    s = (s*i) % m
  for j in range(2, M+1):
    s = (s*j) % m
  if N==M:
    s = (s*2) % m
  print s

Submission status: Accepted

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

md = 10**9 + 7

def m_kaijyo(n) :
    p = 1
    for i in range(1, n + 1) :
        p = p % md * i %md
        p %= md        
    return p

if abs(N - M) in (0, 1) :
    if N == M : 
        print((((m_kaijyo(N) % md) * (m_kaijyo(N) % md)) % md * 2) % md)
    else :
        print(((m_kaijyo(N) % md) * (m_kaijyo(M) % md)) % md)
else :
    print("0")

Submission status: Accepted

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

# 階乗を使う
import math

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

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

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

Submission status: Accepted

Solution code:
mod=1000000007

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

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

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

Submission status: Accepted

Solution code:
import math
def combi(a, b):
    mod = 10**9+7
    if (a+b)%2 == 0:
        return (math.factorial(a)%mod * math.factorial(b)%mod * 2) % mod
    else:
        return (math.factorial(a)%mod * math.factorial(b)%mod) % mod
def main():

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

mod = 10**9+7
Ans = 0

if abs(N-M) == 0:
    t = 1
    for i in range(1,N+1):
        t = (t*i)%mod
    Ans = (2*t*t)%mod
elif abs(N-M) == 1:
    t1,t2 = 1,1
    for i in range(1,N+1):
        t1 = (t1*i)%mod
    for i in range(1,M+1):
        t2 = (t2*i)%mod
    Ans = (t1*t2)%mod

print(Ans)

Submission status: Accepted

Solution code:
import math

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

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
def f(n):
  ans = 1
  i = 1
  while i <= n:
    ans = (ans * i)%(10**9+7)
    i += 1
  return ans
if abs(N-M) >= 2:
  print(0)
if abs(N-M) == 1:
  t = max(N,M) 
  print((f(t)*f(t-1))%(10**9+7))
if N == M:
  print(((2*f(N)**2)%(10**9+7)))

Submission status: Accepted

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

def calc(N, M):
    MODE = 10**9+7
    if N > M:
        N, M = M, N
    if M > N + 1:
        return 0
    r = 1
    for i in range(1, N + 1):
        r = (r * i) % MODE
    for i in range(1, M + 1):
        r = (r * i) % MODE
    if N == M:
        r = (r * 2) % MODE
    return r

print(calc(N, M))

Submission status: Accepted

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

mod = (10**9 + 7)

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

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

Submission status: Accepted

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

Submission status: Accepted

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

if m < n:
    n, m = m, n

if m - n > 1:
    print(0)
else:
    ret *= 2 - (m - n)
    ret %= mod
    print(ret)

Submission status: Accepted

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

MOD = 10**9+7

fac = [1]
for n in range (1,M+2):
    f = (fac[n-1]*n) % MOD
    fac.append(f)

if M-N == 1:
    ans = (fac[M] * fac[N]) % MOD
else:
    ans = (fac[M] * fac[N] * 2) % MOD
print(ans)

Submission status: Accepted

Solution code:
from math import factorial

N, M = list(map(int, input().split()))
perm_N, perm_M = 1, 1
mod = 10**9 + 7
for i in range(1, N+1):
    perm_N = perm_N * i % mod
for i in range(1, M+1):
    perm_M = perm_M * i % mod
if abs(N-M) == 1:
    print(perm_N*perm_M%mod)
elif N == M:
    print(perm_N*perm_M*2%mod)
else:
    print(0)

Submission status: Accepted

Solution code:
MOD = 10**9 + 7
def solve(n, m):
    if n < m:
        n, m = m, n
    if n - m >= 2:
        return 0
    res = 1
    for i in range(n):
        res = (res * (i+1)) % MOD
    for i in range(m):
        res = (res * (i+1)) % MOD
    if n - m == 0:
        res = (res * 2) % MOD
    return res
assert solve(2,2) == 8
assert solve(3,2) == 12
assert solve(1,8) == 0
n, m = map(int, raw_input().split())
print solve(n, m)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
MOD = 10 ** 9 + 7


def main():
    n, m = map(int, input().split())
    total =math.factorial(n+m)
    if abs(n-m) >= 2:
        return 0
    if abs(n-m) == 1:
        return math.factorial(n) * math.factorial(m) % MOD
    return math.factorial(n) * math.factorial(m) * 2 % MOD
print(main())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
N, M = map(int, input().split())
if abs(N-M)>1:
    print(0)
elif N!=M:
    ans = (math.factorial(N)%1000000007)*(math.factorial(M)%1000000007)%1000000007
    print(ans)
else:
    ans = (math.factorial(N)%1000000007)*(math.factorial(M)%1000000007)*2%1000000007
    print(ans)

Submission status: Accepted

Solution code:
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
ans = 1
for i in range(1, N+1):
    ans = ans*i%MOD
ans = ans*ans%MOD
if M > N:
    ans = ans*M%MOD
else:
    ans = ans*2%MOD
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import factorial
mod = 1000000007
N, M = map(int, input().split())
if abs(M-N) > 1:
    print(0)
elif M == N:
    print(2*factorial(M)**2 % mod)
else:
    print(factorial(M)*factorial(N) % mod)

Submission status: Accepted

Solution code:
import math
N, M = map(int, input().split())
if N - M > 1 or M - N > 1:
    key = 0
elif N == M:
    key = math.factorial(N) * math.factorial(M)
    key *= 2
else:
    key = math.factorial(N) * math.factorial(M)
print(key % 1000000007)

Submission status: Accepted

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

# AtCoder Beginner Contest
# Problem C
large_number = 10 ** 9 + 7


def factorial(number: int) -> int:
    value = 1

    for i in range(1, number + 1):
        value = (value * i) % large_number
    return value


if __name__ == '__main__':
    n, m = list(map(int, input().split()))
    diff = abs(n - m)
    result = factorial(min(n, m))

    # See:
    # https://beta.atcoder.jp/contests/abc065/submissions/1654425
    if diff == 1:
        rate = 1
        print(((result * result * max(n, m) * rate) % large_number))
    elif diff == 0:
        rate = 2
        print(((result * result * rate) % large_number))
    else:
        print(0)

Submission status: Accepted

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

# AtCoder Beginner Contest
# Problem C

if __name__ == '__main__':
    n, m = list(map(int, input().split()))
    diff = abs(n - m)
    n_factorial = 1
    m_factorial = 1
    large_number = 10 ** 9 + 7

    for i in range(1, n + 1):
        n_factorial = (n_factorial * i) % large_number

    for k in range(1, m + 1):
        m_factorial = (m_factorial * k) % large_number

    rate = 0

    if diff == 1:
        rate = 1
    elif diff == 0:
        rate = 2

    print(((n_factorial * m_factorial * rate) % large_number))

Submission status: Accepted

Solution code:
def fact(n):
    fact_lst = [0] * 100005
    fact_lst[0] = 1

    for i in range(1, n):
        fact_lst[i] = (i+1) * fact_lst[i-1]
        if fact_lst[i] > 10 ** 9 + 7:
            fact_lst[i] %= 10 ** 9 + 7

    return fact_lst[n-1]


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

if abs(n-m) > 1:
    print(0)
else:
    comb = fact(min(n, m)) ** 2
    comb %= 10 ** 9 + 7

    if n == m:
        comb *= 2
    else:
        comb *= max(n, m)

    comb %= 10 ** 9 + 7
    print(comb)

Submission status: Accepted

Solution code:
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)*math.factorial(m)*2)%mod)
else:
  print((math.factorial(n)*math.factorial(m))%mod)

Submission status: Accepted

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

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

def kaijo(p):
    a = 1
    while p > 0:
        a *= p
        a %= 10**9+7
        p -= 1
    return a

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
mod=10**9+7
import math
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)

Submission status: Accepted

Solution code:
import math
N,M=map(int,input().split())
def f(n):
    n=math.factorial(n)
    return n
if abs(N-M)==1:
    s=f(N)*f(M)
elif N==M:
    s=2*f(N)**2
else:
    s=0
print(s%(10**9+7))

Submission status: Accepted

Solution code:
import math

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

if not abs(n-m)<=1:
    print('0')
    exit()

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

Submission status: Accepted

Solution code:
import math
n,m = list(map(int,input().split()))
a = 0
b = 0

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

Submission status: Accepted

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

def arrengements(n, m):
    if abs(n - m) == 1:
        return (factorial(n) * factorial(m)) % (10 ** 9 + 7)
    elif n == m:
        return (factorial(n) * factorial(m) * 2) % (10 ** 9 + 7)
    else:
        return 0
        

if __name__ == '__main__':
    n, m = map(int, input().split())
    print(arrengements(n, m) % (10 ** 9 + 7))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
mod = 10**9 + 7
def fact(x):
  ret = 1
  for i in range(1, x+1):
    ret = ret * i % mod
  return ret
if n == m:
  print(fact(n)**2 * 2 % mod)
elif n+1 == m or n-1 == m:
  print(fact(n) * fact(m) % mod)
else:
  print(0)

Submission status: Accepted

Solution code:
M = 10**9 + 7
def f(n):
    x = 1
    for i in range(1, n + 1):
        x = x * i % M
    return x

n, m = map(int, input().split())
if n == m:
    x = f(n)
    print(2 * x * x % M)
elif abs(m - n) == 1:
    a = min(m, n)
    x = f(a)
    print(x * x * (a + 1) % M)
else: print(0)

Submission status: Accepted

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

Submission status: Accepted

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

print(int(ans%(1000000000+7)))

Submission status: Accepted

Solution code:
from math import factorial

n, m = map(int, input().split())
calc = pow(10, 9)+7

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

Submission status: Accepted

Solution code:
import sys

res=10**9+7
def permutation(n):
  x=1
  for i in range(n):x=(x*(i+1))%res
  return(x%res)
n,m=map(int,input().split())
n%=res
m%=res
if(n>m+1 or m>n+1):
  print(0)
elif(n==m):
  p=permutation(n)
  print((2*p*p)%res)
else:
  mi=min(n,m)
  p=permutation(mi)
  p2=p*max(n,m)
  print((p*p2)%res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import factorial as fa

n,m = map(int,input().split())
if n == m:
    a = n 
    b = m
else:
    a = max(n,m)
    b = min(n,m)

    
cn = 10**9 + 7

if  a-b > 1:
    print(0)
elif a - b == 1:
    print(fa(a)*fa(b) % cn)
else:
    print(fa(a)*fa(b)*2 % cn)

Submission status: Accepted

Solution code:
import math
n,m = [int(i) for i in input().split()]
if abs(n-m)>=2:
  print(0)
elif abs(n-m)==1:
  print((math.factorial(n) * math.factorial(m))%(10**9 +7))
elif abs(n-m)==0:
  print((math.factorial(n) * math.factorial(m))*2 %(10**9 +7))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
p=10**9+7
temp=list(map(int,input().split(" ")))
a,b=temp

if abs(a-b)>1:
  print(0)

elif a==b:
  print(math.factorial(a)*math.factorial(b)*2%p)
else:
  print(math.factorial(a)*math.factorial(b)%p)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import factorial
mod = 10**9 + 7
N, M = [int(i) for i in input().split()]

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

Submission status: Accepted

Solution code:
MODULO = 10**9+7

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

def ModulousProdct(x, y):
    return x*y % MODULO

def ModulousFactorial(n):
    v = 1
    for i in range(1, n+1):
        v = ModulousProdct(v, i)
    return v


if n == m:
    print(ModulousProdct(ModulousProdct(ModulousFactorial(n), ModulousFactorial(m)), 2))
elif n+1==m or n==m+1:
    print(ModulousProdct(ModulousFactorial(n), ModulousFactorial(m)))
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import factorial

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

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

else:
    print(factorial(M) * factorial(N) *2 % div)

Submission status: Accepted

Solution code:
# main関数
if __name__ == '__main__':

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

    if abs( n - m ) > 1 :
        print(0);
    else:
        ans = 1
        for i in range(1, n+1) :
            ans *= i
            ans %= 1000000007

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

Submission status: Accepted

Solution code:
import math
a,b = map(int,input().split(" "))
if(abs(a-b)>1):
    print("0")
else:
    abikkuri = 1
    ans = 1
    for i in range(a):
        abikkuri = abikkuri * (i+1)
        abikkuri = abikkuri % 1000000007
    ans = abikkuri
    for i in range(b):
        ans = ans * (i+1)
        ans = ans % 1000000007
    if(a==b):
        ans = ans * 2 % 1000000007
    print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
(n, m), mod = map(int, raw_input().split()), 10 ** 9 + 7
if abs(n - m) > 1:
    print '0'
    exit()
def p(n, m):
    ans = 1
    for i in xrange(1, n + 1):
        ans = ((ans % m) * (i % m) % m)
    return ans
print (p(n, mod) * p(m, mod) % mod) if (n + m) % 2 else (p(n, mod) * p(m, mod) * 2) % mod

Submission status: Accepted

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

if abs(N-M) <= 1:
    if (N+M) % 2 == 0:
        tmp = math.factorial(N) * math.factorial(N)
        pt = 2*tmp
    else:
        tmp = math.factorial(min(N,M)) * math.factorial(min(N,M)+1)
        pt = tmp
else:
    pt = 0

print(pt % 1000000007)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
from math import factorial


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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000)
a = list(map(int, input().split()))


def stair(n):
    if n == 1:
        return 1
    else:
        return (n * stair(n - 1)) % 1000000007
if abs(a[0] - a[1]) > 1:
    print("0")
elif abs(a[0] - a[1]) == 1:
    print((stair(a[0]) * stair(a[1]) % 1000000007))
else:
    print((stair(a[0]) * stair(a[1]) * 2)% 1000000007)

Submission status: Accepted

Solution code:

ff = [1, 1]
mod = int(1e9 + 7)

for i in xrange(2, 100002):
    abc = (  ff[i-1] * i ) % mod
    ff.append(abc)
    
#for i in xrange(1, 10):
#    print ff[i]
    
r = lambda : map(int, raw_input().split())

x, y = r()

n = min(x, y)
m = max(x, y)

if m-n > 1 :
    print 0
else :
    if m == n :
        print ( ff[m] * ff[n] * 2) % mod 
    else :
        print ( ff[m] * ff[n] ) % mod

Submission status: Accepted

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

n ,m = (int(i) for i in input().split())
if abs(n - m) > 1:
    print(0)
elif 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))

Submission status: Accepted

Solution code:
import math

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

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

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
import math
N,M=[int(i) for i in input().split()]
mod=10**9+7
#a=[int(input()) for i in range(N)]

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

Submission status: Accepted

Solution code:
div = 1000000007
from math import factorial

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

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

print(res)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://abc065.contest.atcoder.jp/tasks/arc076_a

"""
import sys
from sys import stdin
input = stdin.readline


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

    res = 0
    if N == M:
        combi_N = 1
        for i in range(1, N+1):
            combi_N *= i
            combi_N %= 1000000007
        combi_M = 1
        for i in range(1, M+1):
            combi_M *= i
            combi_M %= 1000000007
        res = combi_N * combi_M * 2
        res %= 1000000007
    elif (N + 1) == M or (M + 1) == N:
        combi_N = 1
        for i in range(1, N+1):
            combi_N *= i
            combi_N %= 1000000007
        combi_M = 1
        for i in range(1, M+1):
            combi_M *= i
            combi_M %= 1000000007
        res = combi_N * combi_M
        res %= 1000000007

    print(res)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
import math

a, b = (int(i) for i in input().split())
_n = 10**9 + 7
if abs(a-b) == 1:
    ab = math.factorial(a) * math.factorial(b)
elif a == b:
    ab = 2*(math.factorial(a) * math.factorial(b))
else:
    ab = 0

print(ab%_n)

Submission status: Accepted

Solution code:
def solve():
    n, m = map(int, input().split())
    if -2 < (n - m) < 2:
        ans = 1
        for i in range(1, n+1):
            ans *= i
            ans %= 1000000007
        for i in range(1, m+1):
            ans *= i
            ans %= 1000000007
        if n != m:
            print(ans)
        else:
            print(((ans) * 2) % 1000000007)
    else:
        print(0)


if __name__=="__main__":
    solve()

Submission status: Accepted

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

MOD = 10 ** 9 + 7

def fact(N, M):
    a = min(N, M)
    b = max(N, M)

    fa = 1
    for i in range(2, a + 1):
        fa = (fa * i) % MOD

    fb = fa
    for i in range(a + 1, b + 1):
        fb = (fb * i) % MOD

    return (fa * fb) % MOD


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

print(ans)

Submission status: Accepted

Solution code:
from math import factorial

a, b = [int(i) for i in input().split()]
num = 1
if abs(a-b) == 1:
    num = factorial(a)*factorial(b)
elif abs(a-b) == 0:
    num = factorial(a) * factorial(b) * 2
else:
    num = 0

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

Submission status: Accepted

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

if abs(n-m) > 1:
    print(0)
    exit()
else:
    for x in range(1, n+1):
        ans *= x
        ans %= z
    for y in range(1, m+1):
        ans *= y
        ans %= z

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

print(ans)

Submission status: Accepted

Solution code:
import itertools

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

if abs(n-m) > 1:
    print(0)
    exit()
else:
    for x in range(1, n+1):
        ans *= x
        ans %= z
    for y in range(1, m+1):
        ans *= y
        ans %= z

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import math


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

    if math.fabs(n-m) > 1: k = 0
    elif n == m: k = 2
    else: k = 1

    print(math.factorial(n)*math.factorial(m)*k % (10**9+7))
    

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
mod = 10 ** 9 + 7
d = abs(n-m)
if(d>1):
    print(0)
else:
    factList = [None] * (max(n, m) + 1)
    for i in range(len(factList)):
        if(i==0):
            factList[i] = 1
            continue
        factList[i] = factList[i-1] * i % mod

    if(d==1):
        print(factList[n]*factList[m] % mod)
    else:
        print(factList[n]*factList[m]*2 % mod)

Submission status: Accepted

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

MOD = 10**9+7
dp = [0 for _ in range(10**5+2)]

dp[0] = 1
dp[1] = 1

for i in range(2, 10**5+2):
    dp[i] = i*dp[i-1] % MOD

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

Submission status: Accepted

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

MOD = 10**9+7

dp = [0 for _ in range(10**5+2)]
dp[0] = 1
for i in range(max(N, M)+1):
    dp[i+1] = dp[i]*(i+1)%MOD


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

Submission status: Accepted

Solution code:
import math

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

fact = [1]
for x in range(1, max(N,M)+1):
	fact.append( (x * fact[x-1]) % C )
    
if math.fabs(N-M) >= 2:
    print(0)
elif math.fabs(N-M) == 1:
    print( (fact[N] * fact[M]) % C )
else:
    print( (fact[N] * fact[M] * 2) % C )

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

N, M = map(int, raw_input().split())
# print N, M                                                                                                                                                                                                                                  

ans = 1
if abs (N - M) > 1:
    ans = 0
elif N != M:
    for i in reversed(range(1, N+1)):
        ans *= i
        ans %= 1e9 + 7
    for i in reversed(range(1, M+1)):
        ans *= i
        ans %= 1e9 + 7
elif N == M:
    for i in reversed(range(1, N+1)):
        ans *= i
        ans %= 1e9 + 7
    for i in reversed(range(1, M+1)):
        ans *= i
        ans %= 1e9 + 7
    ans *= 2
    ans %= 1e9 + 7

print int(ans)

Submission status: Accepted

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

Submission status: Accepted

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