Why my first test case is wrong

import java.util.*;
public class Main { public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int N = s.nextInt();
int a[]=new int[N];
for(int i=0;i<N;i++){
a[i]=s.nextInt();
}
int M = s.nextInt();
System.out.print(lastIndex(a,M,a.length-1));
}
public static int lastIndex(int[] arr,int num,int i){
// if(i<0){
// return -1;
// }
if(arr.length==i)
return -1;
int val =lastIndex( arr,num, i+1);
if(val==-1) {
if(arr[i]==num){
return i;
} else {
return -1;
}
}else {
return val;
}
}
}

for test case:
7
45 -16 77 65 45 77 45
45
ans 7 your 6
check it