output_description: For each dataset, output the number of _clan_ modulo 10007.
input_description: The input consists of several datasets.
  
  The first line of each dataset contains an integer _n_ , which indicates the
  number of members in the family.
  
  Next _n_ lines represents information of the _i_ -th member with four
  integers. The first two integers respectively represent _b_[0] (the partner of
  _i_) and f(_i_ , _b_[0]) (the calling fee per unit time between _i_ and
  _b_[0]). The following two integers represent _b_[1] and _f_(_i_ , _b_[1]) in
  the same manner.
  
  Input terminates with a dataset where n = 0.
attempt: while True:
      n = int(input())
      if n == 0:
          break
      g = [[] for _ in range(n)]
      for u in range(n):
          v1, w1, v2, w2 = map(int, input().split())
          g[u].append((v1, w1))
          g[u].append((v2, w2))
  
      res = 1
      vis = [False] * n
      for i in range(n):
          if vis[i]:
              continue
          ws = []
          p, u = -1, i
          while True:
              vis[u] = True
              for v, w in g[u]:
                  if v != p:
                      ws.append(w)
                      p, u = u, v
                      break
              if u == i:
                  break
          res *= ws.count(max(ws))
      print(res)
  
problem_description: E: Huge Family
  
  Mr. Dango's family has extremely huge number of members. Once it had about 100
  members, and now it has as many as population of a city. It is jokingly
  guessed that the member might fill this planet in near future. They all have
  warm and gracious personality and are close each other.
  
  They usually communicate by a phone. Of course, They are all taking a family
  plan. This family plan is such a thing: when _a_ choose _b_ , and _b_ choose
  _a_ as a partner, a family plan can be applied between them and then the
  calling fee per unit time between them discounted to _f_(_a_ , _b_), which is
  cheaper than a default fee. Each person can apply a family plan at most 2
  times, but no same pair of persons can apply twice. Now, choosing their
  partner appropriately, all members of Mr. Dango's family applied twice.
  
  Since there are huge number of people, it is very difficult to send a message
  to all family members by a phone call. Mr. Dang have decided to make a phone
  calling network that is named '_clan_ ' using the family plan. Let us present
  a definition of _clan_.
  
  Let _S_ be an any subset of all phone calls that family plan is applied.
  _Clan_ is _S_ such that:
  
    1. For any two persons (let them be _i_ and _j_), if _i_ can send a message to _j_ through phone calls that family plan is applied (directly or indirectly), then _i_ can send a message to _j_ through only phone calls in _S_ (directly or indirectly).
    2. Meets condition 1 and a sum of the calling fee per unit time in _S_ is minimized.
  
  _Clan_ allows to send a message efficiently. For example, we suppose that one
  have sent a message through all calls related to him in the clan. Additionaly
  we suppose that every people follow a rule, "when he/she receives a message
  through a call in clan, he/she relays the message all other neibors in respect
  to clan." Then, we can prove that this message will surely be derivered to
  every people that is connected by all discounted calls, and that the message
  will never be derivered two or more times to same person.
  
  By the way, you are given information about application of family plan of Mr.
  Dango's family. Please write a program that calculates that in how many ways a
  different clan can be constructed. You should output the answer modulo 10007
  because it may be very big.
samples: [{"input": "1 1 2 3\n    0 1 2 2\n    1 2 0 3\n    7\n    1 2 2 1\n    0 2 3 2\n    0 1 3 1\n    2 1 1 2\n    5 3 6 2\n    4 3 6 1\n    4 2 5 1\n    0", "output": "2"}]