I want to have a (q^p) x p matrix, where q is a prime number.

And I want to construct a pattern such as below if q=3, p=3:

```
0  0  0
1  0  0
2  0  0
0  1  0
1  1  0
2  1  0
0  2  0
1  2  0
2  2  0
0  0  1
1  0  1
2  0  1
0  1  1
1  1  2
0  2  1
0  0  2
1  0  2
2  0  2
0  1  2
1  1  2
0  2  2
1  2  2
2  2  2

```

The code that I have so far is:

```
  F <- matrix(ncol=p,nrow=q^p) #initialize the vector F
  for(i in 1:(q^p)){
    for(j in 1:p){  
      F[i,j] <- ((i-1) %% q) * (j %% 3)
    }
  }

```

In case another example would be helpful, here's what a q=2, p=3 matrix should look like:

```
0  0  0
1  0  0
0  1  0
1  1  0
0  0  1
1  0  1
0  1  1
1  1  1

```

I know that I'll need to use some modular functions, but I am not sure how to achieve this problem.