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: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
if abs(n-m) >= 2:
  print(0)
  exit()
mod = 10**9+7
def power(a,n):
  ans = 1
  while n:
    if n%2:
      ans = (ans*a)%mod
    a = (a*a)%mod
    n >>= 1
  return ans%mod  
if abs(n-m) == 1:
  print(power(2,m+n))
  exit()
print(power(2,m+n+1))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
lim = int(1e+9+7)


def pat(r,t = lim):
    ans = 1
    for i in range(r):
        ans = (ans*(i+1))%t
    return ans%t

a = pat(n)
b = pat(m)

if abs(n-m) < 2:
    print((a*b*2)%lim)
elif abs(n-m) == 2:
    print((a*b)%lim)
else:
    print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
D,M=list(map(int,input().split()))
if abs(D-M)>2:
    print(0)
    exit()
if D==M:
    print((2*(math.factorial(D)**2))%1000000007)
else:
    print((math.factorial(D)*math.factorial(M))%1000000007)

Submission status: Wrong Answer

Solution code:
import sys
from functools import reduce,lru_cache
import math
from operator import mul

def main():
  #sys.setrecursionlimit(10 ** 5 + 1)
  N,M = map(int,input().split())
  
  @lru_cache(maxsize = None)
  def recur(n):
    if n == 0:
      return 1
    else:
      m = n * recur(n - 1)
      return m
      
  if N == M:
    L = [i for i in range(1,N + 1)]
    #ans = (reduce(mul,L) ** 2) * 2  % (10 ** 9 + 7)
    ans = (math.factorial(M) ** 2) * 2 % ((10 ** 9) + 7)
    print(ans)
  elif abs(M - N) == 1: 
    ans = 1
    ans *= math.factorial(N) % ((10 ** 9) + 7)
    ans *= math.factorial(M) % ((10 ** 9) + 7)
    print(ans)
    
  else:
    print(0)
    
main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
print(-1)

Submission status: Wrong Answer

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

#n=int(input())

tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))

n=hoge[0]
m=hoge[1]

#total=1
#if(n==m):
#	total+=1

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

Submission status: Wrong Answer

Solution code:
# ARC076C - Reconciled?
from math import factorial


def main():
    n, m = list(map(int, input().rstrip().split()))
    MOD = 10 ** 9 + 7
    if abs(n - m) not in (0, 1):
        print(0)
    else:
        ans = (factorial(n) * factorial(m) * 2) % MOD
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

if N==M:
  ans = 1
  for i in range(1,N+1):
    ans *= i
    ans %= 10**9 + 7
  ans *= ans
  ans %= 10**9 + 7
  ans *= 2
  ans %= 10**9 + 7
elif N==M+1 or N+1==M:
  ans = 1
  for i in range(1,N+1):
    ans *= i
    ans %= 10**9 + 7
  for i in range(1,M+1):
    ans *= i
    ans %= 10**9 * 7
else:
  ans = 0
print(ans)

Submission status: Wrong Answer

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


ans=1 if(abs(N-M)<2) else 0
n=max(N,M)

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

if(N==M):
    ans=(ans*ans*2)%(10**9+7)
else:
    ans=((ans//n)*ans)%(10**9+7)

print(ans)

Submission status: Wrong Answer

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


ans=1 if(abs(N-M)<2) else 0
n=max(N,M)

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

if(N==M):
    ans=(ans*ans*2)%(10**9+7)
else:
    ans=((ans*ans)//n)%(10**9+7)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#ARC076
import math

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

Submission status: Wrong Answer

Solution code:
def reconciled():
	n, m = map(int, raw_input().split(" "))
	if abs(n-m)>1:
		print 0
		return
	mod = 1000000007
	mod_n = 1
	mod_m = 1
	for i in reversed(range(1, n+1)):
		mod_n = (mod_n*i)%mod
	for j in reversed(range(1, m+1)):
		mod_m = (mod_m*j)%mod
	if n!=m:
		print (mod_n*mod_n)%mod
	else:
		print (2*mod_n * mod_n) % mod


if __name__ == "__main__":
	reconciled()

Submission status: Wrong Answer

Solution code:
def reconciled():
	n, m = map(int, raw_input().split(" "))
	if abs(n-m)>1:
		print str(0)
		return
	mod = 1000000007
	mod_n = 1
	mod_m = 1
	for i in reversed(range(1, n+1)):
		mod_n = (mod_n*i)%mod
	for j in reversed(range(1, m+1)):
		mod_m = (mod_m*j)%mod
	if n!=m:
		print (mod_n*mod_n)%mod
	else:
		print (2*mod_n * mod_n) % mod


if __name__ == "__main__":
	reconciled()

Submission status: Wrong Answer

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

mod = 10 ** 9 + 7
if N < M:
    N, M = M, N

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

Submission status: Wrong Answer

Solution code:
def junretsu(num):
    ret = 1
    for i in range(1, num+1):
        ret *= i
    return ret

a, b = input().split(' ')
a, b = int(a), int(b)
if abs(a-b) > 1:
    ret = 0
elif abs(a-b) == 1:
    ret = junretsu(a) * junretsu(b)
else:
    ret = junretsu(a) * junretsu(b) / 2
print(ret%(pow(10, 9)+7))

Submission status: Wrong Answer

Solution code:
import sys
import math
n, m = (int(i) for i in input().split())
l=abs(n-m)
div=int(10e9+7)

if l>1:
    print(0)
    sys.exit()

if l==1:
    ans=(math.factorial(n)*math.factorial(m))%div
    print(ans)
else:
    ans=(math.factorial(n)*math.factorial(m)*2)%div
    print(ans)

Submission status: Wrong Answer

Solution code:
#encoding: utf-8

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

    ret=1
    mod=10**9+7
    for i in range(1,N+1):
        ret*=i
        ret%=mod
    for i in range(1,M+1):
        ret*=i
        ret %= mod
    if(N==M):
        r=2
    else:
        r=1
    print(ret*r)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#encoding: utf-8

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

    ret=1
    mod=10**9+7
    for i in range(1,N+1):
        ret*=i
        ret%=mod
    for i in range(1,M+1):
        ret*=i
        ret %= mod
    print(ret*2)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#encoding: utf-8

def main():
    N,M=map(int,input().split())
    if(abs(N-M)<=2):
        return 0
    ret=1
    for i in range(1,N+1):
        ret*=i
    for i in range(1,M+1):
        ret*=i
    return ret


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

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

print(math.factorial(n) % 1000000007 * math.factorial(m) % 1000000007 * k % 1000000007)

Submission status: Wrong Answer

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

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

ans = 0

ans1 = 1
n1 = n
m1 = m
while n1 >= 1 or m1 >= 0:
    ans1 *= n1 * m1
    ans1 %= d
    n1 -= 1
    m1 -= 1


ans2 = 1
n2 = n
m2 = m
while m2 >= 1 or n2 >= 0:
    ans2 *= m2 * n2
    ans2 %= d
    n2 -= 1
    m2 -= 1


print((ans1 + ans2) % d)

Submission status: Wrong Answer

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

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

ans = 0

ans1 = 1
n1 = n
m1 = m
while n1 >= 1:
    ans1 *= n1 * m1
    ans1 %= d
    n1 -= 1
    m1 -= 1


ans2 = 1
n2 = n
m2 = m
while m2 >= 1:
    ans2 *= m2 * n2
    ans2 %= d
    n2 -= 1
    m2 -= 1


print((ans1 + ans2) % d)

Submission status: Wrong Answer

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

MOD = 10**9+7

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

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

print(cnt)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# coding: UTF-8

import itertools
import math

dd = map(int, raw_input().split())

dog = dd[0]
mon = dd[1]

n = dog + mon

list_d = ["0"] * dog
list_m = ["1"] * mon

list_d.extend(list_m)

p = list(itertools.permutations(list_d))

ans = 0
length = len(p)

print length

for i in range(length):
    for j in range(len(p[i]) - 1):
        if p[i][j] == p[i][j + 1]:
            break
        if j == len(p[i]) - 2:
            ans += 1

ans %= (10 ** 9) + 7

print ans

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python3
import math
N, M = map(int, input().split())
n = math.factorial(N)
m = math.factorial(M)
if N == M:
    print(n  * m * 2 % (10**9+7))
elif abs(N - M) > 2:
    print(0)
else:
    print(n * m % (10**9+7))

Submission status: Wrong Answer

Solution code:
import numpy as np
p = 10**9+7
def fact(n):
    sum = 1
    for i in range(n):
        sum = sum*(i+1)%p
    return sum

N,M = [int(_) for _ in input().split()]
sum = 1
if N==M:
    print ((2*fact(N)**2)%p)
elif N-M==1:
    print ((fact(N)**2/N)%p)
elif M-N==1:
    print ((M*fact(N)**2)%p)
else:
    print (0)

Submission status: Wrong Answer

Solution code:
import math
import sys

a = sys.stdin.readline().split(' ')
n = int(a[0])
m = int(a[1])

modulator = 1000000007

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

Submission status: Wrong Answer

Solution code:
import numpy as np
from itertools import permutations as perm
import sys
sys.setrecursionlimit(1000000)

def fact(n):
    if n==0:
        return 1
    elif n>0:
        return n*fact(n-1)

N,M = [int(_) for _ in input().split()]
sum = 1
for i in range(N):
    sum *= (i+1)

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

Submission status: Wrong Answer

Solution code:
import itertools
N, M = input().split(' ',2)
N = int(N)
M = int(M)
S = N - M
if abs(S) >= 2:
    print(0)
if S == 0:
    print(len(list(itertools.permutations(range(N)))) * len(list(itertools.permutations(range(M)))) * 2)
else:
    print(len(list(itertools.permutations(range(N)))) * len(list(itertools.permutations(range(M)))))

Submission status: Wrong Answer

Solution code:
# C
N, M = map(int, input().split())
L = 1000000007

res = 1

if (M - N) >= 2:
    res = 0
else:
    for k in range(M):
        res *= (k+1)
        res = res % L
    for k in range(N):
        res *= (k+1)
        res = res % L
    if M == N:
        res *= 2
        res = res % L
if M == 0 or N == 0:
    res = 0
print(res)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import array, bisect, collections, copy, heapq, itertools, math, random, re, string, sys, time
sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7


def II(): return int(input())
def ILI(): return list(map(int, input().split()))
def IAI(LINE): return [ILI() for __ in range(LINE)]
def IDI(): return {key: value for key, value in ILI()}


def read():
    N, M = ILI()
    return N, M


def solve(N, M):
    if abs(N - M) > 2:
        return 0
    elif abs(N - M) == 1:
        num_min = min(N, M)
        ans = ((math.factorial(num_min) ** 2) * (num_min + 1)) % MOD
        return ans
    elif abs(N - M) == 0:
        ans = (2 * (math.factorial(N) ** 2)) % MOD
        return ans


def main():
    params = read()
    print(solve(*params))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# C
N, M = map(int, input().split())
L = 1000000007

res = 1

if (M - N) >= 2:
    res = 0
else:
    for k in range(M):
        res *= (k+1)
        res = res % L
    for k in range(N):
        res *= (k+1)
        res = res % L
    if M == N:
        res *= 2
        res = res % L
        
print(res)

Submission status: Time Limit Exceeded

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from operator import mul

def main():
  N,M = map(int,input().split())
  if N == M:
    L = [i for i in range(1,N + 1)]
    ans = (reduce(mul,L) ** 2) * 2  % (10 ** 9 + 7)
    print(ans)
  elif N + 1 == M or M + 1 == N: 
    L1 = [i for i in range(1,N + 1)]
    L2 = [i for i in range(1,M + 1)]
    ans = (reduce(mul,L1)) * (reduce(mul,L2)) % (10 ** 9 + 7)
    print(ans)
  else:
    print(0)
main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import math

n,m=map(int,input().split())
n_comb=math.factorial(n)
m_comb=math.factorial(m)

if abs(n-m)>=2:
  print(0)
  sys.exit()
elif n==m:
  print(n_comb*m_comb*2)
  sys.exit()
else:
  print(n_comb*m_comb)

Submission status: Time Limit Exceeded

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

#n=int(input())

tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))

n=hoge[0]
m=hoge[1]

total=1
if(n==m):
	total+=1

if(abs(n-m)>1):
	print(0)
else:
	print(total*math.factorial(n)*math.factorial(m))

Submission status: Time Limit Exceeded

Solution code:
# ARC076C - Reconciled?
def factorial(x):
    ret = 1
    for i in range(1, x + 1):
        ret *= i
    return ret


def main():
    n, m = list(map(int, input().rstrip().split()))
    MOD = 10 ** 9 + 7
    dif = abs(n - m)
    if dif > 1:  # must be ...DDM...
        print(0)
    else:
        if dif == 0:  # start with either D or M (DM..., MD...)
            ans = (factorial(n) * factorial(m) * 2) % MOD
        else:  # sandwich (DMD or MDM)
            ans = (factorial(n) * factorial(m)) % MOD
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
MOD = 10 ** 9 + 7

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

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

Submission status: Time Limit Exceeded

Solution code:
#!usr/bim/python
#! -*- coding: utf-8 -*-

nm = raw_input()
nm = nm.split()
nm = map(int,nm)

mod = 10 ** 9 + 7

def kai(n):
    ans = 1
    if n<=0:
        pass
    else:
        i = 0
        while i < n:
            ans = ans * (i+1)
            i += 1
    return (ans)

ans = 0
if nm[0] - nm[1] == 0:
    ans = (kai(nm[0])%mod)**2*2
elif nm[0] - nm[1] == 1:
    ans = (kai(nm[1])%mod)**2*nm[0]
elif nm[0] - nm[1] == -1:
    ans = (kai(nm[0])%mod)**2*nm[1]
else:
    pass

ans = ans % mod

print ans

Submission status: Time Limit Exceeded

Solution code:
#!usr/bim/python
#! -*- coding: utf-8 -*-

nm = raw_input()
nm = nm.split()
nm = map(int,nm)

def kai(n):
    ans = 1
    if n<=0:
        pass
    else:
        i = 0
        while i < n:
            ans = ans * (i+1)
            i += 1
    return (ans)

ans = 0
if abs(nm[0]-nm[1]) == 0:
    ans = kai(nm[0])*kai(nm[1])*2
elif abs(nm[0]-nm[1]) == 1:
    ans = kai(nm[0])*kai(nm[1])
else:
    pass

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

print ans

Submission status: Time Limit Exceeded

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

res=1
if M==N:
    for i in range(1, M+1):
        res*=i
    res=res*res*2
elif M-N==1:
    for i in range(1, N+1):
        res*=i
    res*=res*M
elif M-N==-1:
    for i in range(1, M+1):
        res*=i
    res*=res*N
else:
    res=0

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

Submission status: Time Limit Exceeded

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

res=1
if M==N:
    for i in range(1, M+1):
        res*=i
    res=res*res*2
elif abs(M-N)==1:
    for i in range(1, M+1):
        res*=i
    for i in range(1, N+1):
        res*=i
else:
    res=0

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

Submission status: Time Limit Exceeded

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

mod=10**9+7

def kaijo(n):
    result=1
    for i in range(2,n+1):
        result*=i
    if result>mod:
        result=result%mod
    return result
    
def count(n,m):
    return kaijo(n)*kaijo(m)

if n-m==-1 or n-m==1:
    result=count(n,m)
    amari=result%mod
    print amari
elif n-m==0:
    result=count(n,m)
    result*=2
    amari=result%mod
    print amari
else:
    print 0

Submission status: Time Limit Exceeded

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

def kaijo(n):
    result=1
    for i in range(2,n+1):
        result*=i
    return result
    
def count(n,m):
    return kaijo(n)*kaijo(m)

if n-m==-1 or n-m==1:
    result=count(n,m)
    amari=result%(10**9+7)
    print amari
elif n-m==0:
    result=count(n,m)
    result*=2
    amari=result%(10**9+7)
    print amari
else:
    print 0

Submission status: Time Limit Exceeded

Solution code:
dog_ape = list(map(int,input().split(" ")))
dog_ape.sort()
cut     = (10**9)+7

def kai(n):
    ans = 1
    r   = 0
    for i in range(n):
        ans *= (i+1)**2
    return ans

if dog_ape[0] == dog_ape[1]:
    ans = ( kai(dog_ape[0]) )*2
    ans %= cut
    print(ans)

elif dog_ape[1] - dog_ape[0] == 1:
    ans = kai(dog_ape[0])*(dog_ape[1])
    ans %= cut
    print(ans)

else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
import math
MOD = pow(10, 9)+7
N, M = map(int, raw_input().split())

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

Submission status: Time Limit Exceeded

Solution code:
from math import factorial

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

Submission status: Time Limit Exceeded

Solution code:
from math import factorial

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

if N == M+1 or N+1 == M:
	print(factorial(N)*factorial(M))

elif N==M:
	print(2*factorial(N)*factorial(M))
	
else:
	print(0)

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
import sys
mod=10**9+7 ; inf=float("inf")
from math import sqrt, ceil
from collections import deque, Counter, defaultdict #すべてのkeyが用意されてる defaultdict(int)で初期化
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from decimal import ROUND_HALF_UP,Decimal as D  #変換後の末尾桁を0や0.01で指定
  #Decimal((str(0.5)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
from functools import lru_cache
from bisect import bisect_left as bileft, bisect_right as biright
from fractions import Fraction as F  #F(a,b)で正確なa/b
#メモ化再帰defの冒頭に毎回 @lru_cache(maxsize=10**10)
#引数にlistはだめ
#######ここまでテンプレ#######
#ソート、"a"+"b"、再帰ならPython3の方がいい
#######ここから天ぷら########

a,b=map(int,input().split())
if abs(a-b)>=2:
    print(0)
    exit()
mod=10**9+7
re=2 if a==b else 1
def kaijou(n):
    if n==1:return 1
    else:
        return (kaijou(n-1)*n)%mod


print(kaijou(a)*kaijou(b)*re%mod)

Submission status: Accepted

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

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

if 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:
import math
N,M = map(int,input().split())

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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)
else:
    print(math.factorial(N)*math.factorial(M)*2%MOD)

Submission status: Accepted

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

MOD = 10**9 + 7

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
SIZE=2*10**5; MOD=10**9+7 #998244353 #ここを変更する

SIZE += 1
fac = [0]*SIZE  # fac[j] = j! mod MOD
fac[0] = fac[1] = 1
for i in range(2,SIZE):
    fac[i] = fac[i-1]*i%MOD

# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

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

if abs(n-m) >= 2:
    print(0)
elif abs(n-m)==1:
    print(fac[n]*fac[m]%MOD)
elif n==m:
    print(2*fac[n]*fac[m]%MOD)

Submission status: Accepted

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

fact = [1]
for i in range(1, max(n, m) + 1):
    fact.append(fact[-1] * i % mod)

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
if abs(n-m) >= 2:
  print(0)
  exit()
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((factorial(m)*factorial(n))%mod)
  exit()
x = factorial(n)
print((2*x*x)%mod)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
lim = int(1e+9+7)


def pat(r,t = lim):
    ans = 1
    for i in range(r):
        ans = (ans*(i+1))%t
    return ans%t

a = pat(n)
b = pat(m)

if abs(n-m) < 1:
    print((a*b*2)%lim)
elif abs(n-m) == 1:
    print((a*b)%lim)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n, m = map(int, input().split())
if abs(n - m) > 1:
  print(0)
  quit()
mod = 10 ** 9 + 7


def cumprod(L, mod=mod):
    A = np.arange(L, dtype=np.int64)
    A[0] = 1
    Lsq = int(L**.5 + 1)
    A = np.resize(A, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        A[:, n] *= A[:, n - 1]
        A[:, n] %= mod
    for n in range(1, Lsq):
        A[n] *= A[n - 1, -1]
        A[n] %= mod
    return A.ravel()[:L]


fact = cumprod(2 * 10**6 + 10, mod)
res = fact[n] * fact[m]
if n == m:
  res *= 2
print(res % mod)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

def fac(n):
    f=1
    for i in range(1,n+1):
        f*=i
        f%=INF
    return f

if abs(n-m)>=2:
    print(0)
elif n==m:
    print(2*fac(n)**2%INF)
else:
    print(fac(min(n,m))**2*max(n,m)%INF)

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

def comb(a,p):
    ini=1
    for i in range(1,a+1):
        ini = (ini*i)%p
    return ini


if abs(N-M)>1:
    ans=0
else:
    if N!=M:
        k=1
    else:
        k=2
       
    dog=comb(N,p)
    mon=comb(M,p)
    tot=comb(N+M,p)
   
    ans= (k*dog*mon)%p
   
print(ans)

Submission status: Accepted

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


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
mod=10**9+7
n,m=map(int,input().split())
F=[0]*(10**5+1)
F[0]=1
for i in range(1,10**5+1):
  F[i]=(F[i-1]*i)%mod

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

Submission status: Accepted

Solution code:
###ncrのmodあまりの話
mod=10**9+7
N=100005
kaidan=[1 for i in range(N)]
gyaku=[1 for i in range(N)]
for i in range(1,N):
    kaidan[i] = (i * kaidan[i-1])%mod
    gyaku[i]=pow(kaidan[i], mod-2, mod)
def comb(a,b):
    return kaidan[a]*gyaku[b]*gyaku[a-b]
n,m=map(int,input().split())
if abs(n-m)>=2:
    print(0)
else:
    if n==m:
        print(kaidan[n]*kaidan[n]*2%mod)
    else:
        a=max(n,m)
        b=min(n,m)
        print(kaidan[a]*kaidan[b]%mod)

Submission status: Accepted

Solution code:
from math import factorial
from sys import stdin


def permutations_count(n, r):
    return factorial(n) // factorial(n - r)


def main():
    N, M = [int(x) for x in stdin.readline().rstrip().split()]
    if N == M:
        x = (factorial(N) % (10 ** 9 + 7)) * (factorial(M) % (10 ** 9 + 7))
        print((x * 2) % (10 ** 9 + 7))
    elif abs(N - M) == 1:
        x = (factorial(N) % (10 ** 9 + 7)) * (factorial(M) % (10 ** 9 + 7))
        print(x % (10 ** 9 + 7))
    else:
        print(0)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#犬同士　または　猿同士
#交互のみ
mod = 10**9+7
"""
def chs(k,i,mod):
    deno = 1
    for j in range(k,k-i,-1):
        deno*=j
        deno%=mod
    nume = 1
    for j in range(1,j+1):
        nume*=pow(j,mod-2,mod)
        nume%=mod
    return deno*nume%mod
"""

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