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
#import copy
#import sys
#import bisect
#input = sys.stdin.readline
mod=10**9+7
def factorial(x): #階乗計算
    n=1
    for i in range(x):
        n=(n*(i+1))%mod
    return n

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)
def lcm(a, b):
    return((a*b)//gcd(a,b))
def div(x): #約数配列
    div=[1]
    check=2
    while(x!=1 and check <= int(x**0.5)+2):
        if(x%check==0):
            div.append(check)
            while(x%check==0):
                x=x//check
        check+=1
    if x != 1:
      div.append(x)
    return div
def div2(x): #素因数分解配列
    div2=[]
    check=2
    while(x!=1 and check <= int(x**0.5)+2):
        while x%check==0:
            div2.append(check)
            x/=check
        check+=1
    if x != 1:
      div2.append(x)
    return div2

def main():
    n,m = map(int,input().split())    
    if abs(n-m)>1:
        print(0)
    else:
        a=max(n,m)
        b=min(n,m)
        if a>b:
            print((factorial(a)*factorial(b))%mod)
        else:
            ans=(factorial(a)**2)%mod
            print((2*ans)%mod)
main()

Submission status: Accepted

Solution code:
import sys

MOD = (10**9)+7
N,M = map(int,input().split())
if not(N == M or N+1 == M or N == M+1):
    print(0)
    sys.exit()

if N == M:
    ans = 2
else:
    ans = 1
for i in range(1,N+1):
    ans *= i
    ans %= MOD
for i in range(1,M+1):
    ans *= i
    ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
import math 

INF = 10**9 + 7
n,m = map(int, input().split())
ans = 0
n,m = max(n,m),min(m,n)
a = math.factorial(n)%INF
b = math.factorial(m)%INF
ans = a*b
if n==m:
    ans = ans*2
    ans = ans%INF
elif n ==m+1:
    ans = ans%INF
else:
    ans = 0
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import math
MOD = 1000000007  # type: int


def solve(N: int, M: int):
    if abs(N-M) >= 2:
        print(0)
        return 
    
    ## Nの並び替えパターン
    N_number = math.factorial(N)%MOD
    M_number = math.factorial(M)%MOD

    if N == M:
        print((N_number*M_number*2)%MOD)
    else:
        print((N_number*M_number)%MOD)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    solve(N, M)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 階乗 x!
def fact(x):
    if x<1:
        return 1
    global p
    res=1
    for i in range(2, x+1):
        res=(res*i)%p
    return res

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

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

print(ans)

Submission status: Accepted

Solution code:
# 逆数 x^(-1)
def inv(x):
    global p
    res=1
    prod=x
    e=p-2
    while e>0:
        if e%2==1:
            res=(res*prod)%p
        prod=(prod*prod)%p
        e//=2
    return res

# 階乗 x!
def fact(x):
    if x<1:
        return 1
    global p
    res=1
    for i in range(2, x+1):
        res=(res*i)%p
    return res

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if a+1 < b:
    print(0)
elif a > b+1:
    print(0)
elif a == b:
    print(math.factorial(a)%mod*math.factorial(b)%mod*2%mod)
else:
    print(math.factorial(a)%mod*math.factorial(b)%mod)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
N, M = map(int,input().split())
N, M = max(N,M), min(N,M)
mod = 10**9 + 7
if N-M != 1 and N != M:
    ans = 0
elif N != M:
    factN = math.factorial(N)
    ans = (factN*factN//N)%mod
else:
    factN = math.factorial(N)
    ans = (factN*factN*2)%mod
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


def factorial(x):
    FCT = 1
    for i in range(2, x + 1):
        FCT = i * FCT % INF
    return FCT


def main():
    N, M = map(int, input().split())
    if N == M:
        print(2 * factorial(min(N, M)) ** 2 % INF)
    elif abs(N - M) <= 1:
        print(factorial(min(N, M)) ** 2 * max(N, M) % INF)
    else:
        print(0)

    return


main()

Submission status: Accepted

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


def factorial(x):
    FCT = 1
    for i in range(2, x + 1):
        FCT = i * FCT % INF
    return FCT


def main():
    N, M = map(int, input().split())
    MIN_NM = factorial(min(N, M))
    if N == M:
        print(2 * MIN_NM ** 2 % INF)
    elif abs(N - M) <= 1:
        print(MIN_NM ** 2 * max(N, M) % INF)
    else:
        print(0)

    return


main()

Submission status: Accepted

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


def factorial(x):
    FCT = 1
    for i in range(2, x + 1):
        FCT = i * FCT % INF
    return FCT


def main():
    N, M = map(int, input().split())
    MIN_NM = factorial(min(N, M))
    if N == M:
        print(2 * MIN_NM * MIN_NM % INF)
    elif abs(N - M) <= 1:
        print(MIN_NM * MIN_NM * max(N, M) % INF)
    else:
        print(0)

    return


main()

Submission status: Accepted

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


def modmulti(a, b):
    # aとbを掛けた値をmodする(a * b mod p)
    return a * b % INF


def factorial(x):
    FCT = 1
    for i in range(2, x + 1):
        FCT = modmulti(i, FCT)
    return FCT


def main():
    N, M = map(int, input().split())
    MIN_NM = factorial(min(N, M))
    if N == M:
        print(modmulti(2, modmulti(MIN_NM, MIN_NM)))
    elif abs(N - M) <= 1:
        print(modmulti(MIN_NM, modmulti(MIN_NM, max(N, M))))
    else:
        print(0)

    return


main()

Submission status: Accepted

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


def modmulti(a, b):
    # aとbを掛けた値をmodする(a * b mod p)
    return a * b % INF


def factorial(x):
    FCT = 1
    for i in range(2, x + 1):
        FCT = modmulti(i, FCT)
    return FCT


def main():
    N, M = map(int, input().split())
    MIN_NM = factorial(min(N, M))
    if N == M or N + 1 == M or N == M + 1:
        print(modmulti(2, modmulti(MIN_NM, MIN_NM))
              if N == M else modmulti(MIN_NM, modmulti(MIN_NM, max(N, M))))
    else:
        print(0)

    return


main()

Submission status: Accepted

Solution code:
from functools import reduce
INF = int(1e9 + 7)


def modmulti(a, b):
    # aとbを掛けた値をmodする(a * b mod p)
    return a * b % INF


def factorial(x):
    FCT = 1
    FCT = reduce(modmulti, range(2, x + 1), FCT)
    return FCT


def main():
    N, M = map(int, input().split())
    MIN_NM = factorial(min(N, M))
    if N == M or N + 1 == M or N == M + 1:
        print(modmulti(2, modmulti(MIN_NM, MIN_NM))
              if N == M else modmulti(MIN_NM, modmulti(MIN_NM, max(N, M))))
    else:
        print(0)

    return


main()

Submission status: Accepted

Solution code:
from functools import reduce
INF = int(1e9 + 7)


def modmulti(a, b):
    # aとbを掛けた値をmodする(a * b mod p)
    return a * b % INF


def factorial(x):
    FCT = 1
    FCT = reduce(modmulti, range(2, x + 1), FCT)
    return FCT


def main():
    N, M = map(int, input().split())
    if N == M or N + 1 == M or N == M + 1:
        print(modmulti(2, modmulti(factorial(N), factorial(M)))
              if N == M else modmulti(factorial(N), factorial(M)))
    else:
        print(0)

    return


main()

Submission status: Accepted

Solution code:
INF = int(1e9 + 7)
FCT = [0] * (int(1e5) + 1)


def modmulti(a, b):
    # aとbを掛けた値をmodする(a * b mod p)
    return a * b % INF


def factorial(x):
    FCT[0] = 1
    FCT[1] = 1
    for i in range(2, x + 1):
        FCT[i] = modmulti(FCT[i - 1], i)


def main():
    N, M = map(int, input().split())
    if N == M or N + 1 == M or N == M + 1:
        if N < M:
            factorial(M)
        else:
            factorial(N)
        print(modmulti(2, modmulti(FCT[N], FCT[M]))
              if N == M else modmulti(FCT[N], FCT[M]))
    else:
        print(0)

    return


main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC065 C - Reconciled?
N,M = map(int,input().split())
mod = 10**9+7
ans = 0
dog = 1
monkey = 1
if abs(N-M) > 1:
    print(ans)
    exit()
elif N == M:
    for i in range(1,N+1):
        dog = (dog*i)%mod

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

elif abs(N-M) <= 1:
    for i in range(1,N+1):
        dog = (dog*i)%mod

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

n,m = map(int,input().split())
a = max(n,m)
b = min(n,m)
if a>b+1:
    print(0)
    exit()
if a>b:
    print((math.factorial(a)*math.factorial(b)%(10**9 + 7)))
else:
    print((math.factorial(a)*math.factorial(b)*2%(10**9 + 7)))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
def f(n):
    c=1
    for i in range(1,n+1):
        c*=i
        if c>=(10**9+7):
            c=c%(10**9+7)
    return c
if abs(n-m)==1:
    s=f(n)*f(m)%(10**9+7)
    print(s)
elif n==m:
    s=f(n)*f(m)*2%(10**9+7)
    print(s)
else:
    print(0)
#print(f(100))

Submission status: Accepted

Solution code:
# C - Reconciled?

import math

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

b = 10**9 + 7

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import exit

divisor = 1000000007

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

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

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

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

Submission status: Accepted

Solution code:
def input():
    import sys
    return sys.stdin.readline().rstrip()


def factorial(n, DIVISOR):
    import functools
    return functools.reduce(lambda x, y: (x % DIVISOR) * y, range(1, n + 1)) % DIVISOR


def main():
    import math
    import collections
    import itertools
    import functools

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import math

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    t = 1
    for i in range(2, n + 1):
        t = (t * i) % (10 ** 9 + 7)
    for i in range(2, m + 1):
        t = (t * i) % (10 ** 9 + 7)
    if abs(n - m) == 1:
        print(t)
    elif abs(n - m) == 0:
        t = (t * 2) % (10 ** 9 + 7)
        print(t)
    else:
        print(0)


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
from math import factorial

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

Submission status: Accepted

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

Submission status: Accepted

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

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

if n - m > 1:
    print(0)
    exit()
elif n - m == 1:
    rev = 1
else:
    rev = 2

mod = 10**9 + 7
c_n = 1
c_m = 1
t = n
while t > 1:
    c_n *= t
    c_n %=mod

    if t > m:
        t -= 1
        continue

    c_m *= t
    c_m %= mod
    t -= 1 

ans = c_n * c_m * rev % mod
print(ans)

Submission status: Accepted

Solution code:
# C - Reconciled?
N, M = map(int, input().split())
ans_N = 1
ans_M = 1
mod = 10**9+7

if abs(N - M) >= 2:
    print(0)
else:
    for i in range(N):
        ans_N = ans_N * ((M - i) * (N - i)) % mod
    for j in range(M):
        ans_M = ans_M * ((M - j) * (N - j)) % mod
    print((ans_N+ans_M)% mod)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
def p(n):
    if n==1:
        return 1
    return n*p(n-1)%(10**9+7)
n,m=map(int,input().split())
if n==m:
    print(p(n)**2*2%(10**9+7))
elif abs(n-m)==1:
    print(p(n)*p(m)%(10**9+7))
else:
    print(0)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

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

base = int(1e9 + 7)
ans = 0
if abs(N -M) > 1:
  pass
else:
  fact = 1
  for i in range(1, min(N+1, M+1)):
    fact = (fact * i) % (base)
  
  if N == M:
    ans = 2 * fact * fact
  else:
    ans = fact * fact * max(N, M)
    
ans = ans % base
print(ans)

Submission status: Accepted

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

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

tmpN = math.factorial(N)
tmpM = math.factorial(M)
ans = tmpN * tmpM
if N == M:
    ans *= 2
print(ans % INF)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def factorial(num):
  res = 1
  for i in range(1, num+1):
    res = res * i % (10**9+7)
  return res
a,b = map(int, input().split())
if(a - b == 0):
  print(factorial(a) * factorial(b) * 2 % (10**9+7))
elif(abs(a - b) == 1):
  print(factorial(a) * factorial(b) % (10**9+7))
else:
  print(0)

Submission status: Accepted

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


n, m = map(int, input().split())
n, m = min(n, m), max(n, m)
minusNM = abs(n-m)
ans = 0
if minusNM == 0:
    ans = 2 * prod(n) ** 2
elif minusNM == 1:
    ans = prod(n) ** 2 * m
print(ans % (10 ** 9 + 7))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
MOD=10**9+7
arr=[1]
for i in range(1,N+M+1):
    arr.append((arr[-1]*i)%MOD)
s=arr[N]*arr[M]
if N==M:
    print(2*s%MOD)
elif abs(N-M)==1:
    print(s%MOD)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
mod = 10**9 + 7
if abs(n - m) <= 1:
    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 abs(n-m) == 1:
        print(ans)
    else:
        print(ans * 2 % mod)
else:
    print(0)

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(F(N) * F(M) % MOD * 2 % MOD)
elif N + 1 == M or M + 1 == N:
  print(F(N) * F(M) % MOD)
else:
  print(0)

Submission status: Accepted

Solution code:
def main():
    mod = 10**9+7
    n,m=map(int,input().split(' '))
    ans = 1
    if abs(n-m)>1:
        ans = 0
        print(ans)
        exit()
    if n==m:
        ans *= 2
    for i in range(1,n+1):
        ans *= i
        ans %=mod
    for j in range(1,m+1):
        ans *= j
        ans %= mod
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

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


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


f = for_factorial(N)*for_factorial(M) % mod
if N == M:
    ans = f*2 % mod
elif abs(N - M) == 1:
    ans = f % mod
else:
    ans = 0

print(ans)

Submission status: Accepted

Solution code:
import math

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

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

Submission status: Accepted

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

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

li = sorted([n,m])

num = 1
for i in range(1,li[0]+1):
    num = (num * i) % mod
    
if li[1] - li[0] == 1:
    print((((num * li[1]) % mod) * num) % mod)
else:
    print((((num * num) % mod) * 2) % mod)

Submission status: Accepted

Solution code:
from scipy.misc import comb
import math
mod = 10**9 + 7

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

Submission status: Accepted

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

Submission status: Accepted

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


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


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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

if abs(N-M)>=2:
    print(0)
else:
    mod = 10**9 + 7
    if abs(N-M)==1:
        w = 1
    else:
        w = 2
    nf = 1
    mf = 1
    
    while N > 1:
        nf *= N
        nf %= mod
        N -= 1

    while M > 1:
        mf *= M
        mf %= mod
        M -= 1
    
    ans = nf * mf % mod
    ans = ans * w % mod

    print(ans)

Submission status: Accepted

Solution code:
import math
N, M = tuple(map(int, input().split()))
if N == M:
    print(2 * math.factorial(N) * math.factorial(M) % (10**9 + 7))
    exit()

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

Submission status: Accepted

Solution code:
import math
fact=math.factorial
mod=10**9+7


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

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

if N==M+1 or N==M-1:
    ans=(math.factorial(N)*math.factorial(M))%mod
elif N==M:
    ans=((math.factorial(N)**2)*2)%mod


print(ans)

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

Submission status: Accepted

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

sn,sm = 1,1
for i in range(1,n+1):
    sn = sn*i%mod
for i in range(1,m+1):
    sm = sm*i%mod

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

Submission status: Accepted

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

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

mod = 10 ** 9 + 7


def factorial(x):
    ret = 1
    for i in range(1, x + 1):
        ret *= i
        ret %= mod

    return ret


def solve(m, n):
    if m > n:
        m, n = n, m
    if n - m > 1:
        return 0
    fct = factorial(m)
    if n - m == 1:
        return fct ** 2 * n % mod
    else:  # n - m == 0
        return fct ** 2 * 2 % mod


ans = solve(m, n)
print(ans)

Submission status: Accepted

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

from math import factorial

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


def solve(m, n):
    if m > n:
        m, n = n, m
    if n - m > 1:
        return 0
    fct = factorial(m)
    if n - m == 1:
        return fct ** 2 * n
    else:  # n - m == 0
        return fct ** 2 * 2


mod = 10 ** 9 + 7
ans = solve(m, n) % mod
print(ans)

Submission status: Accepted

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


a=math.factorial(N)*math.factorial(M)
if abs(N-M)>1:
    print(0)
elif N>M or M>N:
    print(int(a%(1000000000+7)))
elif M==N:
    print(int(a*2%(1000000000+7)))

Submission status: Accepted

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


a=math.factorial(N)*math.factorial(M)
if abs(N-M)>1:
    print(0)
elif abs(N-M)==1:
    print(int(a%(1000000000+7)))
elif M==N:
    print(int(a*2%(1000000000+7)))

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

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

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

Submission status: Accepted

Solution code:
from sys import stdin
import math
N,M = [int(x) for x in stdin.readline().rstrip().split()]
mod = 10**9+7
nm = abs(N-M)
if nm == 0:
    ans = math.factorial(N)**2*2
    ans %= mod
    print(ans)
elif nm == 1:
    ans = math.factorial(N)*math.factorial(M)
    ans %= mod
    print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
#include <bits/stdc++.h>
n,m=list(map(int,input().strip().split()))
if n-m>1 or n-m<-1:
    print(0)
else:
    ans=1
    for i in range(1,n+1,1):
        ans=ans*i%1000000007
    for i in range(1,m+1,1):
        ans=ans*i%1000000007
    if n==m:
        print(ans*2%1000000007)
    else:
        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) * 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:
from math import factorial
N,M = list(map(int,input().split()))

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    from math import factorial
    n,m=map(int,input().split())
    if abs(n-m)>1:
        print(0)
    elif abs(n-m)==1:
        print(factorial(n)%MOD*factorial(m)%MOD)
    else:
        print(2*factorial(n)%MOD*factorial(m)%MOD)
resolve()

Submission status: Accepted

Solution code:
import math


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


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
import math as m

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

if abs(N-M)>=2:
  print(0)
else:
  N,M=max(N,M),min(N,M)
  
  Pm=1
  for i in range(1,M+1):
    Pm*=i
    Pm%=10**9+7

  Pn=1
  for i in range(1,N+1):
    Pn*=i
    Pn%=10**9+7

    
  print((Pm*Pn*1)%(10**9+7) if M!=N else (Pm*Pn*2)%(10**9+7))

Submission status: Accepted

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

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

mod = 10**9+7
if big > small+1:
    print(0)
else:
    ans = 1
    for i in range(1,n+1):
        ans *= i
        ans %= mod
    for i in range(1,m+1):
        ans *= i
        ans %= mod
    if n== m:
        ans *= 2
    print(ans%mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import factorial
n,m=map(int,input().split())
if n>m+1 or n+1<m:
  print(0)
  exit()

if n == m:
  print(2*((factorial(n)%1000000007)*(factorial(m)%1000000007))%1000000007)
if n != m:
  print(((factorial(n)%1000000007)*(factorial(m)%1000000007))%1000000007)