Compute the Wilson numbers Given a positive integer n, compute the nth Wilson number W(n) where



and e = 1 if n has a primitive root modulo n, otherwise e = -1. In other words, n has a primitive root if there does not exist an integer x where 1 &lt; x &lt; n-1 and x2 = 1 mod n.


This is code-golf so create the shortest code for a function or program that computes the nth Wilson number for an input integer n > 0.
You may use either 1-based or 0-based indexing. You may also choose to output the first n Wilson numbers.
This is the OEIS sequence A157249.


Test Cases

n  W(n)
1  2
2  1
3  1
4  1
5  5
6  1
7  103
8  13
9  249
10 19
11 329891
12 32
13 36846277
14 1379
15 59793
16 126689
17 1230752346353
18 4727
19 336967037143579
20 436486
21 2252263619
22 56815333
23 48869596859895986087
24 1549256
25 1654529071288638505
Jelly, 8 7 bytes

1 byte thanks to Dennis.

gRỊTP‘:


Try it online!

You don't really have to compute e since you need to divide anyway.