#include <bits/stdc++.h>
using namespace std;
namespace xkMath {
long long quick_pow(long long a, long long b, long long mod = 1000000007) {
  long long x = 1;
  a %= mod;
  while (b) {
    if (b & 1) x = x * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return x;
}
long long A(long long a, long long b) {
  long long tmp = 1;
  for (int i = 0; i < b; ++i) {
    tmp = tmp * (a - i) % 1000000007;
  }
  return tmp;
}
int getFirstDigit(long long a, long long& muti) {
  muti = 1;
  while (a >= 10) a /= 10, muti * 10;
  return a;
}
int CmpVersion(char* tf, char* ts) {
  char f[1000], s[1000];
  memset(f, 0, sizeof(f));
  memset(s, 0, sizeof(s));
  strcpy(f, tf);
  strcpy(s, ts);
  int lf = 0, lenf = strlen(f);
  int ls = 0, lens = strlen(s);
  f[lenf] = '.';
  s[lens] = '.';
  int vs, vf;
  while (true) {
    int pf = lf, ps = ls;
    vs = vf = -1;
    while (f[pf] != '.' && f[pf] != 0) ++pf;
    while (s[ps] != '.' && s[ps] != 0) ++ps;
    if (!f[lf] && !s[ls]) return 0;
    if (!f[lf])
      vf = 0;
    else if (!s[ls])
      vs = 0;
    f[pf] = 0;
    s[ps] = 0;
    if (lf < 1000 && f[lf] && vs == -1)
      sscanf(f + lf, "%d", &vf);
    else
      vf = 0;
    if (ls < 1000 && s[ls] && vs == -1)
      sscanf(s + ls, "%d", &vs);
    else
      vs = 0;
    if (vf != vs) {
      return vf - vs;
    }
    lf = ++pf, ls = ++ps;
  }
}
}  // namespace xkMath
namespace xkIO {
int buf[20], len = 0;
inline void printInt(int a, int base = 10) {
  int len = 0;
  while (a) {
    buf[len++] = a % 10;
    a /= 10;
  }
  for (int i = len - 1; i >= 0; i--) {
    putchar(buf[i] + '0');
  }
}
}  // namespace xkIO
const int max_combination = 1000;
class xkCombination {
  long long comb[max_combination][max_combination];
  long long w;

 public:
  xkCombination(long long _w) {
    w = _w;
    memset(comb, 0, sizeof(comb));
    for (int i = 0; i <= w; ++i) {
      comb[i][0] = 1;
      comb[i][i] = 1;
    }
    for (int i = 2; i <= w; ++i) {
      for (int j = 1; j < i; ++j) {
        comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1];
        comb[i][j] %= 1000000007;
      }
    }
  }
  long long GetIndex(int a, int b) { return comb[a][b]; }
};
class xkCombination_n {
  long long data[1000003], mod;
  long long len = 0;

 public:
  xkCombination_n(int n, long long tmod, bool is_mod_prime = 1) {
    len = n;
    int ni = 0;
    if (is_mod_prime) ni = tmod - 2;
    mod = tmod;
    data[0] = 1;
    for (int i = 1; i <= n; ++i) {
      data[i] =
          data[i - 1] * (n - i + 1) % mod * xkMath::quick_pow(i, ni, mod) % mod;
    }
  }
  long long getIndex(int x) { return data[x]; }
  long long operator[](int x) {
    if (x <= len) return data[x];
    return -1;
  }
};
class HashInt {
 private:
  int hash[1000000];
  int tlen;

 public:
  HashInt(int* a, int len, int offset = 0) {
    if (len > 1000000) {
      printf("no enough space , place extend the class 'HashInt'");
      exit(-1);
    }
    for (int i = 0; i < len; ++i) hash[i] = a[i + offset];
    sort(hash, hash + len);
    tlen = unique(hash, hash + len) - hash;
  }
  int find(int a) { return lower_bound(hash, hash + tlen, a) - hash; }
};
class xkReplaceDivisor {
 private:
  vector<int> divisor;

 public:
  xkReplaceDivisor() {}
  xkReplaceDivisor(vector<int>& tmp) { init_with_vector(tmp); }
  void init_with_vector(vector<int>& tmp) {
    divisor.clear();
    int len = tmp.size();
    for (int i = 0; i < len; ++i) {
      divisor.push_back(tmp[i]);
    }
  }
  const vector<int>& GetVector() { return divisor; }
  void push_back(int a) { divisor.push_back(a); }
};
class xkReplacement {
 private:
  vector<int> replace;

 public:
  void push_back(int s) { replace.push_back(s); }
  vector<xkReplaceDivisor> MakeDivisor() {
    vector<xkReplaceDivisor> tmp;
    vector<bool> vis(replace.size(), 0);
    for (int i = 0; i < replace.size(); ++i)
      if (vis[i] == 0) {
        int now = replace[i];
        xkReplaceDivisor xktmp;
        while (vis[now - 1] == 0) {
          vis[now - 1] = 1;
          xktmp.push_back(now);
          now = replace[now - 1];
        }
        tmp.push_back(xktmp);
      }
    return tmp;
  }
};
class xkJieCheng {
  long long jie10e7[120] = {
      1,         682498929, 491101308, 76479948,  723816384, 67347853,
      27368307,  625544428, 199888908, 888050723, 927880474, 281863274,
      661224977, 623534362, 970055531, 261384175, 195888993, 66404266,
      547665832, 109838563, 933245637, 724691727, 368925948, 268838846,
      136026497, 112390913, 135498044, 217544623, 419363534, 500780548,
      668123525, 128487469, 30977140,  522049725, 309058615, 386027524,
      189239124, 148528617, 940567523, 917084264, 429277690, 996164327,
      358655417, 568392357, 780072518, 462639908, 275105629, 909210595,
      99199382,  703397904, 733333339, 97830135,  608823837, 256141983,
      141827977, 696628828, 637939935, 811575797, 848924691, 131772368,
      724464507, 272814771, 326159309, 456152084, 903466878, 92255682,
      769795511, 373745190, 606241871, 825871994, 957939114, 435887178,
      852304035, 663307737, 375297772, 217598709, 624148346, 671734977,
      624500515, 748510389, 203191898, 423951674, 629786193, 672850561,
      814362881, 823845496, 116667533, 256473217, 627655552, 245795606,
      586445753, 172114298, 193781724, 778983779, 83868974,  315103615,
      965785236, 492741665, 377329025, 847549272, 698611116,
  };
  const int gap = 10000000;

 public:
  long long GetIndex(int id) {
    long long tmp = jie10e7[id / gap];
    int start = id / gap * gap + 1;
    for (start; start <= id; ++start) {
      tmp = tmp * start % 1000000007;
    }
    return tmp;
  }
};
class BIT {
 public:
  int MAX_BIT_LEN = 300000;
  long long* data;
  BIT(int size) {
    data = new long long[size + 1];
    MAX_BIT_LEN = size + 1;
    clear();
  }
  void clear() { memset(data, 0, MAX_BIT_LEN * sizeof(long long)); }
  long long query(int x) {
    long long ans = 0;
    while (x) {
      ans += data[x];
      x -= (x & (-x));
    }
    return ans;
  }
  void insert(int x, int val) {
    assert(x);
    while (x < MAX_BIT_LEN) {
      data[x] += val;
      x += (x & (-x));
    }
  }
};
;
using namespace std;
int main() {
  long long mod = 998244353;
  long long n;
  cin >> n;
  xkCombination_n com(n, mod);
  int i;
  long long ans = 0;
  for (i = 1; i < n + 1; ++i) {
    using namespace xkMath;
    long long tt = quick_pow(3, n * (n - i), mod);
    long long t3i = quick_pow(3, i, mod);
    long long tmp = tt * t3i % mod;
    tmp += (t3i + mod - 3) * (tt) % mod +
           3 * quick_pow((quick_pow(3, n - i, mod) + mod - 1), n, mod);
    tmp %= mod;
    tmp = tmp * com.getIndex(i);
    tmp %= mod;
    if (i & 1)
      ans += tmp;
    else
      ans += mod - tmp;
    ans %= mod;
  }
  cout << ans << endl;
}
