segmantaion fault occurs in c program : I'm solving a problem in hacker rank,  when I'm running the  following code in my local system it's not showing any error  but when i submit it's showing segmentation fault. 

it accepted the first test case but remaining test cases it is showing segmentation fault.

#include<stdio.h>
#include<stdlib.h>

int main(void)
{
	
	int n,q,i,j;
	scanf("%d %d",&n,&q);

	int k,l,m,seq,lastAns=0;
	
	int **arr;
    arr=(int **) malloc(sizeof(int )*n);
   
    if(!arr) return 0;
    
    arr[0] = (int *) malloc(sizeof(int )*q*n);

    for(i = 0; i < n; i++)
            arr[i] = *arr + i*q ; 
    
    if(!*arr) return 0;

     for(i = 0; i < q ; i++)
       {
          scanf("%d%d%d",&k,&l,&m);
         

          switch (k)
          {
              case 1:  seq= (l^lastAns)%n;
                     
                       for(j = 0 ; j < q; j++)
                         {
                         	if(!arr[seq][j]) 
                         	  {
                         	   
                         	   arr[seq][j]=m;	
                         	 
                         	   break; 
                         	  }
                         }
                        break;

               case 2:  
                        seq= (l^lastAns)%n;
                        lastAns = arr[seq][m % n];
                        printf("%d\n", lastAns);
                        break;
          }

       }   


   
   free(arr[0]);

   free(arr);	   
   

  return 0;

    
}  


thanks in advance. 