  def each_fact(ary)
    is = ary.collect{|f_i| f_i.last}
    rect_index(is) do |idx|
      product = []
      idx.each_with_index do |k, i|
	product.push [ary[i].first, k]
      end
      yield PI(product)
    end
  end

  def rect_idx(a)
    if a.size > 0
      (0..a.first).each do |i|
	rect_idx(a[1..-1]) do |s|
	  yield [[i] + s.first, i + s.last]
	end
      end
    else
      yield [[], 0]
    end
  end
  
  def rect_index(a)
    s = []
    rect_idx(a) do |x|
      s.push x
    end
    s.sort{|x, y| x.last <=> y.last}.each do |x|
      a, d = x
      next if d == 0
      yield a
    end
  end
