• + 0 comments

    Python3 :-)

    from math import ceil
    
    def gcd(a, b):
        if a > b:
            return gcd(b, a)
        while a != 0:
            a, b = b % a, a
        return b
    
    def product(lst):
        prod = 1
        for n in lst:
            prod = (prod * n) % (10 ** 9 + 7)
        return prod
        
    # Have a global variable denote the bounds
    # Have the function denote how much to divide the bounds by
    bounds = None
    cache = None
        
    def coprime_count(divisor):
        if divisor > bounds[-1] // 2:
            return 1
        if divisor in cache:
            return cache[divisor]
        # bounds are assumed to be sorted
        scaled_bounds = [n // divisor for n in bounds]
        total = product(scaled_bounds)
        cap = scaled_bounds[0]
        # total = sum d from 1 to lowest bound coprime_count(d) (inclusive)
        rest = sum(coprime_count(divisor * d) for d in range(2, cap + 1))
        cache[divisor] = (total - rest) % (10 ** 9 + 7)
        return cache[divisor]
        
    t = int(input())
    for _ in range(t):
        k = int(input())
        bounds = list(map(int, input().split()))
        bounds.sort()
        cache = dict()
        total = 0
        for i in range(1, bounds[0]+1):
            total = (total + i * coprime_count(i)) % (10 ** 9 + 7)
        print(total)