Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

N,Ma,Mb=map(int,input().split())

dp = [[10**18 for _ in range(401)] for _ in range(401)]

dp[0][0]=0
for i in range(N):
    a,b,c=map(int,input().split())
    for ca in range(361, -1, -1):
        for cb in range(361, -1, -1):
            dp[ca+a][cb+b] = min(dp[ca+a][cb+b],dp[ca][cb]+c)

ans = 10**18
for ca in range(401):
    for cb in range(401):
        if not ca*Mb == cb*Ma or ca == 0:
            continue
        if ans > dp[ca][cb]:
            ans = dp[ca][cb]
        
if ans == 10**18:
    print(-1)
else:
    print(ans)


### FIXED CODE ###

N,Ma,Mb=map(int,input().split())

dp = [[10**18 for _ in range(401)] for _ in range(401)]

dp[0][0]=0
for i in range(N):
    a,b,c=map(int,input().split())
    for ca in range(i * 10, -1, -1):
        for cb in range(i * 10, -1, -1):
            dp[ca+a][cb+b] = min(dp[ca+a][cb+b],dp[ca][cb]+c)

ans = 10**18
for ca in range(401):
    for cb in range(401):
        if not ca*Mb == cb*Ma or ca == 0:
            continue
        if ans > dp[ca][cb]:
            ans = dp[ca][cb]
        
if ans == 10**18:
    print(-1)
else:
    print(ans)
### BUGGY CODE ###

n = int(input())
S = [input() for _ in range(n)]

words = list(set(S))
words.sort()

num = len(words)
counter = [0] * num
for i in range(num):
    counter[i] = S.count(words[i])

max = max(counter)
for i in range(num):
    if counter[i] == max:
        print(words[i])

### FIXED CODE ###

n = int(input())

words = []
dict = {}
for i in range(n):
    s = input()
    words.append(s)
    if s not in dict:
        dict[s] = 0
    dict[s] += 1

max = max(dict.values())
for s in sorted([d for d in dict if dict[d] == max]):
    print(s)
### BUGGY CODE ###

def number(m,l):
  count=0
  for i in range(m):
    if min(sorted(l[:i+1]))==l[i]:
      count+=1
  return count

n=int(input())
p=list(map(int,input().split()))
result=number(n,p)
print(result)

### FIXED CODE ###

n=int(input())
p=list(map(int,input().split()))
count=0
min=1000000
for i in range(n):
  if p[i]<=min:
    count+=1
    min=p[i]
  
print(str(count))
### BUGGY CODE ###

N,Q=map(int,(input()).split())
S=input()
l_list=[]
r_list=[]
for i in range(Q):
  l,r=input().split()
  l_list.append(int(l))
  r_list.append(int(r))
for i in range(Q):
  SS=S[int(l_list[i])-1:int(r_list[i])]
  n=0
  while "AC" in SS:
    n+=1
    SS=SS.replace("AC","",1)
  print(n)

### FIXED CODE ###

N,Q=map(int,(input()).split())
S=input()
l_list=[]
r_list=[]
for i in range(Q):
  l,r=input().split()
  l_list.append(int(l))
  r_list.append(int(r))
count=0
AC_count=[0]
for i in range(1,N+2):
  if S[i-1:i+1]=="AC":
    count+=1
  AC_count.append(count)
AC_count.append(count)
k=0
for j in range(Q):
  k=AC_count[r_list[j]-1]-AC_count[l_list[j]-1]
  print(k)
### BUGGY CODE ###

import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。
def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

def hoge(n):
    count = 0
    i = 0
    while count < n:
        if "0" not in str(2019*i):
            count += 1
            print(2019*i)
        i += 1


s = input()
X = list(map(int, list(s)))
n = len(s)


ans = 0
for i in range(n):
    x = 0
    for j in range(i, n):
        x = 10*x + X[j]
        if x%2019 == 0:
            ans += 1
            log(x)

print(ans)


### FIXED CODE ###

import math
from functools import reduce
from collections import deque,defaultdict
import sys
sys.setrecursionlimit(10**7)

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

s = input()
X = list(map(int, list(s)))
n = len(s)

ruiseki = [0] * (n+1)
for i in (range(n-1, -1, -1)):
    ruiseki[i] = (X[i] * pow(10, (n-i-1), 2019) + ruiseki[i+1]) % 2019

# log(ruiseki)

ans = 0
count = defaultdict(int)

for x in ruiseki:
    # log(count[x])
    ans += count[x]
    count[x] += 1
# log(count)
print(ans)
### BUGGY CODE ###

a,b = map(int,input().split())
li = []
li2 = [0]*(len(list(bin(b))[2:]))

for i in range(a,b+1):
    li.append(list(bin(i))[2:][::-1])
for i in range(len(li)):
    for j in range(len(li[i])):
        li2[j] += int(li[i][j])

li3 = []
for i in range(len(li2)):
    if li2[i]%2 == 1:
        li3.append("1")
    else:
        li3.append("0")
li3 = ("".join(li3))[::-1]

print(int(li3,2))

### FIXED CODE ###

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

mid_xor = 0
if a%2 == 0 and b%2 == 1:
    even_cnt = (b-a+1)//2
    if even_cnt%2 == 0:
        mid_xor = 0
    else:
        mid_xor = 1
    print(mid_xor)

elif a%2 == 1 and b%2 == 0:
    even_cnt = (b-1-(a+1)+1)//2
    if even_cnt%2 == 0:
        mid_xor = 0
    else:
        mid_xor = 1
    print(a^mid_xor^b)

elif a%2 == 0 and b%2 == 0:
    even_cnt = (b-a)//2
    if even_cnt%2 == 0:
        mid_xor = 0
    else:
        mid_xor = 1
    print(mid_xor^b)
    
elif a%2 == 1 and b%2 == 1:
    even_cnt = (b-a)//2
    if even_cnt%2 == 0:
        mid_xor = 0
    else:
        mid_xor = 1
    print(a^mid_xor)
### BUGGY CODE ###

import bisect
n,q=map(int,input().split())
s = input()
a=[]
for i in range(n-1):
  if s[i:i+2]=='AC':
    a.append(i)
for _ in range(q):
  b=list(a)
  l,r=map(int,input().split())
  l-=1.5
  r-=1.5
  bisect.insort(b,l)
  bisect.insort(b,r)
  print(b.index(r)-b.index(l)-1)

### FIXED CODE ###

import bisect
n,q=map(int,input().split())
s=input()
a=[]
for i in range(n-1):
  if s[i:i+2]=='AC':
    a.append(i)
for _ in range(q):
  l,r=map(int,input().split())
  print(bisect.bisect(a,r-1.5)-bisect.bisect(a,l-1.5))
### BUGGY CODE ###

# coding: utf-8
from collections import deque
def main():
    s = deque(input())
    q = int(input())
    top = True
    for i in range(q):
        query = list(map(str, input().split()))
        if query[0] == "1":
            top = not top
        else:
            if (query[1]=="1" and top==True) or (query[1]=="2" and top==False):
                s.appendleft(query[2])
            else:
                s.append(query[2])
    if top == False:
        for i in range(len(s)):
            print(s[len(s)-1-i],end="")
    else:
        for c in s:
            print(c,end="")
            
if __name__ == '__main__':
    main()

### FIXED CODE ###

# coding: utf-8
from collections import deque
def main():
    s = deque(input())
    q = int(input())
    top = True
    for i in range(q):
        query = list(map(str, input().split()))
        if query[0] == "1":
            top = not top
        else:
            if (query[1]=="1" and top==True) or (query[1]=="2" and top==False):
                s.appendleft(query[2])
            else:
                s.append(query[2])
    if top == False:
        for i in range(len(s)):
            print(s[len(s)-1-i],end="")
    else:
        for c in s:
            print(c,end="")
            
if __name__ == '__main__':
    main()
### BUGGY CODE ###

import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=998244353
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list -- START --
def summarize_list(l):
  sl=sorted(l)

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append([a,c])
      a=x
      c=1
  res.append([a,c])

  return res
# Summarize count of factor within list --- END ---

# nCr -- START --
def nCr(n,r):
  if n<r:
    return 0
  return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))
# nCr --- END ---

def main():
  n,p=LI()
  s=S()

  ans=0
  if p==2:
    for i,x in enumerate(s):
      if int(x)%2==0:
        ans+=i+1

  elif p==5:
    for i,x in enumerate(s):
      if int(x)%5==0:
        ans+=i+1

  else:
    a=[]
    for i in range(n)[::-1]:
      _i=n-1-i
      b=(10**_i*int(s[i]))%p
      a.append(b)

    # print(a)
    for i in range(1,n)[::-1]:
      a[i-1]+=a[i]
      a[i-1]%=p
    # print(a)
    
    a=summarize_list(a)
    # print(a)

    for x,y in a:
      if x%p==0:
        ans+=y
      ans+=nCr(y,2)

  return ans

# main()
print(main())


### FIXED CODE ###

import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=998244353
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,p=LI()
  s=S()

  ans=0
  if p==2 or p==5:
    for i,x in enumerate(s):
      if int(x)%p==0:
        ans+=i+1
    return ans

  _s=list(s)
  _s.reverse()

  a=[]
  i=1
  for x in _s:
    b=(i*int(x))%p
    a.append(b)
    i*=10
    i%=p

  b=[0]*p
  b[a[0]%p]=1
  for i in range(n-1):
    a[i+1]+=a[i]
    a[i+1]%=p
    b[a[i+1]]+=1

  for i,x in enumerate(b):
    if i==0:
      ans+=x
    if x>1:
      ans+=x*(x-1)//2

  return ans

# main()
print(main())
### BUGGY CODE ###

H,W=map(int,input().split())
S=[]
for i in range(H):
    s=input()
    S.append(s)
 
Gh=[[0]*W for i in range(H)]
for i in range(H):
    tmp=0
    s=0
    j=0
    while(j!=W):
        if S[i][j]=="#":
            Gh[i][j]==0
            for k in range(s,j):
                Gh[i][k]=tmp
            s=j+1
            j+=1
            tmp=0
        else:
            tmp+=1
            j+=1
    if s!=j:
        for k in range(s,j):
            Gh[i][k]=tmp
Gv=[[0]*W for i in range(H)]
for j in range(W):
    tmp=0
    s=0
    i=0
    while(i!=H):
        if S[i][j]=="#":
            Gv[i][j]==0
            for k in range(s,i):
                Gv[k][j]=tmp
            s=i+1
            i+=1
            tmp=0
        else:
            tmp+=1
            i+=1
    if s!=i:
        for k in range(s,i):
            Gv[k][j]=tmp
ans=0
for i in range(H):
    for j in range(W):
        ans=max(ans,Gh[i][j]+Gv[i][j]-1)
print(ans)

### FIXED CODE ###

H,W=map(int,input().split())
S=[]
for i in range(H):
    s=input()
    S.append(s)

Gh=[[0]*W for i in range(H)]
for i in range(H):
    tmp=0
    s=0
    j=0
    while(j!=W):
        if S[i][j]=="#":
            Gh[i][j]==0
            for k in range(s,j):
                Gh[i][k]=tmp
            s=j+1
            j+=1
            tmp=0
        else:
            tmp+=1
            j+=1
    if s!=j:
        for k in range(s,j):
            Gh[i][k]=tmp
Gv=[[0]*W for i in range(H)]
for j in range(W):
    tmp=0
    s=0
    i=0
    while(i!=H):
        if S[i][j]=="#":
            Gv[i][j]==0
            for k in range(s,i):
                Gv[k][j]=tmp
            s=i+1
            i+=1
            tmp=0
        else:
            tmp+=1
            i+=1
    if s!=i:
        for k in range(s,i):
            Gv[k][j]=tmp
ans=0
for i in range(H):
    for j in range(W):
        ans=max(ans,Gh[i][j]+Gv[i][j]-1)
print(ans)
### BUGGY CODE ###

from collections import defaultdict
n, q = map(int, input().split())
child_dict = defaultdict(list)
value_dict = defaultdict(lambda: 0)
for _ in range(n - 1):
    a, b = map(int, input().split())
    child_dict[a].append(b)
import copy
for _ in range(q):
    c, d = map(int, input().split())
    value_dict[c] += d
    child = child_dict[c]
    while len(child) > 0:
        k = []
        for i in child:
            value_dict[i] += d
            k.extend(child_dict[i])
        child = copy.deepcopy(k)
ans = [value_dict[i] for i in range(1 ,n+1)]
print(*ans)

### FIXED CODE ###

n, q = map(int, input().split())
graph = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)
point_list = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    p -= 1
    point_list[p] += x

from collections import deque
d = deque()
d.append(0)
visited = set([])

while d:
    x = d.popleft()
    for child in graph[x]:
        if child not in visited:
            point_list[child] += point_list[x]
            d.append(child)
        visited.add(x)
print(*point_list)
### BUGGY CODE ###

N, K = map(int, input().split())
p = list(map(int, input().split()))

ans, ai = 0, 10**9
for i in range(N-K+1):
  sum = 0
  for k in range(K):
    sum += p[i+k]
  ans = max(ans, sum)
  if ans == sum:
    ai = i

dp = [0]*1001
dp[0] = 0
for i in range(1000):
  dp[i+1] = i+1 + dp[i]
  
ans, num = 0, 0
for i in range(ai, ai+K):
  ans += dp[p[i]]/p[i]

print(ans)

### FIXED CODE ###

N, K = map(int, input().split())
p = list(map(int, input().split()))

ans, ai = 0, -1
sum = 0
for i in range(N-K+1):
  if i == 0:
    for k in range(K):
      sum += p[i+k]
  else:
    sum = sum - p[i-1] + p[i+k]
  ans = max(ans, sum)
  if ans == sum:
    ai = i

dp = [0]*1001
dp[0] = 0
for i in range(1000):
  dp[i+1] = i+1 + dp[i]

ans = 0.0
for i in range(ai, ai+K):
  ans += dp[p[i]]/p[i]

print(ans)
### BUGGY CODE ###

def main():
    import sys
    input = sys.stdin.buffer.readline
    N = int(input())
    from collections import defaultdict
    edge = defaultdict(set)
    deg = defaultdict(int)
    for i in range(N):
        A = [int(k)-1 for k in input().split()]
        for j in range(N-2):
            if i < A[j]:
                if i < A[j+1]:
                    edge[(i, A[j])].add((i, A[j+1]))
                    deg[(i, A[j+1])] += 1
                else:
                    edge[(i, A[j])].add((A[j+1], i))
                    deg[(A[j+1], i)] += 1
            else:
                if i < A[j+1]:
                    edge[(A[j], i)].add((i, A[j+1]))
                    deg[(i, A[j+1])] += 1
                else:
                    edge[(A[j], i)].add((A[j+1], i))
                    deg[(A[j+1], i)] += 1

    from collections import deque
    que = deque()
    order = 1
    ans = []
    dist = defaultdict(int)
    for i in range(N):
        for j in range(i+1, N):
            if deg[(i, j)] == 0:
                que.append((i, j))  # root
                ans.append(((i, j), order))
                dist[(i, j)] = 1
                order += 1
    while que:
        u = que.popleft()
        for v in edge[u]:
            deg[v] -= 1
            dist[v] = max(dist[v], dist[u] + 1)
            if deg[v] == 0:
                que.append(v)
                ans.append((v, order))
                order += 1

    if len(ans) != N*(N-1)//2:
        return print(-1)

    print(max(dist.values()))


if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    import sys
    input = sys.stdin.buffer.readline
    N = int(input())
    c = [[int(i) for i in input().split()] for j in range(N)]
    from collections import defaultdict
    edge = defaultdict(set)
    deg = defaultdict(int)
    for i, A in enumerate(c, start=1):
        for j in range(N-2):
            if i < A[j]:
                if i < A[j+1]:
                    edge[i*N+A[j]].add(i*N+A[j+1])
                    deg[i*N+A[j+1]] += 1
                else:
                    edge[i*N+A[j]].add(A[j+1]*N+i)
                    deg[A[j+1]*N+i] += 1
            else:
                if i < A[j+1]:
                    edge[A[j]*N+i].add(i*N+A[j+1])
                    deg[i*N+A[j+1]] += 1
                else:
                    edge[A[j]*N+i].add(A[j+1]*N+i)
                    deg[A[j+1]*N+i] += 1
    from collections import deque
    que = deque()
    dist = defaultdict(int)
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            # print(i, j, i*N+j)
            if deg[i*N+j] == 0:
                que.append(i*N+j)  # root
                dist[i*N+j] = 1
    while que:
        u = que.popleft()
        for v in edge[u]:
            deg[v] -= 1
            dist[v] = max(dist[v], dist[u] + 1)
            if deg[v] == 0:
                que.append(v)

    if any(deg.values()):
        # 閉路検出
        print(-1)
    else:
        print(max(dist.values()))


if __name__ == '__main__':
    main()
### BUGGY CODE ###

# your code goes here
N=int(input())
s=list(input())
a = []

for i in range(1,N):
	b =s[:i:].count('W')
	c=s[i+1::].count('E')
	a.append(b+c)
a.append(s[1::].count('E'))
print(min(a))

### FIXED CODE ###

N = int(input())
S = input()
 
cnt = S[1:].count("E")
ans = cnt
for i in range(1, N):
    if S[i-1] == "W":
        cnt += 1
    if S[i] == "E":
        cnt -= 1
    ans = min(ans, cnt)
 
print(ans)
### BUGGY CODE ###

from bisect import bisect_left
from bisect import bisect_right

n = int(input())
s = list(input())
q = int(input())

ind_list = [[] for _ in range(26)]

for i, x in enumerate(s):
    ind_list[ord(x)-97].append(i)

for _ in range(q):
    _type, val1, val2 = input().split()
    if _type == '1':
        i = int(val1)-1
        c = val2
        if s[i] == c:
            continue
        ind_list[ord(s[i])-97].remove(i)
        ind_list[ord(c)-97].append(i)
        ind_list[ord(c)-97].sort()
        s[i] = c
    elif _type == '2':
        l = int(val1)-1
        r = int(val2)-1
        ans = 0
        for i in range(26):
            if bisect_right(ind_list[i], r) - bisect_left(ind_list[i], l) > 0:
                ans += 1
        print(ans)
    else:
        print('Exception caught!')
        exit()


### FIXED CODE ###

from bisect import bisect_right
from bisect import bisect_left

n = int(input())
s = list(input())
q = int(input())

ind_list = [[] for _ in range(26)]

for i, x in enumerate(s):
    ind_list[ord(x)-97].append(i)

for _ in range(q):
    _type, val1, val2 = input().split()
    if _type == '1':
        i = int(val1)-1
        c = val2
        if s[i] == c:
            continue
        ind_list[ord(s[i])-97].remove(i)
        p = bisect_right(ind_list[ord(c)-97], i)
        ind_list[ord(c)-97].insert(p, i)
        s[i] = c
    elif _type == '2':
        l = int(val1)-1
        r = int(val2)-1
        ans = 0
        for i in range(26):
            if bisect_right(ind_list[i], r) - bisect_left(ind_list[i], l) > 0:
                ans += 1
        print(ans)
    else:
        print('Exception caught!')
        exit()
### BUGGY CODE ###

import math
import itertools

def is_prime(n):
    if n == 1: return 0

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return 0
    return 1

Q = int(input())
L = []
R = []

for _ in range(Q):
    l,r = [int(i) for i in input().split()]
    L.append(l)
    R.append(r)

li = []
for i in range(min(L),max(R)+1,1):
    if i%2 == 1:
        li.append(is_prime(i) * is_prime((i+1)//2))
    else:
        li.append(0)

ans = [0]
ans += list(itertools.accumulate(li))

for i,j in zip(L,R):
    print(ans[j-min(L)+1] - ans[i-min(L)])


### FIXED CODE ###

import math
from itertools import accumulate


def is_prime(n):
    if n == 1:
        return 0

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return 0

    return 1


Q = int(input())
L = [0]*Q
R = [0]*Q

for i in range(Q):
    L[i], R[i] = list(map(int, input().split()))

min_L = min(L)
max_R = max(R)

check = []
for i in range(min_L, max_R+1):
    if (i % 2 == 1):
        flg = (is_prime(i) and is_prime((i+1)/2))
    else:
        flg = 0
    check.append(flg)

# 累積和
check = [0] + check
check = list(accumulate(check))

for i in range(Q):
    print(check[R[i] - min_L + 1] - check[L[i] - min_L])
### BUGGY CODE ###

import math
import collections


nc2_memo = {}
def nc2(n):
    if n <= 1:
        return 0
    if n in nc2_memo:
        return nc2_memo[n]
    r = math.factorial(n)//(2*math.factorial(n-2))
    nc2_memo[n] = r
    return r



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

cnt = collections.Counter(a)
each_ans = {}
for key, value in cnt.items():
    each_ans[key] = (nc2(value), nc2(value-1))
all_sum = sum(value[0] for _, value in each_ans.items())
ans = {}
for key in cnt.keys():
    ans[key] = all_sum - each_ans[key][0] + each_ans[key][1]
for i in a:
    print(ans[i])


### FIXED CODE ###

import math
import collections


def nc2(n):
    if n <= 1:
        return 0
    return math.factorial(n)//(2*math.factorial(n-2))


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

cnt = collections.Counter(a)
each_ans = {}
for key, value in cnt.items():
    t = nc2(value)
    each_ans[key] = (t, t-value+1)
all_sum = sum(value[0] for _, value in each_ans.items())
ans = {}
for key in cnt.keys():
    ans[key] = all_sum - each_ans[key][0] + each_ans[key][1]
for i in a:
    print(ans[i])
### BUGGY CODE ###

from itertools import combinations, product
n=int(input())
s=[["a" for _ in range(n)] for _ in range(n)]
for i in range(n):
  s[i]=list(input())

res=0
for a in range(n):
  for i,j in combinations(range(n),2):
    if(s[(i+a)%n][j%n]!=s[(j+a)%n][i%n]):
      break
  else:
    res+=1

print(res*n)


### FIXED CODE ###

from itertools import combinations, product
n=int(input())
s=[["a" for _ in range(n)] for _ in range(n)]
for i in range(n):
  s[i]=list(input())

res=0
for a in range(n):
  for i,j in combinations(range(n),2):
    if(s[(i+a)%n][j%n]!=s[(j+a)%n][i%n]):
      break
  else:
    res+=1

print(res*n)
### BUGGY CODE ###

x, y = [int(i) for i in input().split()]
xl = [int(i) for i in input().split()]
yl = [int(i) for i in input().split()]


s=0;
#xの左位置
for ix1 in range(x-1):
    #xの右位置
    for ix2 in range(ix1+1,x):
        wide = xl[ix2] - xl[ix1]
        #yの下位置
        for iy1 in range(y-1):
            #xの右位置
            for iy2 in range(iy1+1,y):
                height = yl[iy2] - yl[iy1]
                s += wide * height
                #print(str(wide)+' '+str(height)+' '+str(s)+';  '+str(yl[iy1])+' '++str(iy2))

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


### FIXED CODE ###

### D - 井井井 / ###　やりなおし
x, y = [int(i) for i in input().split()]
xl = [int(i) for i in input().split()]
yl = [int(i) for i in input().split()]

xx = sum(  [ i*xl[i] - (x-i-1)*xl[i] for i in range(len(xl)) ]  )
yy = sum(  [ i*yl[i] - (y-i-1)*yl[i] for i in range(len(yl)) ]  )
ans = (xx * yy) % (10**9 + 7)
print(ans)
### BUGGY CODE ###

from math import gcd
from sys import setrecursionlimit
setrecursionlimit(10**6)


def gcds(lst):
    if len(lst) == 2:
        return gcd(*lst)
    return gcd(lst[0], gcds(lst[1:]))

n,x = map(int,input().split())
xl = list(map(int,input().split()))
xl = list(map(lambda y: abs(y-x), xl))

ans = -1
if len(xl) == 1:
    ans = xl[0]
else:
    ans = gcds(xl)

print(ans)

### FIXED CODE ###

import math
from functools import reduce
from sys import setrecursionlimit
setrecursionlimit(10**6)

def gcd(*numbers):
  return reduce(math.gcd, numbers)

n,x = map(int,input().split())
xl = list(map(int,input().split()))
xl = list(map(lambda y: abs(y-x), xl))

ans = -1
if len(xl) == 1:
    ans = xl[0]
else:
    ans = gcd(*xl)

print(ans)