WA in REC spoj

Can please someone tell me as of why am I getting a WA in this question?
Can you please tell any corner case I might be missing?

    import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args)throws java.lang.Exception
    {
        Scanner S = new Scanner(System.in);
        int T;
        T = S.nextInt();
        BigInteger A, B, N,MOD;
        BigInteger one = BigInteger.valueOf(1);
        for(int i = 0;i<T;i++)
        {
            A = S.nextBigInteger();
            B = S.nextBigInteger();
            N = S.nextBigInteger();
            MOD = S.nextBigInteger();
            if(N.compareTo(BigInteger.ZERO)==0)
            {
                BigInteger BO = BigInteger.ONE;
                BO = BO.mod(MOD);
                System.out.println(BO);
            }
            else if(N.compareTo(BigInteger.ONE)==0)//i.e. F = a + b
            {
                BigInteger ans = A.mod(MOD);
                ans = ans.add(B.mod(MOD));
                ans = ans.mod(MOD);
                System.out.println(ans);
            }
            else
            {
                BigInteger AA = A;
                AA = AA.subtract(one);//A = A - 1
                BigInteger BA = B;//L = B
                BigInteger BAAN = A.modPow(N, MOD);//A^N
                BAAN = BAAN.subtract(one);//A^N - 1
                BAAN = BAAN.mod(MOD);
                BAAN = BAAN.multiply(BA);//A^N*B - B
                BAAN = BAAN.divide(AA);//(A^N - 1)*B/(A-1)
               
                BAAN = BAAN.mod(MOD);
                BAAN = BAAN.add(A.modPow(N, MOD));
                BAAN = BAAN.mod(MOD);
                System.out.println(BAAN);
            }
            
        }
    }
}

when A=1 :slight_smile: