#include <bits/stdc++.h>
using namespace std;
const int N = 1000010, Mo = 998244353;
int g[N];
inline int Pow(int x, int y) {
  int ret = 1;
  while (y) {
    if (y & 1) ret = 1LL * ret * x % Mo;
    x = 1LL * x * x % Mo, y >>= 1;
  }
  return ret;
}
int main() {
  int n, ans = 0;
  cin >> n;
  g[0] = 1;
  for (int i = 1; i <= n; i++)
    g[i] = 1LL * g[i - 1] * Pow(i, Mo - 2) % Mo * (n - i + 1) % Mo;
  for (int i = 1, t = 1; i <= n; i++, t = -t)
    ans = (ans + 1LL * t * g[i] * Pow(3, (1LL * n * (n - i) + i) % (Mo - 1))) %
          Mo;
  for (int i = 1, t = 1; i <= n; i++, t = -t)
    ans = (ans + 1LL * t * g[i] *
                     (3LL * Pow(Pow(3, n - i) - 1, n) +
                      1LL * (Pow(3, i) - 3) *
                          Pow(3, 1LL * (n - i) * n % (Mo - 1)) % Mo)) %
          Mo;
  cout << (ans + Mo) % Mo;
  return 0;
}
