Help - CoDeFuSiOn - Number of ways

i don’t know where i am going wrong…
plz help me out!!!

mod=998244353
def fact(n):
    f=1
    for i in range(2,n+1):
        f=(f*i)%mod
    #print(f)
    return f
def val(n,r):
    ans=fact(n)//(fact(r)*fact(n-r))
    #print(ans)
    return ans
for _ in range(int(input())):
    n,m,x1,y1,x2,y2=map(int, input().split())
    total=val(n+m,m)
    # ways from 0,0 to x1,x2
    a1=val(x1+y1,y1)
    # ways from x1,y1 to n,m
    b1=val(n-x1+m-y1,m-y1)
    # ways from 0,0 to x2,y2
    a2=val(x2+y2,y2)
    # ways from x2,y2 to n,m
    b2=val(n-x2+m-y2,m-y2)
    # ways from x1,y1 to x2,y2
    a3=val(x2-x1+y2-y1,y2-y1)

    print((total-a1*b1-a2*b2+a1*b2*a3)%mod)

1.Trying using mod at every single step of final answer calculation:
print((total-a1b1-a2b2+a1b2a3)%mod)
and also add mod to the answer in case the final answer is negative.

2.def val(n,r):
ans=fact(n)//(fact®*fact(n-r))
#print(ans)
return ans
modular division is a suitable approach.