Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
mod = 998244353
half = pow(2, mod - 2, mod)
maxA = max(A)


def main():
    cnt = [0] * (maxA + 1)
    for Ai in A:
        cnt[Ai] += 1

    w = [pow(i, mod-2, mod) for i in range(maxA + 1)]
    for i in range(1, maxA + 1):
        for j in range(i * 2, maxA + 1, i):
            w[j] = (w[j] - w[i]) % mod

    def sum_cross(arr):
        s = 0
        s2 = 0
        for a in arr:
            s += a
            s2 += a ** 2
            s %= mod
            s2 %= mod
        return ((((s ** 2) % mod - s2) % mod) * half) % mod

    ans = 0
    for i in range(1, maxA + 1):
        Ai = []
        for j in range(i, maxA + 1, i):
            for k in range(cnt[j]):
                Ai.append(j)
        ans += w[i] * sum_cross(Ai)
        ans %= mod
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
N = int(input())
A = list(map(int, input().split()))
mod = 998244353
half = pow(2, mod - 2, mod)
maxA = max(A) + 1


def main():
    cnt = [0] * maxA
    for Ai in A:
        cnt[Ai] += 1

    w = [pow(i, mod-2, mod) for i in range(maxA)]
    for i in range(1, maxA):
        for j in range(i * 2, maxA, i):
            w[j] = (w[j] - w[i]) % mod

    ans = 0
    for i in range(1, maxA):
        s = s2 = 0
        for j in range(i, maxA, i):
            s = (s + (j * cnt[j])) % mod
            s2 = (s2 + (j * j * cnt[j])) % mod
        ans += w[i] * ((((s ** 2) % mod - s2) % mod) * half) % mod
        ans %= mod
    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
mod = 998244353
half = pow(2, mod - 2, mod)
maxA = max(A)


def main():
    cnt = [0] * (maxA + 1)
    for Ai in A:
        cnt[Ai] += 1

    w = [0] * (maxA + 1)
    for i in range(1, maxA + 1):
        w[i] += pow(i, mod - 2, mod)
        w[i] %= mod
        for j in range(i * 2, maxA + 1, i):
            w[j] -= w[i]
            w[j] %= mod


    def sum_cross(arr):
        s = 0
        s2 = 0
        for a in arr:
            s += a
            s2 += a ** 2
            s %= mod
            s2 %= mod
        return ((((s ** 2) % mod - s2) % mod) * half) % mod


    ans = 0
    for i in range(1, maxA + 1):
        Ai = []
        for j in range(i, maxA + 1, i):
            for k in range(cnt[j]):
                Ai.append(j)
        ans += w[i] * sum_cross(Ai)
        ans %= mod

    print(ans)

if __name__ == '__main__':
    main()


Fixed submission:
N = int(input())
A = list(map(int, input().split()))
mod = 998244353
half = pow(2, mod - 2, mod)
maxA = max(A) + 1


def main():
    cnt = [0] * maxA
    for Ai in A:
        cnt[Ai] += 1

    w = [pow(i, mod-2, mod) for i in range(maxA)]
    for i in range(1, maxA):
        for j in range(i * 2, maxA, i):
            w[j] = (w[j] - w[i]) % mod

    ans = 0
    for i in range(1, maxA):
        s = s2 = 0
        for j in range(i, maxA, i):
            s = (s + (j * cnt[j])) % mod
            s2 = (s2 + (j * j * cnt[j])) % mod
        ans += w[i] * ((((s ** 2) % mod - s2) % mod) * half) % mod
        ans %= mod
    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
mod = 998244353
half = pow(2, mod - 2, mod)
maxA = max(A)

cnt = [0] * (maxA + 1)
for Ai in A:
    cnt[Ai] += 1

w = [0] * (maxA + 1)
for i in range(1, maxA + 1):
    w[i] += pow(i, mod - 2, mod)
    w[i] %= mod
    for j in range(i * 2, maxA + 1, i):
        w[j] -= w[i]
        w[j] %= mod


def sum_cross(arr):
    s = 0
    s2 = 0
    for a in arr:
        s += a
        s2 += a ** 2
        s %= mod
        s2 %= mod
    return ((((s ** 2) % mod - s2) % mod) * half) % mod


ans = 0
for i in range(1, maxA + 1):
    Ai = []
    for j in range(i, maxA + 1, i):
        for k in range(cnt[j]):
            Ai.append(j)
    ans += w[i] * sum_cross(Ai)
    ans %= mod

print(ans)


Fixed submission:
N = int(input())
A = list(map(int, input().split()))
mod = 998244353
half = pow(2, mod - 2, mod)
maxA = max(A) + 1


def main():
    cnt = [0] * maxA
    for Ai in A:
        cnt[Ai] += 1

    w = [pow(i, mod-2, mod) for i in range(maxA)]
    for i in range(1, maxA):
        for j in range(i * 2, maxA, i):
            w[j] = (w[j] - w[i]) % mod

    ans = 0
    for i in range(1, maxA):
        s = s2 = 0
        for j in range(i, maxA, i):
            s = (s + (j * cnt[j])) % mod
            s2 = (s2 + (j * j * cnt[j])) % mod
        ans += w[i] * ((((s ** 2) % mod - s2) % mod) * half) % mod
        ans %= mod
    print(ans)


if __name__ == '__main__':
    main()