import sys # sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = 10**16 # md = 998244353 md = 10**9+7 # sum((a*i+b)//m for i in range(n))γŒζ±‚γΎγ‚‹ # a,b>=0 m>=1 def floor_sum(n: int, m: int, a: int, b: int) -> int: ans = 0 while 1: if a >= m: ans += (n-1)*n*(a//m)//2 a %= m if b >= m: ans += n*(b//m) b %= m y_max = (a*n+b)//m x_max = y_max*m-b if y_max == 0: return ans ans += (n-(x_max+a-1)//a)*y_max%md ans %= md n, m, a, b = y_max, a, m, (a-x_max%a)%a n = II() m = II() aa = LI() bb = LI() ans = 0 for a in aa: for b in bb: ans += 2*floor_sum(b+1, b, a, 0) ans %= md print(ans)