using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("3 3");
WillReturn.Add("1 2");
WillReturn.Add("2 3");
WillReturn.Add("3 1");
//6
}
else if (InputPattern == "Input2") {
WillReturn.Add("3 0");
//27
}
else if (InputPattern == "Input3") {
WillReturn.Add("4 6");
WillReturn.Add("1 2");
WillReturn.Add("2 3");
WillReturn.Add("3 4");
WillReturn.Add("2 4");
WillReturn.Add("1 3");
WillReturn.Add("1 4");
//0
}
else if (InputPattern == "Input4") {
WillReturn.Add("20 0");
//3486784401
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static int mN;
// 隣接リスト
static Dictionary<int, List<int>> mToNodeListDict = new Dictionary<int, List<int>>();
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
SplitAct(InputList[0]);
mN = wkArr[0];
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
int FromNode = wkArr[0];
int ToNode = wkArr[1];
if (mToNodeListDict.ContainsKey(FromNode) == false) {
mToNodeListDict[FromNode] = new List<int>();
}
if (mToNodeListDict.ContainsKey(ToNode) == false) {
mToNodeListDict[ToNode] = new List<int>();
}
mToNodeListDict[FromNode].Add(ToNode);
mToNodeListDict[ToNode].Add(FromNode);
}
// 処理1 連結成分分解を行う
Dictionary<int, List<int>> SCCListDict = DeriveCCListDict(1, mN);
// 処理2 連結成分ごとに色を塗り、塗り方の場合の数を返す
var PatternCntList = new List<long>();
foreach (var EachPair in SCCListDict) {
PatternCntList.Add(ExecPaint(EachPair.Value));
}
// 処理3 連結成分ごとに、場合の数の積の法則を使う
long Answer = 1;
PatternCntList.ForEach(pX => Answer *= pX);
Console.WriteLine(Answer);
}
struct JyoutaiDef1
{
internal int CurrNode; // 現在ノード
}
// 処理1 連結成分分解を行う
static Dictionary<int, List<int>> DeriveCCListDict(int pMinNode, int pMaxNode)
{
var CCListDict = new Dictionary<int, List<int>>();
var VisitedSet = new HashSet<int>();
for (int I = 1; I <= mN; I++) {
if (VisitedSet.Contains(I)) continue;
CCListDict[I] = new List<int>();
CCListDict[I].Add(I);
var Stk = new Stack<JyoutaiDef1>();
JyoutaiDef1 WillPush;
WillPush.CurrNode = I;
Stk.Push(WillPush);
VisitedSet.Add(I);
while (Stk.Count > 0) {
JyoutaiDef1 Popped = Stk.Pop();
// 子ノード無しの場合
if (mToNodeListDict.ContainsKey(Popped.CurrNode) == false)
continue;
foreach (int EachToNode in mToNodeListDict[Popped.CurrNode]) {
if (VisitedSet.Add(EachToNode) == false) continue;
CCListDict[I].Add(EachToNode);
WillPush.CurrNode = EachToNode;
Stk.Push(WillPush);
}
}
}
return CCListDict;
}
struct JyoutaiDef2
{
internal int CurrInd; // 現在ノード
internal Dictionary<int, char> ColorDict; // 塗った色[ノード]
}
// 処理2 連結成分ごとに色を塗り、塗り方の場合の数を返す
static long ExecPaint(List<int> pNodeList)
{
var Stk = new Stack<JyoutaiDef2>();
JyoutaiDef2 WillPush;
WillPush.CurrInd = 0;
WillPush.ColorDict = new Dictionary<int, char>();
WillPush.ColorDict[pNodeList[0]] = 'R'; // Rで決め打ちする
Stk.Push(WillPush);
long PatternCnt = 0;
while (Stk.Count > 0) {
JyoutaiDef2 Popped = Stk.Pop();
// クリア判定
if (pNodeList.Count == Popped.ColorDict.Count) {
PatternCnt++;
continue;
}
int NextNode = pNodeList[Popped.CurrInd + 1];
HashSet<char> CanUseColor = DeriveCanUseColor(NextNode, Popped.ColorDict);
foreach (char EachColor in CanUseColor) {
WillPush.CurrInd = Popped.CurrInd + 1;
WillPush.ColorDict = new Dictionary<int, char>(Popped.ColorDict);
WillPush.ColorDict[NextNode] = EachColor;
Stk.Push(WillPush);
}
}
return PatternCnt * 3; // Rで決め打ちしたので3倍する
}
// ノードとColorDictを引数として、使用可能な色のSetを返す
static HashSet<char> DeriveCanUseColor(int pNode, Dictionary<int, char> pColorDict)
{
var CanUseColorSet = new HashSet<char>("RGB");
if (mToNodeListDict.ContainsKey(pNode)) {
foreach (int EachToNode in mToNodeListDict[pNode]) {
if (pColorDict.ContainsKey(EachToNode)) {
CanUseColorSet.Remove(pColorDict[EachToNode]);
if (CanUseColorSet.Count == 0) break;
}
}
}
return CanUseColorSet;
}
}