Buggy python submission (verdict: Runtime Error):
from sys import stdin


def gcd(x, y):
    if x % y == 0:
        return y
    return gcd(y, x % y)


def main():
    k = int(stdin.readline())

    ans = 0

    for a in range(1, k + 1):
        for b in range(a, k + 1):
            d = gcd(a, b)
            for c in range(b, k + 1):
                e = gcd(d, c)
                ans += e * factorial(3) // factorial(3 - len(set([a, b, c])) + 1)

    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
from math import factorial

from sys import stdin


def gcd(x, y):
    if x % y == 0:
        return y
    return gcd(y, x % y)


def main():
    k = int(stdin.readline())

    ans = 0

    for a in range(1, k + 1):
        for b in range(a, k + 1):
            d = gcd(a, b)
            for c in range(b, k + 1):
                e = gcd(d, c)
                ans += e * factorial(3) // factorial(3 - len(set([a, b, c])) + 1)

    print(ans)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from math import factorial
import fractions as fr

from sys import stdin


def main():
    k = int(stdin.readline())

    ans = 0

    for a in range(1, k + 1):
        for b in range(a, k + 1):
            d = fr.gcd(a, b)
            for c in range(b, k + 1):
                e = fr.gcd(d, c)
                ans += e * factorial(3)//factorial(3 - len(set([a, b, c])) + 1)

    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
from math import factorial

from sys import stdin


def gcd(x, y):
    if x % y == 0:
        return y
    return gcd(y, x % y)


def main():
    k = int(stdin.readline())

    ans = 0

    for a in range(1, k + 1):
        for b in range(a, k + 1):
            d = gcd(a, b)
            for c in range(b, k + 1):
                e = gcd(d, c)
                ans += e * factorial(3) // factorial(3 - len(set([a, b, c])) + 1)

    print(ans)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import fractions as fr

from sys import stdin


def main():
    k = int(stdin.readline())

    g = [[[0 for _ in range(k)] for _ in range(k)] for _ in range(k)]

    for a in range(1, k + 1):
        for b in range(a, k + 1):
            g[a - 1][b - 1][0] = fr.gcd(a, b)
            for c in range(b, k + 1):
                g[a - 1][b - 1][c - 1] = fr.gcd(g[a - 1][b - 1][0], c)

    ans = 0

    for a in range(k):
        for b in range(k):
            for c in range(k):
                abc = sorted([a, b, c])
                ans += g[abc[0]][abc[1]][abc[2]]

    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
from math import factorial

from sys import stdin


def gcd(x, y):
    if x % y == 0:
        return y
    return gcd(y, x % y)


def main():
    k = int(stdin.readline())

    ans = 0

    for a in range(1, k + 1):
        for b in range(a, k + 1):
            d = gcd(a, b)
            for c in range(b, k + 1):
                e = gcd(d, c)
                ans += e * factorial(3) // factorial(3 - len(set([a, b, c])) + 1)

    print(ans)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
from sys import stdin


def main():
    k = int(stdin.readline())

    g = [[[0 for _ in range(k)] for _ in range(k)] for _ in range(k)]

    for a in range(1, k + 1):
        for b in range(a, k + 1):
            g[a - 1][b - 1][0] = fr.gcd(a, b)
            for c in range(b, k + 1):
                g[a - 1][b - 1][c - 1] = fr.gcd(g[a - 1][b - 1][0], c)

    ans = 0

    for a in range(k):
        for b in range(k):
            for c in range(k):
                abc = sorted([a, b, c])
                ans += g[abc[0]][abc[1]][abc[2]]

    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
from math import factorial

from sys import stdin


def gcd(x, y):
    if x % y == 0:
        return y
    return gcd(y, x % y)


def main():
    k = int(stdin.readline())

    ans = 0

    for a in range(1, k + 1):
        for b in range(a, k + 1):
            d = gcd(a, b)
            for c in range(b, k + 1):
                e = gcd(d, c)
                ans += e * factorial(3) // factorial(3 - len(set([a, b, c])) + 1)

    print(ans)


if __name__ == "__main__":
    main()