def numWays(self, S: str, MOD=int((1000000000.0 + 7))) -> int:
    N = len(S)
    cnt = len([c for c in S if (c == '1')])
    if (not cnt):
        return ((((N - 2) * (N - 1)) // 2) % MOD)
    if (cnt % 3):
        return 0
    K = (cnt // 3)
    first = 0
    second = 0
    ones = 0
    for i in range(N):
        if (S[i] == '1'):
            ones += 1
        if ((ones == (1 * K)) and (S[i] == '0')):
            first += 1
        if ((ones == (2 * K)) and (S[i] == '0')):
            second += 1
    return (((first + 1) * (second + 1)) % MOD)
def numWays(self, S: str, MOD=int((1000000000.0 + 7))) -> int:
    N = len(S)
    cnt = len([c for c in S if (c == '1')])
    if (not cnt):
        return ((((N - 2) * (N - 1)) // 2) % MOD)
    if (cnt % 3):
        return 0
    K = (cnt // 3)
    first = 0
    second = 0
    ones = 0
    for i in range(N):
        if (S[i] == '1'):
            ones += 1
        if ((ones == (1 * K)) and (S[i] == '0')):
            first += 1
        if ((ones == (2 * K)) and (S[i] == '0')):
            second += 1
    return (((first + 1) * (second + 1)) % MOD)