Problem p02555 (python)

Fixed submission:
S = int(input())
mod = 10 ** 9 + 7

if S <= 2:
    print(0)
    exit()

ans = 0
max_items = S // 3

# print("max_items", max_items)

for item in range(1, max_items + 1):
    # print("---------------------------")
    # print("item", item)
    n = S - 3 * item
    # print("n", n)
    if n == 0:
        ans += 1
        break

    modinv_table = [-1] * (n + 1)
    modinv_table[1] = 1
    for i in range(2, n + 1):
        modinv_table[i] = (-modinv_table[mod % i] * (mod // i)) % mod

    add = 1
    for p in range(n):
        add *= (n + item - 1) - p
        add *= modinv_table[p + 1]
        add %= mod


    # print("add", add)


    ans += add
    ans %= mod



print(ans)

Buggy submission (verdict: Runtime Error):
mod = 10 ** 9 + 7

class Solution:
    def FactorialMod(self, n):
        if n <= 1:
            return 1
        else:
            return n * self.FactorialMod(n - 1) % mod
    def ModInv(self, p):
        if p <= 1:
            return 1
        return (-self.ModInv(mod % p) * (mod // p)) % mod
    def FactorialModInv(self, p):
        if p <= 1:
            return 1
        return modinv_table[p] * self.FactorialModInv(p - 1)
    def Solve(self, n):
        ans = 0
        #Fill in the modinv_table
        for p in range(2, n + 1):
            modinv_table[p] = (-modinv_table[mod % p] * (mod // p)) % mod
        # print(modinv_table)
        for item in range(1, max_items + 1):
            # print("----------------------------------")
            # print("item", item)
            max_value = S - 3 * item
            # print("max_value", max_value)
            add = self.CalculateAddition(max_value, item)
            # print("add", add)
            ans += add
            ans %= mod
        return ans
    def CalculateAddition(self, max_value, item):
        if max_value == 0:
            return 1
        add = 1
        add = add * self.FactorialMod(max_value + item - 1) % mod
        add = add * self.FactorialModInv(max_value) % mod
        add = add * self.FactorialModInv(item - 1) % mod
        return add

S = int(input())
if S <= 2:
    print(0)
    exit()

max_items = S // 3
# print("max_items", max_items)
modinv_table = [-1] * (S + 1)
modinv_table[1] = 1

sol = Solution()
print(sol.Solve(S))

Buggy submission (verdict: Runtime Error):
mod = 10 ** 9 + 7

class Solution:
    def FactorialMod(self, n):
        if n <= 1:
            return 1
        else:
            return n * self.FactorialMod(n - 1) % mod
    def ModInv(self, p):
        if p <= 1:
            return 1
        return (-self.ModInv(mod % p) * (mod // p)) % mod
    def FactorialModInv(self, p):
        if p <= 1:
            return 1
        return modinv_table[p] * self.FactorialModInv(p - 1)
    def Solve(self, n):
        ans = 0
        #Fill in the modinv_table
        for p in range(2, n + 1):
            modinv_table[p] = (-modinv_table[mod % p] * (mod // p)) % mod
        # print(modinv_table)
        for item in range(1, max_items + 1):
            print("----------------------------------")
            print("item", item)
            max_value = S - 3 * item
            print("max_value", max_value)
            add = self.CalculateAddition(max_value, item)
            print("add", add)
            ans += add
            ans %= mod
        return ans
    def CalculateAddition(self, max_value, item):
        if max_value == 0:
            return 1
        add = 1
        add = add * self.FactorialMod(max_value + item - 1) % mod
        add = add * self.FactorialModInv(max_value) % mod
        add = add * self.FactorialModInv(item - 1) % mod
        return add

S = int(input())
if S <= 2:
    print(0)
    exit()

max_items = S // 3
# print("max_items", max_items)
modinv_table = [-1] * (S + 1)
modinv_table[1] = 1

sol = Solution()
print(sol.Solve(S))

Buggy submission (verdict: Runtime Error):
S = int(input())
mod = 10 ** 9 + 7

if S <= 2:
    print(0)
    exit()

ans = 0
max_items = S // 3

# print("max_items", max_items)

for item in range(1, max_items + 1):
    # print("---------------------------")
    n = S - 3 * item
    modinv_table = [-1] * (n + 1)
    modinv_table[1] = 1
    for i in range(2, n + 1):
        modinv_table[i] = (-modinv_table[mod % i] * (mod // i)) % mod

    add = 1
    for p in range(n):
        add *= (n + item - 1) - p
        add *= modinv_table[p + 1]
        add %= mod

    # print("item", item)
    # print("n", n)
    # print("add", add)


    ans += add
    ans %= mod



print(ans)